I found this code on the internet, and it seems it works fine with others. But I always get something like this: Unhandled exception at 0x012c1073 in examplexx.exe: 0xC0000005: Access violation reading location 0x00000028.
Have anybody idea what might be a problem?
#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);
}
Here is an example for 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;
}
Draw multiple images in single window is well discussed in another thread OpenCV draw an image over another image
By the way, you can use something like this:
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));
In this case I assume the images have the same size and you want to show them in one column.
User contributions licensed under CC BY-SA 3.0