1

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

enter image description here

1 Answer 1

0

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;
}
7
  • Thank you so much! It worked perfectly! Lets say if i want more images like in 4x4, i set it to this way? int dstWidth = max(image1.cols, image2.cols,image3.cols,image4.cols); int dstHeight = image1.rows + image2.rows+image3.rows+image4.rows;
    – TxRx
    Commented Feb 28, 2016 at 13:45
  • No, 4x4 is a little bit trickier... I'll post an example in a few minutes
    – Miki
    Commented Feb 28, 2016 at 13:47
  • Posted code for 4 images in 2x2. Now you can extend this how you prefer
    – Miki
    Commented Feb 28, 2016 at 13:56
  • Its works like a matrix I'm not sure I understand... But yes, you're working on matrices ;D
    – Miki
    Commented Feb 28, 2016 at 14:17
  • sorry i just want to ask, I added a mouseclick function, I want to open a url by clicking on the image by using ShellExecute. Is it possible to open different urls by clicking on the different images?
    – TxRx
    Commented Feb 28, 2016 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.