Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cvCaptureFromCAM() returns 0.

Hi, I have copied the following link program.

https://www.youtube.com/watch?v=2i2bt-YSlYQ&feature=youtube_gdata_player

It is supposed to at least display on a window what the webcam sees. Unfortunately, it does not,the webcam does not even work and the message "Error: capture is NULL" on the console. as it is supposed to do in the program when cvCaptureFromCAM returns 0 Bellow is the full program.

// Tracker_1.cpp

include<opencv cvaux.h="">

include<opencv highgui.h="">

include<opencv cxcore.h="">

include<stdio.h>

include<stdlib.h>

/////////////////////////////: int main(int argc, char* argv[]) { CvSize size640x480 = cvSize(640, 480); // use a 640 x 480 size for all windows =, also make sure your webcam is set to 640x480 !!

CvCapture* p_capWebcam;  // we will assign our web cam videostream to this later . . .

IplImage* p_imgOriginal;  // pointer to an image structure, this will be the input image from webcam.
IplImage* p_imgProcessed; // pointer to an image structure, this will be the processed image.
                          /* IPL is short for Intel Image Processing Librart, this is the structure used in OpenCV.1.x to work with images */

CvMemStorage* p_strStorage;  // Necessary storage variable to pass into cvHoughCircles()

CvSeq* p_seqCircles;         // Pointer to an OpenCV sequence, will be returned by cvHoughCircles() and will contain all circles.
                             // Call cvGetSeqElem(p_seqCircles, i) will return a 3 element array of the ith circle (see next variable).

float* p_fltXYRadius;        // Pointer to a 3 element array floats.
                             // [0] => x position of detected object.
                             // [1] => y position of detected object.
                             // [2] => Radius of detected object.

int i;                       // Loop counter.
char charCheckForEscKey;     // Char for checking key press (Esc exits program).

p_capWebcam = cvCaptureFromCAM(0);  // 0 => use lst webcam, may have to change to a different number if you have multiple cameras.

if(p_capWebcam == NULL)             // If capture was not successful . . .
{
    printf("Error: capture is NULL \n"); // Error message to standard out . . .
    getchar();                           // getchar() to pause for user see message.
    return(-1);                          // Exit program.
}
// Declare 2 windows.
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);  // Original image from webcam.
cvNamedWindow("Processed", CV_WINDOW_AUTOSIZE); // The processed image we will use dor detectiing circles.

p_imgProcessed = cvCreateImage(size640x480,     // 640 x 480 pixels (CvSize struct from earlier)
                               IPL_DEPTH_8U,    // 8-bit color depth.
                               1);              // 1 channel (grayscale), if this was a color image, use 3

while(1)                                        // For each frame . . .
{
    p_imgOriginal = cvQueryFrame(p_capWebcam);  // Get fram from webcam.

    if(p_imgOriginal == NULL)                   // If frame was not captures succesfully . . .
    {
        printf("Error: frame is NULL \n");      // Error message to std out.
        getchar();
        break;
    }

    cvInRangeS(p_imgOriginal,                   // function input.
               CV_RGB(175, 0, 0),               // min filtering value (if color is greater thar or equal to this)
               CV_RGB(256, 100, 100),           // max filtering value (if color is less than this)
               p_imgProcessed);                 // function output.

    // Smooth the processed image, this will make it easier for the next function to pick out the circles.
    cvSmooth(p_imgProcessed,                    // Function input.
             p_imgProcessed,                    // Function output.
             CV_GAUSSIAN,                       // Use Gaussian filter (average nearby pixels, with the closest pixels weighted more).
             9,                                 // Smoothing filter window width.
             9);                                // Smoothing filter window height.

    // Fill sequential structure with all circles in processed image.
    p_seqCircles = cvHoughCircles(p_imgProcessed,   // Input image, nothe that this has to be grascale (no color).
                                  p_strStorage,     // Provide function with memory storage, makes function return a pointer to a CvSeq.
                                  CV_HOUGH_GRADIENT,// Two-pass algorithm for detecting circles, this is the only choice available.
                                  2,                // Size of image / 2 = "accumulator resolution", i.e. accum = res = size of image / 2.
                                  p_imgProcessed->height / 4,   // Min distance in pixels between the centers of the detected circles.
                                  100,                          // High threshold of Canny edge detector, called by cvHoughCircles.
                                  50,                           // Low threshold of Canny edge detector, called by cvHoughCircles.
                                  10,                           // Min circle radius in pixels.
                                  400);                         // Max circle radius, in pixels.

    for(i=0; i < p_seqCircles->total; i++)          // For each element in sequential circles structure (i.e. for each object detected).
    {
        p_fltXYRadius = (float*)cvGetSeqElem(p_seqCircles, i);  // From the sequential structure, read the ith value into a pointer to a float.

        printf("Ball position x = %f, y = %f, r = %f \n", p_fltXYRadius[0],     // X position of center point of circle.
                                                          p_fltXYRadius[1],     // Y position of center point of circle.
                                                          p_fltXYRadius[2]),        // Radius of circle.

    // Draw a small green circle at center of detected object
    cvCircle(p_imgOriginal,     // Draw on the original image.
             cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])),     // Center point of circle.
             3,     // 3 pixels radius of circle.
             CV_RGB(0, 255, 0),     // Draw pure green.
             CV_FILLED);            // Thickness. Fill in the circle.

        // Draw a red circle arround the detected object.
    cvCircle(p_imgOriginal,         // Draw on the original image.
             cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])),     // Center point of circle.
             cvRound(p_fltXYRadius[2]),     // Radius of circle in pixels.
             CV_RGB(255, 0, 0),             // Draw pure red.
             3);                            // Thickness of circle in pixels.
    } // Enf for.

    cvShowImage("Original", p_imgOriginal);         // Original image with detected ball overlay.
    cvShowImage("Processed", p_imgProcessed);       // Image after processing.

    cvReleaseMemStorage(&p_strStorage);             // Deallocated necessary storage variable to pass into cvHoughCircles.

    charCheckForEscKey = cvWaitKey(10);             // Delay (in ms), and get key press, if any.
    if(charCheckForEscKey == 27) break;             // If Esc key (ASCII 27) was pressed, jump out of while loop.
} // End while.

cvReleaseCapture(&p_capWebcam);                     // Release memory as applicable.

cvDestroyWindow("Original");
cvDestroyWindow("Processed");

return(0);

}

Anyone knows how to force he webcam to activate?

cvCaptureFromCAM() returns 0.

Hi, I have copied the following link program.

https://www.youtube.com/watch?v=2i2bt-YSlYQ&feature=youtube_gdata_player

It is supposed to at least display on a window what the webcam sees. Unfortunately, it does not,the webcam does not even work and the message "Error: capture is NULL" on the console. as it is supposed to do in the program when cvCaptureFromCAM returns 0 Bellow is the full program.

// Tracker_1.cpp

include<opencv cvaux.h="">

include<opencv highgui.h="">

include<opencv cxcore.h="">

include<stdio.h>

include<stdlib.h>

/////////////////////////////: int main(int argc, char* argv[]) { CvSize size640x480 = cvSize(640, 480); // use a 640 x 480 size for all windows =, also make sure your webcam is set to 640x480 !!

CvCapture* p_capWebcam;  // we will assign our web cam videostream to this later . . .

IplImage* p_imgOriginal;  // pointer to an image structure, this will be the input image from webcam.
IplImage* p_imgProcessed; // pointer to an image structure, this will be the processed image.
                          /* IPL is short for Intel Image Processing Librart, this is the structure used in OpenCV.1.x to work with images */

CvMemStorage* p_strStorage;  // Necessary storage variable to pass into cvHoughCircles()

CvSeq* p_seqCircles;         // Pointer to an OpenCV sequence, will be returned by cvHoughCircles() and will contain all circles.
                             // Call cvGetSeqElem(p_seqCircles, i) will return a 3 element array of the ith circle (see next variable).

float* p_fltXYRadius;        // Pointer to a 3 element array floats.
                             // [0] => x position of detected object.
                             // [1] => y position of detected object.
                             // [2] => Radius of detected object.

int i;                       // Loop counter.
char charCheckForEscKey;     // Char for checking key press (Esc exits program).

p_capWebcam = cvCaptureFromCAM(0);  // 0 => use lst webcam, may have to change to a different number if you have multiple cameras.

if(p_capWebcam == NULL)             // If capture was not successful . . .
{
    printf("Error: capture is NULL \n"); // Error message to standard out . . .
    getchar();                           // getchar() to pause for user see message.
    return(-1);                          // Exit program.
}
// Declare 2 windows.
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);  // Original image from webcam.
cvNamedWindow("Processed", CV_WINDOW_AUTOSIZE); // The processed image we will use dor detectiing circles.

p_imgProcessed = cvCreateImage(size640x480,     // 640 x 480 pixels (CvSize struct from earlier)
                               IPL_DEPTH_8U,    // 8-bit color depth.
                               1);              // 1 channel (grayscale), if this was a color image, use 3

while(1)                                        // For each frame . . .
{
    p_imgOriginal = cvQueryFrame(p_capWebcam);  // Get fram from webcam.

    if(p_imgOriginal == NULL)                   // If frame was not captures succesfully . . .
    {
        printf("Error: frame is NULL \n");      // Error message to std out.
        getchar();
        break;
    }

    cvInRangeS(p_imgOriginal,                   // function input.
               CV_RGB(175, 0, 0),               // min filtering value (if color is greater thar or equal to this)
               CV_RGB(256, 100, 100),           // max filtering value (if color is less than this)
               p_imgProcessed);                 // function output.

    // Smooth the processed image, this will make it easier for the next function to pick out the circles.
    cvSmooth(p_imgProcessed,                    // Function input.
             p_imgProcessed,                    // Function output.
             CV_GAUSSIAN,                       // Use Gaussian filter (average nearby pixels, with the closest pixels weighted more).
             9,                                 // Smoothing filter window width.
             9);                                // Smoothing filter window height.

    // Fill sequential structure with all circles in processed image.
    p_seqCircles = cvHoughCircles(p_imgProcessed,   // Input image, nothe that this has to be grascale (no color).
                                  p_strStorage,     // Provide function with memory storage, makes function return a pointer to a CvSeq.
                                  CV_HOUGH_GRADIENT,// Two-pass algorithm for detecting circles, this is the only choice available.
                                  2,                // Size of image / 2 = "accumulator resolution", i.e. accum = res = size of image / 2.
                                  p_imgProcessed->height / 4,   // Min distance in pixels between the centers of the detected circles.
                                  100,                          // High threshold of Canny edge detector, called by cvHoughCircles.
                                  50,                           // Low threshold of Canny edge detector, called by cvHoughCircles.
                                  10,                           // Min circle radius in pixels.
                                  400);                         // Max circle radius, in pixels.

    for(i=0; i < p_seqCircles->total; i++)          // For each element in sequential circles structure (i.e. for each object detected).
    {
        p_fltXYRadius = (float*)cvGetSeqElem(p_seqCircles, i);  // From the sequential structure, read the ith value into a pointer to a float.

        printf("Ball position x = %f, y = %f, r = %f \n", p_fltXYRadius[0],     // X position of center point of circle.
                                                          p_fltXYRadius[1],     // Y position of center point of circle.
                                                          p_fltXYRadius[2]),        // Radius of circle.

    // Draw a small green circle at center of detected object
    cvCircle(p_imgOriginal,     // Draw on the original image.
             cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])),     // Center point of circle.
             3,     // 3 pixels radius of circle.
             CV_RGB(0, 255, 0),     // Draw pure green.
             CV_FILLED);            // Thickness. Fill in the circle.

        // Draw a red circle arround the detected object.
    cvCircle(p_imgOriginal,         // Draw on the original image.
             cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])),     // Center point of circle.
             cvRound(p_fltXYRadius[2]),     // Radius of circle in pixels.
             CV_RGB(255, 0, 0),             // Draw pure red.
             3);                            // Thickness of circle in pixels.
    } // Enf for.

    cvShowImage("Original", p_imgOriginal);         // Original image with detected ball overlay.
    cvShowImage("Processed", p_imgProcessed);       // Image after processing.

    cvReleaseMemStorage(&p_strStorage);             // Deallocated necessary storage variable to pass into cvHoughCircles.

    charCheckForEscKey = cvWaitKey(10);             // Delay (in ms), and get key press, if any.
    if(charCheckForEscKey == 27) break;             // If Esc key (ASCII 27) was pressed, jump out of while loop.
} // End while.

cvReleaseCapture(&p_capWebcam);                     // Release memory as applicable.

cvDestroyWindow("Original");
cvDestroyWindow("Processed");

return(0);

}

Anyone knows how to force he webcam to activate? activate?

Thank you by advance.

click to hide/show revision 3
retagged

updated 2013-11-25 09:10:15 -0600

berak gravatar image

cvCaptureFromCAM() returns 0.

Hi, I have copied the following link program.

https://www.youtube.com/watch?v=2i2bt-YSlYQ&feature=youtube_gdata_player

It is supposed to at least display on a window what the webcam sees. Unfortunately, it does not,the webcam does not even work and the message "Error: capture is NULL" on the console. as it is supposed to do in the program when cvCaptureFromCAM returns 0 Bellow is the full program.

// Tracker_1.cpp

include<opencv cvaux.h="">

include<opencv highgui.h="">

include<opencv cxcore.h="">

include<stdio.h>

include<stdlib.h>

/////////////////////////////: int main(int argc, char* argv[]) { CvSize size640x480 = cvSize(640, 480); // use a 640 x 480 size for all windows =, also make sure your webcam is set to 640x480 !!

CvCapture* p_capWebcam;  // we will assign our web cam videostream to this later . . .

IplImage* p_imgOriginal;  // pointer to an image structure, this will be the input image from webcam.
IplImage* p_imgProcessed; // pointer to an image structure, this will be the processed image.
                          /* IPL is short for Intel Image Processing Librart, this is the structure used in OpenCV.1.x to work with images */

CvMemStorage* p_strStorage;  // Necessary storage variable to pass into cvHoughCircles()

CvSeq* p_seqCircles;         // Pointer to an OpenCV sequence, will be returned by cvHoughCircles() and will contain all circles.
                             // Call cvGetSeqElem(p_seqCircles, i) will return a 3 element array of the ith circle (see next variable).

float* p_fltXYRadius;        // Pointer to a 3 element array floats.
                             // [0] => x position of detected object.
                             // [1] => y position of detected object.
                             // [2] => Radius of detected object.

int i;                       // Loop counter.
char charCheckForEscKey;     // Char for checking key press (Esc exits program).

p_capWebcam = cvCaptureFromCAM(0);  // 0 => use lst webcam, may have to change to a different number if you have multiple cameras.

if(p_capWebcam == NULL)             // If capture was not successful . . .
{
    printf("Error: capture is NULL \n"); // Error message to standard out . . .
    getchar();                           // getchar() to pause for user see message.
    return(-1);                          // Exit program.
}
// Declare 2 windows.
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);  // Original image from webcam.
cvNamedWindow("Processed", CV_WINDOW_AUTOSIZE); // The processed image we will use dor detectiing circles.

p_imgProcessed = cvCreateImage(size640x480,     // 640 x 480 pixels (CvSize struct from earlier)
                               IPL_DEPTH_8U,    // 8-bit color depth.
                               1);              // 1 channel (grayscale), if this was a color image, use 3

while(1)                                        // For each frame . . .
{
    p_imgOriginal = cvQueryFrame(p_capWebcam);  // Get fram from webcam.

    if(p_imgOriginal == NULL)                   // If frame was not captures succesfully . . .
    {
        printf("Error: frame is NULL \n");      // Error message to std out.
        getchar();
        break;
    }

    cvInRangeS(p_imgOriginal,                   // function input.
               CV_RGB(175, 0, 0),               // min filtering value (if color is greater thar or equal to this)
               CV_RGB(256, 100, 100),           // max filtering value (if color is less than this)
               p_imgProcessed);                 // function output.

    // Smooth the processed image, this will make it easier for the next function to pick out the circles.
    cvSmooth(p_imgProcessed,                    // Function input.
             p_imgProcessed,                    // Function output.
             CV_GAUSSIAN,                       // Use Gaussian filter (average nearby pixels, with the closest pixels weighted more).
             9,                                 // Smoothing filter window width.
             9);                                // Smoothing filter window height.

    // Fill sequential structure with all circles in processed image.
    p_seqCircles = cvHoughCircles(p_imgProcessed,   // Input image, nothe that this has to be grascale (no color).
                                  p_strStorage,     // Provide function with memory storage, makes function return a pointer to a CvSeq.
                                  CV_HOUGH_GRADIENT,// Two-pass algorithm for detecting circles, this is the only choice available.
                                  2,                // Size of image / 2 = "accumulator resolution", i.e. accum = res = size of image / 2.
                                  p_imgProcessed->height / 4,   // Min distance in pixels between the centers of the detected circles.
                                  100,                          // High threshold of Canny edge detector, called by cvHoughCircles.
                                  50,                           // Low threshold of Canny edge detector, called by cvHoughCircles.
                                  10,                           // Min circle radius in pixels.
                                  400);                         // Max circle radius, in pixels.

    for(i=0; i < p_seqCircles->total; i++)          // For each element in sequential circles structure (i.e. for each object detected).
    {
        p_fltXYRadius = (float*)cvGetSeqElem(p_seqCircles, i);  // From the sequential structure, read the ith value into a pointer to a float.

        printf("Ball position x = %f, y = %f, r = %f \n", p_fltXYRadius[0],     // X position of center point of circle.
                                                          p_fltXYRadius[1],     // Y position of center point of circle.
                                                          p_fltXYRadius[2]),        // Radius of circle.

    // Draw a small green circle at center of detected object
    cvCircle(p_imgOriginal,     // Draw on the original image.
             cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])),     // Center point of circle.
             3,     // 3 pixels radius of circle.
             CV_RGB(0, 255, 0),     // Draw pure green.
             CV_FILLED);            // Thickness. Fill in the circle.

        // Draw a red circle arround the detected object.
    cvCircle(p_imgOriginal,         // Draw on the original image.
             cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])),     // Center point of circle.
             cvRound(p_fltXYRadius[2]),     // Radius of circle in pixels.
             CV_RGB(255, 0, 0),             // Draw pure red.
             3);                            // Thickness of circle in pixels.
    } // Enf for.

    cvShowImage("Original", p_imgOriginal);         // Original image with detected ball overlay.
    cvShowImage("Processed", p_imgProcessed);       // Image after processing.

    cvReleaseMemStorage(&p_strStorage);             // Deallocated necessary storage variable to pass into cvHoughCircles.

    charCheckForEscKey = cvWaitKey(10);             // Delay (in ms), and get key press, if any.
    if(charCheckForEscKey == 27) break;             // If Esc key (ASCII 27) was pressed, jump out of while loop.
} // End while.

cvReleaseCapture(&p_capWebcam);                     // Release memory as applicable.

cvDestroyWindow("Original");
cvDestroyWindow("Processed");

return(0);

}

Anyone knows how to force he webcam to activate?

Thank you by advance.