Displaying multiple image on single window not working properly (C++ VS12)

穿精又带淫゛_ 提交于 2019-12-13 02:20:49

问题


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

This is the result of the program above


回答1:


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

image2.copyTo(targetROI);

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) to set dstWidth and dstHeight like:

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

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

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

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


来源:https://stackoverflow.com/questions/35683144/displaying-multiple-image-on-single-window-not-working-properly-c-vs12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!