使用opencv編寫的刪除面積過小的輪廓的函數

來源:互聯網
上載者:User

最下面的代碼是我未畢業的時候寫的,沒有錯,但是Cvseq不能傳遞到其他函數,在http://www.opencv.org.cn/forum/viewtopic.php?t=8300的代碼的協助下寫了如下的代碼,下面的代碼的執行效率更高。

int Contours::findContours(){this->firstContour = NULL;//全域變數。numCon = 0;//輪廓的個數CvContourScanner scanner = cvStartFindContours(this->imgBinary,this->storage);//參看學習opencvCvSeq * seq =NULL;double indexArea;while( (seq = cvFindNextContour(scanner) ) != NULL)//開始尋找{indexArea = fabs(cvContourArea(seq)/(imgBinary->width * imgBinary->height) );if(indexArea >this->thresholdLow  && indexArea <= this->thresholdHigh)//如果尋找出的輪廓的面積大於low,並且小於high,則留下,反之,則刪除。{//符合留下的條件。numCon = numCon+1;}else{cvSubstituteContour(scanner,NULL);//刪除當前的輪廓}}this->firstContour = cvEndFindContours(&scanner);//把找到的輪廓返回到firstContour中。return this->numCon;}

半年前寫的刪除過小輪廓的代碼。;;;;

int removeNoise(IplImage *img ){IplImage *img_contour = cvCreateImage(cvGetSize(img),8,1);IplImage *img_contour_3 = cvCreateImage(cvGetSize(img),8,3);cvCopy(img,img_contour);cvCvtColor(img,img_contour_3,CV_GRAY2BGR);CvMemStorage *storage= cvCreateMemStorage();CvSeq * first_contour = NULL;int Nc = cvFindContours(img_contour,storage,&first_contour,sizeof(CvContour),CV_RETR_LIST);int num_contours = 0;struct min_max_list *head = NULL;double area = 0;double areaOfImg = img->width *img->height;CvSeq *cPrev = first_contour;CvSeq *cNext = NULL;bool first= true;for(CvSeq *c = first_contour;c!= NULL;c= cNext){area = fabs(cvContourArea(c) );if(area <areaOfImg *0.01){cNext = c->h_next;cvClearSeq(c);//cvClearMemStorage(c->storage);//回收記憶體continue;}//ifelse{if(first)first_contour = c;first = false;cvDrawContours(img_contour_3,c,COLOR_GREEN,COLOR_GREEN,0,2,8);}cNext= c->h_next;}//for first_constours;cvShowImage("removeNoise",img);cvShowImage("33333",img_contour_3);cvWaitKey();//釋放記憶體。cvReleaseImage(&img_contour_3);cvReleaseImage(&img_contour);cvReleaseMemStorage(&storage);return 0;}

從下面的可以看到小的輪廓沒有被綠線框住,基本上符合我的想法。我通過一個死迴圈來檢驗有無記憶體泄露,發現記憶體使用量量基本上不增長。我想應該沒有記憶體泄露吧,大牛們還請多指點這個程式有什麼錯誤啊。小弟謝謝啦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.