本文介绍了在单个窗口上显示多个图像无法正常工作(C ++ VS12)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过在这里获得的这段代码.它用于在C ++的单个窗口上显示多个图像.我在程序中也包括了opencv 3.0库.下面是代码.我正在尝试加载2张图像,但仅出现第一个(1.jpg),但是当我将image2等于cv :: imread("1.jpg");出现1.jpg的两个图像.我真的很陌生,我不明白我要去哪里错了.我希望有一个人可以帮助我.谢谢你.

I have tried this code I gotten here. Its for displaying multiple images on a single window for C++. I have included the opencv 3.0 library on the program as well. Below is the code. I am trying to load 2 images but only the first one (1.jpg) appears but when i put image2 to be equal to cv::imread("1.jpg"); two images of 1.jpg appears. I am really new to this and I dont understand where i am going wrong here. I hope someone can help me. Thank you.

int main(int argc, char *argv[])
{
    // read an image
    cv::Mat image1= cv::imread("1.jpg");
    cv::Mat image2= cv::imread("2.jpg");

    int dstWidth = image1.cols;
    int dstHeight = image1.rows * 2;

    cv::Mat dst = cv::Mat(dstHeight, dstWidth, CV_8UC3, cv::Scalar(0,0,0));
    cv::Rect roi(cv::Rect(0,0,image1.cols, image1.rows));
    cv::Mat targetROI = dst(roi);
    image1.copyTo(targetROI);
    targetROI = dst(cv::Rect(0,image1.rows,image1.cols, image1.rows));
    image2.copyTo(targetROI);

    // create image window named "My Image"
    cv::namedWindow("OpenCV Window");
    // show the image on window
    cv::imshow("OpenCV Window", dst);
    // wait key for 5000 ms
    cv::waitKey(5000);

    return 0;
}

这是上面程序的结果

推荐答案

如果图像大小相同,您的代码对我来说就可以正常工作.否则,调用

Your code works ok for me, if images have the same size. Otherwise, the call to

image2.copyTo(targetROI);

会将image2复制到新创建的图像中,而不是您期望的那样复制到dst中.

will copy image2 into a newly created image, not in dst as you would expect.

如果您想使其正常运行,您应该:

If you want to make it work in general, you should:

1)将dstWidthdstHeight设置为:

int dstWidth = max(image1.cols, image2.cols);
int dstHeight = image1.rows + image2.rows;

2)将第二个ROI设置为第二张图像的大小:

2) set the second ROI with the size of the second image:

targetROI = dst(cv::Rect(0, image1.rows, image2.cols, image2.rows));
//                                            ^            ^


根据评论,要显示以2x2布置的4张图像,您需要做更多的工作:


From the comments, to show 4 images disposed as 2x2, you need a little more work:

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    // read an image
    cv::Mat image1 = cv::imread("path_to_image1");
    cv::Mat image2 = cv::imread("path_to_image2");
    cv::Mat image3 = cv::imread("path_to_image3");
    cv::Mat image4 = cv::imread("path_to_image4");

    //////////////////////
    // image1   image2
    // image3   image4
    //////////////////////

    int max13cols = max(image1.cols, image3.cols);
    int max24cols = max(image2.cols, image4.cols);
    int dstWidth = max13cols + max24cols;

    int max12rows = max(image1.rows, image2.rows);
    int max34rows = max(image3.rows, image4.rows);
    int dstHeight = max12rows + max34rows;

    cv::Mat dst = cv::Mat(dstHeight, dstWidth, CV_8UC3, cv::Scalar(0, 0, 0));

    cv::Rect roi(cv::Rect(0, 0, image1.cols, image1.rows));
    image1.copyTo(dst(roi));

    roi = cv::Rect(max13cols, 0, image2.cols, image2.rows);
    image2.copyTo(dst(roi));

    roi = cv::Rect(0, max12rows, image3.cols, image3.rows);
    image3.copyTo(dst(roi));

    roi = cv::Rect(max13cols, max12rows, image4.cols, image4.rows);
    image4.copyTo(dst(roi));

    cv::imshow("OpenCV Window", dst);
    cv::waitKey(0);

    return 0;
}

这篇关于在单个窗口上显示多个图像无法正常工作(C ++ VS12)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:25