tags:

views:

58

answers:

1

I am applying common image transforms to my live webcam capture. I want to display the original webcam in one window and the image with the transforms applied to in another window. However, I am getting same image (filtered) on both windows, I am wondering if I am limited by the OpenCV API or if I am missing something? My code snippet looks like -

/* allocate resources */
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Filtered", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCaptureFromCAM(0);

do {    
    IplImage* img = cvQueryFrame(capture);
    cvShowImage("Original", img);           

    Filters* filters = new Filters(img);
    IplImage* dst = filters->doSobel();
    cvShowImage("Filtered", dst);

    cvWaitKey(10);      
} while (1);

/* deallocate resources */
cvDestroyWindow("Original");
cvDestroyWindow("Filtered");
cvReleaseCapture(&capture);
A: 

Its possible! Try copying img to another IplImage before sending it to processing and see if that works first.

Yes, I know what you're going to say. But just try that first and see if it does what you want. The code below is just to illustrate what you should do, I don't know if it will work:

/* allocate resources */
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Filtered", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCaptureFromCAM(0);    

do {    
    IplImage* img = cvQueryFrame(capture);
    cvShowImage("Original", img);           

    IplImage* img_cpy = cvCreateImage(cvGetSize(img), 8, 3);
    img_cpy = cvCloneImage(img);

    Filters* filters = new Filters(img_cpy);
    IplImage* dst = filters->doSobel();
    cvShowImage("Filtered", dst);

    /* Be aware that if you release img_cpy here it might not display 
     * the data on the window. On the other hand, not doing it now will
     * cause a memory leak.
     */
    //cvReleaseImage( &img_cpy );  

    cvWaitKey(10);      
} while (1);

/* deallocate resources */
cvDestroyWindow("Original");
cvDestroyWindow("Filtered");
cvReleaseCapture(&capture);
karlphillip
aargh! doesn't work, on second thought, I notice the last cvShowImage() is what is shown on both windows, so if I move cvShowImage("Original") below cvShowImage("Filtered"), I start getting Original on both windows, and Filtered disappears!
Vaibhav Bajpai
There was a bug in the code, I fixed it. I'll take a look at this later. I already done this in the past, maybe not exactly like this but I know its possible to do so.
karlphillip
okay, it worked! actually I had passed the copy to the Filters constructor myself, but I was not doing cvCreateImage(), I wonder why do I need it, wouldn't cvCloneImage() allocate new storage while cloning and return pointer to new location already?
Vaibhav Bajpai
I'm not sure. Anyway, don't forget to vote up my answer if it helped you or accept it as the official answer if it solved your question.
karlphillip