在单个窗口OPENCV中显示多个图像

时间:2015-02-15 00:15:45

标签: opencv

我在互联网上找到了这个代码,似乎它可以和其他人一起使用。但我总是得到这样的东西: examplexx.exe中0x012c1073处的未处理异常:0xC0000005:访问冲突读取位置0x00000028。

有人知道可能存在什么问题吗?

#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);

}

2 个答案:

答案 0 :(得分:2)

以下是Opencv 2.4.10的示例:

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

int main(int argc, char *argv[])
{
    // read an image
    cv::Mat image1= cv::imread("/home/user/im.png");
    cv::Mat image2= cv::imread("/home/user/im.png");

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

答案 1 :(得分:0)

在另一个线程OpenCV draw an image over another image

中详细讨论了在单个窗口中绘制多个图像

顺便说一下,你可以使用这样的东西:

image1.copyTo(dst.rowRange(0, rows).colRange(0, cols));
image2.copyTo(dst.rowRange(rows, 2 * rows).colRange(0, cols));
image3.copyTo(dst.rowRange(rows * 2, 3 * rows).colRange(0, cols));

在这种情况下,我假设图像大小相同,并且您希望将它们显示在一列中。