cphyc
Posts: 1
Joined: Mon Apr 15, 2013 3:00 pm

Impossible to grab frames from a camera

Mon Apr 15, 2013 3:12 pm

Hello,
I'm currently trying to grab frames from a webcam (a cheap one, so I don't have any name or model). The fact is that it seems quite buggy, and I can't make it work for long
I had issues while grabbing, so I followed the instructions there : http://www.raspberrypi.org/phpBB3/viewt ... 9&p=314596. Now, the code doesn't return any error, but after some steps (usually 0~5), it just stops.

Do you have any idea ?

Code: Select all

//OpenCV Headers
#include <cv.h>
#include <highgui.h>
//Input-Output
#include <stdio.h>
//Blob Library Headers
#include <cvblob.h>
//Definitions
#define h 240
#define w 320
#define SCREEN_X 1600
#define SCREEN_Y 900
//NameSpaces
using namespace cvb;
using namespace std;
int main()
{
	//Structure to get feed from CAM
	CvCapture* capture 	=		cvCreateCameraCapture ( 0 );
	//Structure to hold blobs
	CvBlobs blobsR;
	//Windows
	cvNamedWindow ( "Live", CV_WINDOW_AUTOSIZE );
	//Image Variables
	IplImage *frame			=		cvCreateImage ( cvSize(w,h), 8, 3);   //Original Image
	IplImage *hsvframe	=		cvCreateImage ( cvSize(w,h), 8, 3);//Image in HSV color space
	IplImage *labelImgR	=		cvCreateImage ( cvSize(w,h), IPL_DEPTH_LABEL, 1);//I//Image Variable for blobs
	IplImage *threshyR	=		cvCreateImage ( cvSize(w,h), 8, 1); //Threshold image of yellow color
	 
	while(1)
	{
		//Getting the current frame
		IplImage *fram = cvQueryFrame ( capture );
		//If failed to get break the loop
		if(!fram)
			break;
		//Resizing the capture
		cvResize (fram, frame, CV_INTER_LINEAR );
		//Flipping the frame
		cvFlip ( frame, frame, 1 );
		//Changing the color space
		cvCvtColor ( frame, hsvframe, CV_BGR2HSV );
		//Thresholding the frame 
		cvInRangeS ( hsvframe, cvScalar ( 160, 150, 150 ), cvScalar ( 170, 250, 300), threshyR );
		//Filtering the frame
		cvSmooth ( threshyR, threshyR, CV_MEDIAN, 7, 7 );
		//Finding the blobs
		unsigned int resultR = cvLabel ( threshyR, labelImgR, blobsR );
		//Printing the output
		cout << "Rouge : " << resultR << endl;
		//Rendering the blobs
		cvRenderBlobs (labelImgR, blobsR, frame, frame );
		//Filtering the blobs
		cvFilterByArea ( blobsR, 60, 500 );

		for ( CvBlobs::const_iterator it=blobsR.begin() ; it!=blobsR.end(); ++it )
		{
			
			double moment10 = it->second->m10;
			double moment01 = it->second->m01;
			double area 		= it->second->area;
			//Variable for holding position
			int x1;
			int y1;
			//Calculating the current position
			x1 = moment10/area;
			y1 = moment01/area;
			//Mapping to the screen coordinates
			int x	=	(int)( x1*SCREEN_X/w );
			int y	=	(int)( y1*SCREEN_Y/h );
			//Printing the position information
			cout << "Red:\t" << "X: " << x << "\tY:" << y << endl;
		}
		//Showing the images
		cvShowImage ( "Live", frame );
		//Escape Sequence
		char c = cvWaitKey ( 33 );
		if( c == 27 )
		break;
	}
	//Cleanup
	cvReleaseCapture ( &capture );
	cvDestroyAllWindows ();
}

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Impossible to grab frames from a camera

Mon Apr 15, 2013 5:11 pm

I used this code and it worked fine...
http://opencv.willowgarage.com/wiki/CameraCapture

Regards.
Dave.
Apple say... Monkey do !!

Return to “C/C++”