如何合并两个图像在opencv? [英] How to merge two images in opencv?

查看:657
本文介绍了如何合并两个图像在opencv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经计算单应性,取出透视变换。我可以在一个窗口中显示两个图像,但无法合并它们。这是我的示例图像 - >

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->

我使用的代码 - >

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);

我认为问题出在我给roiImgResult_right的坐标。

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

输出图像是 - >
$ b $

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

推荐答案

p> OpenCV已经实现了图像拼接。如果使用-D BUILD_EXAMPLES编译,可以使用二进制 stitching_detailed 。用法很简单: ./ stitching_detailed img1 img2 ...

或者,你可以使用stitcher类(来自此处的示例): p>

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);
}

这篇关于如何合并两个图像在opencv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆