Ask Your Question
0

display many images in single window in opencv

asked 2013-10-26 05:51:02 -0600

zulfiqar gravatar image

updated 2013-10-26 06:22:06 -0600

berak gravatar image

I am new to opencv. I want to display many images in a single window currently i m showing each image in new window and old window get destroyed.I don't know number of images before hand. below is my code

 // newproject.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
//Image Reading
IplImage* img = cvLoadImage( "index.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvDestroyWindow( "Example1" );
cout<<img->height<<endl;
cout<<img->width<<endl;

//Summarize by sampling

CvCapture* capture = 0;
capture = cvCreateFileCapture( "video.avi" );
if(!capture){
    return -1;
}
IplImage *bgr_frame=cvQueryFrame(capture);//Init the video read
double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);
CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter *writer = cvCreateVideoWriter("ayesh.avi",-1,10,size);
IplImage* logpolar_frame = cvCreateImage(size,IPL_DEPTH_8U,3);

cout<<size.height<<endl;
cout<<size.width<<endl;

int x=0;
while(1){       

    x++;
    bgr_frame = cvQueryFrame( capture );
    if(!bgr_frame)
        break;

    if(x==19||x==21||x==18||x==17||x==22){
        cvShowImage("Example2",bgr_frame);
        cvWriteFrame( writer, bgr_frame );
    }

    if(x==20){
        cvShowImage("Example2",bgr_frame);
        cvShowImage ("Example2",img);
        cvWriteFrame( writer, bgr_frame );
        cvWriteFrame( writer , img);
        x=0;
    }
}

cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}
edit retag flag offensive close merge delete

Comments

I searched it but didn't found these answers sorry and thanks for your help

zulfiqar gravatar imagezulfiqar ( 2013-10-26 09:27:58 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
4

answered 2013-10-26 07:18:45 -0600

Haris gravatar image

You can simply do this by creating a big image which contain all your input image, then copy your input images to the appropriate location of the created image using opencv setROI and resetROI and finally display the destination image . The below code assumes that all the images are with same height and width.

And also it is always recommended to use OpenCV C++ API instead of old C interface, for more info see this links, link1, link2

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

}
edit flag offensive delete link more

Comments

i have to show many images in one window is there any method to add scoll bar?

zulfiqar gravatar imagezulfiqar ( 2013-10-26 09:35:24 -0600 )edit

OpenCV doesn't have such a functionality. You might use some other trick for doing that. Or you can try Emgu CV http://www.emgu.com/wiki/index.php/Main_Page which is .Net wrapper to the OpenCV.

Haris gravatar imageHaris ( 2013-10-27 00:45:55 -0600 )edit
0

answered 2014-03-05 21:17:16 -0600

arronlee gravatar image

Thanks for sharing the code. But I wonder why not build a 3rd party web-based image viewer to help display many images in a single window. I am testing about another relevant manual imaging toolkits to help me with it. Do you have any ideas about it? Or any good suggestion? Thanks in advance.And I will try your code later.

Best regards, Arron

edit flag offensive delete link more

Comments

You may need to see Emgu CV which is a cross platform .Net wrapper to the OpenCV image processing library, might be helpful.

Haris gravatar imageHaris ( 2014-03-05 22:30:25 -0600 )edit

Question Tools

Stats

Asked: 2013-10-26 05:51:02 -0600

Seen: 18,522 times

Last updated: Mar 05 '14