tags:

views:

41

answers:

2

Hi. This should be easy. I have a video stream coming in from my webcam. I'm just playing with image transformation etc. I'd like to be able to view the original images (video input) in one window and the transformed video in another. Problem is, as soon as I start capturing video instead of just single images, the original video window displays transformed video. I don't understand why.

cvNamedWindow("in", CV_WINDOW_AUTOSIZE);
cvNamedWindow("out", CV_WINDOW_AUTOSIZE);

CvCapture *fc = cvCaptureFromCAM(0);

IplImage* frame = cvQueryFrame(fc);

if (!frame) {
    return 0;
}

IplImage* greyscale = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
IplImage* output = cvCreateImage(cvGetSize(frame),IPL_DEPTH_32F , 1);

while(1){

    frame= cvQueryFrame(fc);
    cvShowImage("in", frame);

    // manually convert to greyscale
    for (int y = 0; y < frame->height; y++) {
        uchar* p = (uchar*) frame->imageData + y* frame->widthStep; // pointer to row
        uchar* gp = (uchar*) greyscale->imageData + y*greyscale->widthStep;  
        for(int x = 0; x < frame->width; x++){
            gp[x] = (p[3*x] + p[3*x+1] + p[3*x+2])/3;   // average RGB values 
        }
    }

    cvShowImage("out", greyscale);

    char c = cvWaitKey(33);
    if (c == 27) {
        return 0;
    }
}

In this simple example, both video streams end up appearing greyscale... The pointer values and imagedata for frame and greyscale are totally different. If I stop showing greyscale in the "out" window, then frame will appear in color.

Also, if I continue and apply a Sobel operation on the greyscale image and display the result in "out", both "in" and "out" windows will show the Sobel image!

Any ideas?

+1  A: 

Hmm This was weird, but it seems using CV_WINDOW_AUTOSIZE was the problem? Perhaps it's not supported in OpenCV 2.1 (which I'm pretty sure is what I'm running). Anyways, using 0 instead of CV_WINDOW_AUTOSIZE when creating the windows works fine.

wallacer
@wallacer Ok, don't forget to accept your own answer when you can.
karlphillip
A: 

I have tried your code with openCV 2.0 under mandriva 2010 and it is working fine either with CV_WINDOW_AUTOSIZE or 0.

You may try to convert to grayscale with cvCvtColor(frame,grayscale,CV_RGB2GRAY) and see if the problem persist.

rics
hmm I'm running openCV 2.1. There were more errors than this caused by Window autosize... In some cases the second window would display half correct, and half a garbled view of the first window. Either way, using 0 made it all go away. Very strange.
wallacer