如何在opencv中合并两个图像?

ank*_*kit 20 opencv image-processing computer-vision visual-c++ image-stitching

我已经计算出单应性,取出了透视变换.我能够在一个窗口中显示两个图像,但无法合并它们.这是我的示例图像 - > 此搜索

图像2

我正在使用此代码的代码 - >

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);
Run Code Online (Sandbox Code Playgroud)

我认为问题出在我给予roiImgResult_right的坐标中.

输出图像是 - > 输出图像 如你所见,图像没有正确合并,右侧有黑色区域.如何删除它?

eri*_*eed 17

OpenCV已经实现了图像拼接.如果使用"-D BUILD_EXAMPLES"进行编译,则可以使用binary stitching_detailed.用法很简单:./stitching_detailed img1 img2 ...

或者,您可以使用拼接器类(此处的示例):

#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);
}
Run Code Online (Sandbox Code Playgroud)


0x9*_*x90 7

  • 图像混合:您可以使用拉普拉斯金字塔混合.使用opencv在这里查看示例代码.你可以使用你喜欢的任何掩码(这是一个二进制掩码).

  • 创建全景如果要制作全景图,可以使用最小切割拼接.我找到了执行全景处理的代码.