【OpenCV學習】橢圓擬合

來源:互聯網
上載者:User

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

 

#include "cv.h"#include "highgui.h"int slider_pos=70;//閾值IplImage *image02 =0,*image03 = 0,*image04 = 0;void process_image(int h);int main(int argc ,char **argv){    const char *filename ="rice.png";    if ((image03 = cvLoadImage(filename,0))==0)//讀入映像為灰階映像    {        return -1;    }    image02 = cvCloneImage(image03);    image04 = cvCloneImage(image03);        cvNamedWindow("Source",1);    cvNamedWindow("Result",1);    cvShowImage("Source",image03);    cvCreateTrackbar("Threshold","Result",&slider_pos,255,process_image);    process_image(0);    cvWaitKey(0);    cvSaveImage("1.jpg",image04);    cvReleaseImage(&image02);    cvReleaseImage(&image03);    cvDestroyWindow("Source");    cvDestroyWindow("Result");    return 0;}//這個函數尋找出輪廓、用橢圓擬合畫出void process_image(int h){    CvMemStorage *stor;    CvSeq *cont;    CvBox2D32f *box;    CvPoint *PointArray;    CvPoint2D32f *PointArray2D32f;    stor = cvCreateMemStorage(0);    cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT,sizeof(CvSeq),sizeof(CvPoint),stor);    cvThreshold(image03,image02,slider_pos,255,CV_THRESH_BINARY);    cvFindContours(image02,stor,&cont,sizeof(CvContour),        CV_RETR_LIST,CV_CHAIN_APPROX_NONE,cvPoint(0,0));    cvZero(image02);    cvZero(image04);    //繪製所有輪廓並用橢圓擬合    for (;cont;cont = cont ->h_next)    {        int i;        int count= cont->total;//輪廓個數        CvPoint center;        CvSize size;        /*個數必須大於6,這是cvFitEllipse_32f的要求*/        if (count<6)        {            continue;        }            //分配記憶體給點集        PointArray = (CvPoint *)malloc(count*sizeof(CvPoint));        PointArray2D32f = (CvPoint2D32f*)malloc(count*sizeof(CvPoint2D32f));            //分配記憶體給橢圓資料        box = (CvBox2D32f *)malloc(sizeof(CvBox2D32f));        //得到點集(這個方法值得借鑒)        cvCvtSeqToArray(cont,PointArray,CV_WHOLE_SEQ);            //將CvPoint點集轉化為CvBox2D32f集合        for (i=0;i<count;i++)        {            PointArray2D32f[i].x=(float)PointArray[i].x;            PointArray2D32f[i].y=(float)PointArray[i].y;        }        //擬合當前輪廓        cvFitEllipse(PointArray2D32f,count,box);        //繪製當前輪廓        cvDrawContours(image04,cont,CV_RGB(255,255,255),CV_RGB(255,255,255),            0,1,8,cvPoint(0,0));        //將橢圓資料從浮點轉化為整數表示        center.x = cvRound(box->center.x);        center.y = cvRound(box->center.y);        size.width = cvRound(box->size.width*0.5);        size.height = cvRound(box->size.height*0.5);        box->angle = -box->angle;        //畫橢圓        cvEllipse(image04,center,size,box->angle,0,360,CV_RGB(0,0,255),1,CV_AA,0);        free(PointArray);        free(PointArray2D32f);        free(box);    }    cvShowImage("Result",image04);}

作者:gnuhpc

出處:http://www.cnblogs.com/gnuhpc/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.