Combine two Mat images into one

I have a problem. I have an image. Then I have to split the image into two equal parts. I did it like this (code compiled, all is well):

Mat image_temp1 = image(Rect(0, 0, image.cols, image.rows/2)).clone();
Mat image_temp2 = image(Rect(0, image.rows/2, image.cols, image.rows/2)).clone();

      

Then I have to change each part myself and finally merge into one. I have no idea how to get it right. How am I supposed to combine these 2 parts of the image into one image?
Example: http://i.stack.imgur.com/CLDK7.jpg

+3


source to share


3 answers


There are several ways to do this, but the best way I've found is to use cv::hconcat(mat1, mat2, dst)

for horizontal merge or cv::vconcat(mat1, mat2, dst)

for vertical.



Don't forget to take care of the empty matrix addition case!

+7


source


It seems that cv :: Mat :: push_back is exactly what you are looking for:

C ++: void Mat :: push_back (const Mat & m): adds elements to the bottom of the matrix.

Parameters:    
    m – Added line(s).

      

Methods add one or more elements to the bottom of the matrix. When elem is Mat, its type and number of columns must be the same , as in a container matrix.



Perhaps you can create a new one of the cv::Mat

size you want and place parts of the image right in it:

Mat image_temp1 = image(Rect(0, 0, image.cols, image.rows/2)).clone();
Mat image_temp2 = image(Rect(0, image.rows/2, image.cols, image.rows/2)).clone();
...
cv::Mat result(image.rows, image.cols);
image_temp1.copyTo(result(Rect(0, 0, image.cols, image.rows/2)));
image_temp2.copyTo(result(Rect(0, image.rows/2, image.cols, image.rows/2));

      

+3


source


How about this:

Mat newImage = image.clone();
Mat image_temp1 = newImage(Rect(0, 0, image.cols, image.rows/2));
Mat image_temp2 = newImage(Rect(0, image.rows/2, image.cols, image.rows/2));

      

By not using clone()

temporal images to create, you are implicitly newImage

modifying when temporal images change without having to merge them again. After changing image_temp1

and image_temp2

, it newImage

will be exactly the same as if you split, modify, and then merge the subimages.

+1


source







All Articles