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