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
source to share
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));
source to share
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.
source to share