c++ - Circle and hand detection in OpenCV -
beginner here. i'm trying detect circle , hand, , draw circle around circle , rectangle around hand, , display both in same image. when run program memory error, can please help?
below code:
#include "opencv/cv.h" #include "opencv2\highgui\highgui.hpp" #include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <conio.h> using namespace std; //declarations iplimage* img = 0; cvhaarclassifiercascade *cascade; cvmemstorage *cstorage; cvmemstorage *hstorage; void detectobjects( iplimage *img ); int key; int main( int argc, char** argv ) { cvcapture *capture; iplimage *frame; // loads classifier hand haar cascade char *filename = "haarcascade_hand.xml"; cascade = ( cvhaarclassifiercascade* )cvload( "haarcascade_hand.xml", 0, 0, 0 ); // setup memory buffer hstorage = cvcreatememstorage( 0 ); cstorage = cvcreatememstorage( 0 ); // initialize camera capture = cvcapturefromcam( 0 ); // check //assert( cascade && storage && capture ); // create window cvnamedwindow( "camera", 1 ); while(key!='q') { // captures frame , check every frame frame = cvqueryframe( capture ); if( !frame ) break; // detect objects , display video detectobjects (frame ); // quit if user press 'q' key = cvwaitkey( 10 ); } // free memory cvreleasecapture( &capture ); cvdestroyallwindows(); cvreleasehaarclassifiercascade( &cascade ); cvreleasememstorage( &cstorage ); cvreleasememstorage( &hstorage ); return 0; } void detectobjects( iplimage *img ) { int px; int py; int edge_thresh = 1; iplimage *gray = cvcreateimage( cvsize(640,480), 8, 1 ); iplimage *edge = cvcreateimage( cvsize(640,480), 8, 1 ); // convert video image color cvcvtcolor(img,gray,cv_bgr2gray); // set converted image's origin gray->origin=1; // color threshold cvthreshold(gray,gray,100,255,cv_thresh_binary); // smooths out image cvsmooth(gray, gray, cv_gaussian, 11, 11); // edges cvcanny(gray, edge, (float)edge_thresh, (float)edge_thresh*3, 5); // detects circle cvseq* circle = cvhoughcircles(gray, cstorage, cv_hough_gradient, 1, gray->height/50, 5, 35); // draws circle , centerpoint float* p = (float*)cvgetseqelem( circle, 0 ); cvcircle( img, cvpoint(cvround(p[0]),cvround(p[1])), 3, cv_rgb(255,0,0), -1, 8, 0 ); cvcircle( img, cvpoint(cvround(p[0]),cvround(p[1])), cvround(p[2]), cv_rgb(200,0,0), 1, 8, 0 ); px=cvround(p[0]); py=cvround(p[1]); // displays coordinates of circle's center cout <<"(x,y) -> ("<<px<<","<<py<<")"<<endl; // detects hand cvseq *hand = cvhaardetectobjects(img, cascade, hstorage, 1.2, 2, cv_haar_do_canny_pruning, cvsize(100, 100)); // draws red box around hand when detected cvrect *r = ( cvrect* )cvgetseqelem( hand, 0 ); cvrectangle( img, cvpoint( r->x, r->y ), cvpoint( r->x + r->width, r->y + r->height ), cv_rgb( 255, 0, 0 ), 1, 8, 0 ); cvshowimage("camera",img); }
the issue size of gray scale image created should same of image obtained camera.
instead of:
iplimage *gray = cvcreateimage( cvsize(640,480), 8, 1 );
write as:
iplimage *gray = cvcreateimage( cvsize(img->width,img->height), 8, 1);
Comments
Post a Comment