#includeusing namespace cv; int main() { Mat mat1 = Mat::zeros(2, 3, CV_8UC1); // Create a 2x3 matrix with all elements initialized to 0 Mat row1 = Mat::ones(1, 3, CV_8UC1) * 255; // Create a 1x3 row vector with all elements initialized to 255 Mat row2 = Mat::ones(1, 3, CV_8UC1) * 127; // Create a 1x3 row vector with all elements initialized to 127 mat1.push_back(row1); // Append row1 to the end of mat1 mat1.push_back(row2); // Append row2 to the end of mat1 std::cout << mat1 << std::endl; // Output the resulting matrix to the console return 0; }
[255, 255, 255; 127, 127, 127; 0, 0, 0]
#includeusing namespace cv; int main() { Mat mat1 = Mat::zeros(3, 2, CV_8UC1); // Create a 3x2 matrix with all elements initialized to 0 Mat col1 = Mat::ones(3, 1, CV_8UC1) * 255; // Create a 3x1 column vector with all elements initialized to 255 Mat col2 = Mat::ones(3, 1, CV_8UC1) * 127; // Create a 3x1 column vector with all elements initialized to 127 hconcat(mat1, col1, mat1); // Concatenate col1 to mat1 horizontally hconcat(mat1, col2, mat1); // Concatenate col2 to mat1 horizontally std::cout << mat1 << std::endl; // Output the resulting matrix to the console return 0; }
[0, 0, 255, 127; 0, 0, 255, 127; 0, 0, 255, 127]In this example, we first created a 3x2 matrix called mat1, and then created two 3x1 column vectors, col1 and col2, with different values. We then used hconcat() to concatenate these two columns to the right of mat1, resulting in a new 3x4 matrix with the two columns added on the right. Package library: OpenCV library