1. Make your image to Grayscale.
2. Convert Grayscale to data biner (threshold).
3. Filter from noise.
4. Labeling (can do with the other technique from processing image).
5. Get the center point (also you must use a pointer memory to save the data).
6. Give a name.
Ok... Let see How I labeling with cvCountours library on Opencv
in this state i use bottle cap ....
/* ROBOT MOBILE */ #include "cv.h" #include "cxcore.h" #include "highgui.h" using namespace std; using namespace cv; int main(int argc, char* argv[]) { int den; int nc,n; CvCapture* capture = cvCaptureFromCAM(0); IplImage* frame; frame = cvQueryFrame(capture); //frame = cvLoadImage("Botol.jpg"); CvSize sz = cvSize( frame->width & -2, frame->height & -2 ); den = 1; IplImage* sg = cvCreateImage( cvSize(sz.width/den, sz.height/den), 8, 3 ); IplImage* grey = cvCreateImage( cvSize(sz.width/den, sz.height/den), 8, 1 ); IplImage* thr = cvCreateImage(cvSize(sz.width/den, sz.height/den), IPL_DEPTH_8U, 1); IplImage* d1 = cvCreateImage( cvSize(sz.width/den, sz.height/den), 8, 1 ); IplImage* d2 = cvCreateImage( cvSize(sz.width/den, sz.height/den), 8, 1 ); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contours = 0; /* Setting cvFindContours */ CvScalar red = CV_RGB(250,0,0); CvScalar blue = CV_RGB(0,250,0); CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments)); CvPoint center; /////////////////////////////////////////////////////////////////// //MEMORY //save data to memory pointer struct datasementara { ///////// ///////// ////// CvPoint cnt[1000]; /// /// // // CvPoint lastcnt[1000]; /// /// /// CvPoint savecnt[1000]; /// /// /// /// /// // // }; ///////// /// ////// //deklarasi variabel struktur datasementara* IT = NULL; IT = new datasementara; /////////////////////////////////////////////////////////////////// /* Inisialiasi font */ CvFont font; double hScale= 1; double vScale= 1; int lineWidth=2; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth); while(cvWaitKey(1) != 'q') { frame = cvQueryFrame(capture); //frame = cvLoadImage("Botol.jpg"); if( !frame ) break; cvResize(frame,sg); cvCvtColor(sg,grey,CV_RGB2GRAY); cvClearMemStorage(storage); cvAdaptiveThreshold(grey, thr,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,71,5); cvNot(thr,d1); cvErode(d1,d1,NULL,15); cvSmooth(d1,d1,2,9); cvDilate(d1,d1,NULL,12); cvCopy(d1,d2); //buat clonning gambar /* deteksi botol */ nc = cvFindContours(d2, storage, &contours,sizeof(CvContour),CV_RETR_EXTERNAL); printf("nc = %d\n",nc); n = 0; for( CvSeq* c=contours; c!=NULL; c=c->h_next ) { cvDrawContours(sg,c,red,blue,0,2,8); //printf("Contour #%d\n", n ); //printf(" %d elements:\n", c->total ); cvMoments( c, moments, 0 ); double m00 = cvGetSpatialMoment( moments, 0, 0) ; double m10 = cvGetSpatialMoment( moments, 1, 0) ; double m01 = cvGetSpatialMoment( moments, 0, 1); center.x = (float)(m10 / m00); center.y = (float)(m01 / m00); (*IT).cnt[n].x = center.x; (*IT).cnt[n].y = center.y; //printf(" (%d,%d)\n", center.x, center.y ); /*for( int i=0; i<c->total; ++i ) { CvPoint* p = CV_GET_SEQ_ELEM( CvPoint, c, i ); printf(" (%d,%d)\n", p->x, p->y ); }*/ n++; }//end deteksi center for( int i = 0; i < n; i++ ){ cvCircle(sg,(*IT).cnt[i],2,cvScalar(0,255,0),2); /* Tampilkan font */ char jumlah[200]; char labelRobotfont[200]; itoa (abs(i),jumlah,10); cvPutText (sg,jumlah,cvPoint( (*IT).cnt[i].x, (*IT).cnt[i].y), &font, cvScalar(0,255,0)); } cvShowImage("original", sg); cvShowImage("result", d1); cvShowImage("thr", thr); } cvDestroyAllWindows(); // close your console windows cvReleaseImage(&frame); return 0; }
And its the one of my project that I use labeling to detection mobile robot.
When you have done about labeling, you can get more information. For example : center, direction, tracking, degree, distance and etc.
0 comments:
Post a Comment