c – OpenCV show both incoming video and modified video in separate windows – Education Career Blog

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


    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++){
            gpx = (p3*x + p3*x+1 + p3*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?


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.


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.

Leave a Comment