使用OpenCV 3.0和MSVS 12在单个窗口上显示多个图像

时间:2016-02-20 17:06:06

标签: c++ opencv visual-studio-2012

我是opencv和c ++的新手,目前正在制作一个程序,要求我用opencv 3.0和visual studio 12显示多个图像。我正在处理以下代码,但它不起作用。我想问一下如何解决这个问题。

#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/core/core.hpp>        
#include <opencv2/highgui/highgui.hpp>
#include <iostream>


using namespace cv;
using namespace std;

int main()
{
//Image Reading
IplImage* img1 = cvLoadImage( "ball.jpg" );
IplImage* img2 = cvLoadImage( "ball.jpg" );
IplImage* img3 = cvLoadImage( "ball.jpg" );
IplImage* img4 = cvLoadImage( "ball.jpg" );

int dstWidth=img1->width+img1->width;
int dstHeight=img1->height+img1->height;

IplImage* dst=cvCreateImage(cvSize(dstWidth,dstHeight),IPL_DEPTH_8U,3); 

// Copy first image to dst
cvSetImageROI(dst, cvRect(0, 0,img1->width,img1->height) );
cvCopy(img1,dst,NULL);
cvResetImageROI(dst);

// Copy second image to dst
cvSetImageROI(dst, cvRect(img2->width, 0,img2->width,img2->height) );
cvCopy(img2,dst,NULL);
cvResetImageROI(dst);

// Copy third image to dst
cvSetImageROI(dst, cvRect(0, img3->height,img3->width,img3->height) );
cvCopy(img3,dst,NULL);
cvResetImageROI(dst);

// Copy fourth image to dst
cvSetImageROI(dst, cvRect(img4->width, img4->height,img4->width,img4->height) );
cvCopy(img4,dst,NULL);
cvResetImageROI(dst);

//show all in a single window
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", dst );
cvWaitKey(0);

}

1 个答案:

答案 0 :(得分:2)

如果所有图片都具有相同的尺寸,请使用C ++ API执行此操作:

int main(int argc, char* argv[])
{
    cv::Mat input1 = cv::imread("C:/StackOverflow/Input/Lenna.png");
    cv::Mat input2 = cv::imread("C:/StackOverflow/Input/Lenna.png");
    cv::Mat input3 = cv::imread("C:/StackOverflow/Input/Lenna.png");
    cv::Mat input4 = cv::imread("C:/StackOverflow/Input/Lenna.png");

    int width = 2*input1.cols; // width of 2 images next to each other
    int height = 2*input1.rows; // height of 2 images over reach other

    cv::Mat inputAll = cv::Mat(height, width, input1.type());

    cv::Rect subImageROI = cv::Rect(0, 0, input1.cols, input1.rows);

    // copy to subimage:
    input1.copyTo(inputAll(subImageROI));

    // move to 2nd image ROI position:
    subImageROI.x = input1.cols;
    input2.copyTo(inputAll(subImageROI));

    subImageROI.x = 0;
    subImageROI.y = input1.rows;
    input3.copyTo(inputAll(subImageROI));

    subImageROI.x = input1.cols;
    subImageROI.y = input1.rows;
    input4.copyTo(inputAll(subImageROI));


    cv::imshow("input", inputAll);
    cv::waitKey(0);
    return 0;
}