I'm new to opencv and i searched on the internet if there is an example of how to merge two images, but didin't found anything good to help me. Can someone help me with some indications or a small code to understand ? thanks in advance
Merging two imagesCreate an empty image using the Image. new() function. Paste the images using the paste() function. Save and display the resultant image using the save() and show() functions.
From the comments to the question, you said:
I dont want to blend half from the first picture with the other half from the second. I just waint to print both images, one near the other one
So, starting from these images:
You want this result?
Note that if both images have the same height, you won't see the black background.
Code:
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
// Load images
Mat3b img1 = imread("path_to_image_1");
Mat3b img2 = imread("path_to_image_2");
// Get dimension of final image
int rows = max(img1.rows, img2.rows);
int cols = img1.cols + img2.cols;
// Create a black image
Mat3b res(rows, cols, Vec3b(0,0,0));
// Copy images in correct position
img1.copyTo(res(Rect(0, 0, img1.cols, img1.rows)));
img2.copyTo(res(Rect(img1.cols, 0, img2.cols, img2.rows)));
// Show result
imshow("Img 1", img1);
imshow("Img 2", img2);
imshow("Result", res);
waitKey();
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With