Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two images in opencv?

Tags:

I have calculated homography ,taken out perspective transform .I am able two display two images in one window but unable to merge them.Here are my example images-> image1

image2

The code I am using thiscode ->

cv::warpPerspective(image2,warpresult2,homography,cv::Size(2*image2.cols,image2.rows));   Mat imgResult(image1.rows,2*image1.cols,image1.type());  Mat roiImgResult_Left = imgResult(Rect(0,0,image1.cols,image1.rows));  Mat roiImgResult_Right = imgResult(Rect(image1.cols,0,image2.cols,image2.rows));   Mat roiImg1 = image1(Rect(0,0,image1.cols,image1.rows)); Mat roiImg2 = warpresult2(Rect(0,0,image2.cols,image2.rows));  roiImg1.copyTo(roiImgResult_Left); //Img1 will be on the left of imgResult roiImg2.copyTo(roiImgResult_Right); //Img2 will be on the right of imgResult  imshow("Finalimg",imgResult); imwrite("C:\\OpenCv_Projects\\outputimage.jpg",imgResult); cvWaitKey(0); 

I think the problem is in the coordinates that i am giving roiImgResult_right.

And the Output Image is -> Output images As you can see the images are not properly merge and there is black area on the right side.how to remove that also?

like image 317
ankit Avatar asked Apr 21 '12 06:04

ankit


People also ask

How do I merge two photos in cv2?

We can use the concatenate() function of NumPy to concatenate the matrices of the images along different axes. For example, let's use the zeros() function of NumPy to create two images with different colors and then combine them horizontally using the concatenate() function.

How do I blend images in OpenCV?

Like before, we start by importing the cv2 module, followed by reading both images and resizing them to be 400×400. We will then display the second image in a window called “blend“. It will be the one we will use every time we update the weights to display the resulting image.


2 Answers

OpenCV already has image stitching implemented. If you compile with "-D BUILD_EXAMPLES", you can use the binary stitching_detailed. The usage is simple: ./stitching_detailed img1 img2 ...

Or, you can just use the stitcher class (example from here):

#include "opencv2/highgui/highgui.hpp" #include "opencv2/stitching/stitcher.hpp"  using namespace std; using namespace cv;  bool try_use_gpu = false; string result_name = "result.jpg";  int main(int argc, char* argv[]) {     vector<Mat> imgs;     // add images...      Mat pano;     Stitcher stitcher = Stitcher::createDefault(try_use_gpu);     stitcher.stitch(imgs, pano);     imwrite(result_name, pano); } 
like image 57
erikreed Avatar answered Sep 27 '22 21:09

erikreed


  • Image Blending: You may use a laplacian pyramid blending. see a sample code here using opencv. You can use whatever mask you like (which is a binary mask).

  • create Panorama If you want to make a panorama, you may use Min-Cut Stitching. I found this code which do the panorama processing.

like image 32
0x90 Avatar answered Sep 27 '22 21:09

0x90