OpenCV. Webcal does not work. RRS feed

  • Question

  • Hi,

    I am new to OpenCV and I entirely copied a code from a ideo online.

    It is supposed to activate the webcam and draw a virtual circle arroung red real circles. It is also supposed to create a processed image.

    The problem is that the webcam does not work and the following message appears "Error: capture is NULL". It is not what I want. It corresponds to the next coding.

    // Tracker_1.cpp
    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.
    		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.

    I have no idea where the problem comes from. Doe anyone have an idea?

    PS: I am not using a kinect. Just my computer's webcam. Does it matter? If so, where should I post it instead?

    Thank you by advance.

    • Edited by Cedric100 Monday, November 25, 2013 2:47 PM
    Monday, November 25, 2013 2:46 PM