首页 > 解决方案 > 如何拼接两个具有不同绝对坐标的图像?

问题描述

例如,缝合

first image
1 1 1
1 1 1
1 1 1

second image
2 2 2 2
2 2 2 2
2 2 2 2

and What I want
0 0 0 2 2 2 2
1 1 1 2 2 2 2 
1 1 1 2 2 2 2
1 1 1 0 0 0 0

或者

1 1 1 0 0 0 0
1 1 1 2 2 2 2 
1 1 1 2 2 2 2
0 0 0 2 2 2 2

在python中,这很容易像..

temp_panorama = np.zeros((1's height+abs(2's upper part length), 1's width+2's width))
temp_panorama[(2's upper part length) : 1's height, 0 : 1's width] = img1[:]
temp_panorama[0 : 2's height, 1's width +1 :] = img2[:, :]

但是如何在 C++ 的 opencv 中实现相同的功能呢?

标签: opencv

解决方案


使用子图像:

// ROI where first image will be placed
cv::Rect firstROI = cv::Rect(x1,y2, first.cols, first.height);
cv::Rect secondROI = cv::Rect(x2,y2, second.cols, second.height);

// create an image big enought to hold the result
cv::Mat canvas = cv::Mat::zeros(cv::Size(std::max(x1+first.cols, x2+second.cols), std::max(y1+first.rows, y2+second.rows)), first.type());

// use subimages:
first.copyTo(canvas(firstROI));
second.copyTo(canvas(secondROI));

在你的例子中:

x1 = 0, 
y1 = 1, 
x2 = 3, 
y2 = 0

first.cols == 3 first.rows == 3 second.cols == 4 second.rows == 3


推荐阅读