If the images do not have the same size, the combine width will be equal to the sum of the width, but the height should be greater than the height of the two images.
Define a combination image as follows:
Mat combine(max(img_object.size().height, img_scene.size().height), img_object.size().width + img_scene.size().width, CV_8UC3);
Note that we simply create a new Mat object with a height equal to the maximum height and a width equal to the combined width of the images (if you need a small distance between the images, you need to consider this here).
Then you can define areas of interest for each side inside combine (using the convenient Mat constructor) and finally copy each image to the corresponding side (here I assume that the object goes to the left and the scene goes to the right):
Mat left_roi(combine, Rect(0, 0, img_object.size().width, img_object.size().height)); img_object.copyTo(left_roi); Mat right_roi(combine, Rect(img_object.size().width, 0, img_scene.size().width, img_scene.size().height)); img_scene.copyTo(right_roi);
Edit: Fixed the typo that Timzaman indicated.
source share