image-processing - OpenCV 在同一窗口中相邻显示 2 个图像

标签 image-processing opencv roi

我正在尝试使用 OpenCV 在同一窗口中显示 2 个彼此水平相邻的图像。

我已经尝试为此使用 adjustROI 函数。图片 1 的宽度为 1088 像素,高度为 2208 像素,而图片 2 的宽度为 1280 像素,高度为 2208 像素。请指出以下代码中可能存在的问题。我得到的只是大小为 Image2 的图像以及来自 Image2 的内容。

Mat img_matches=Mat(2208,2368,imgorig.type());//set size as combination of img1 and img2
img_matches.adjustROI(0,0,0,-1280); 
imgorig.copyTo(img_matches);
img_matches.adjustROI(0,0,1088,1280);
imgorig2.copyTo(img_matches);

最佳答案

编辑:以下是我如何做您想做的事:

Mat left(img_matches, Rect(0, 0, 1088, 2208)); // Copy constructor
imgorig.copyTo(left);
Mat right(img_matches, Rect(1088, 0, 1280, 2208)); // Copy constructor
imgorig2.copyTo(right);

复制构造函数创建一个 Mat header 的副本,该 header 指向由每个 Rect 定义的 ROI。

完整代码:

#include <cv.h>
#include <highgui.h>

using namespace cv;

int
main(int argc, char **argv)
{
    Mat im1 = imread(argv[1]);
    Mat im2 = imread(argv[2]);
    Size sz1 = im1.size();
    Size sz2 = im2.size();
    Mat im3(sz1.height, sz1.width+sz2.width, CV_8UC3);
    Mat left(im3, Rect(0, 0, sz1.width, sz1.height));
    im1.copyTo(left);
    Mat right(im3, Rect(sz1.width, 0, sz2.width, sz2.height));
    im2.copyTo(right);
    imshow("im3", im3);
    waitKey(0);
    return 0;
}

编译:

g++ foo.cpp -o foo.out `pkg-config --cflags --libs opencv`

编辑 2:

下面是 adjustROI 的效果:

#include <cv.h>
#include <highgui.h>

using namespace cv;

int
main(int argc, char **argv)
{
    Mat im1 = imread(argv[1]);
    Mat im2 = imread(argv[2]);
    Size sz1 = im1.size();
    Size sz2 = im2.size();
    Mat im3(sz1.height, sz1.width+sz2.width, CV_8UC3);
    // Move right boundary to the left.
    im3.adjustROI(0, 0, 0, -sz2.width);
    im1.copyTo(im3);
    // Move the left boundary to the right, right boundary to the right.
    im3.adjustROI(0, 0, -sz1.width, sz2.width);
    im2.copyTo(im3);
    // restore original ROI.
    im3.adjustROI(0, 0, sz1.width, 0);
    imshow("im3", im3);
    waitKey(0);
    return 0;
}

您必须跟踪当前的 ROI,移动 ROI 的语法可能有点不直观。结果与第一段代码相同。

关于image-processing - OpenCV 在同一窗口中相邻显示 2 个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13133055/

相关文章:

opencv - opencv将倾斜 View 转换为平面 View

c++ - 为什么垫子的尺寸不同?

python - OpenCV 的 selectROI 坐标如何工作?

c++ - 组合两个矩阵,保留它们的元素值

iphone - 确定图像亮度/亮度

algorithm - 检测二值图像中的圆圈

opencv - 使用g++进行编译的OpenCV错误(Mountain Lion 10.8)

python - 如何在 Python 和 OpenCV 中使用 "xphoto_WhiteBalancer.balanceWhite"?

visual-studio-2010 - OpenCV GPU程序未运行

ios - GLImageProcessing ROI(感兴趣区域)