[Opencv learning] elliptic fitting

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

 

# Include "CV. H "# include" highgui. H "int slider_pos = 70; // threshold 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) // read the grayscale image {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;} // This function is used to find the contour and draw void process_image (INT h) {cvmemstorage * stor; cvseq * cont by fitting the elliptic form; cvbox2d32f * box; cvpoint * pointarray; cvpoint2d32f * empty; stor = cvcreatememstorage (0); cont = cvcreateseq (sequence, sizeof (cvseq), sizeof (cvpoint), STOR ); cvthreshold (image03, image02, slider_pos, 255, rows); cvfindcontours (image02, STOR, & Cont, sizeof (cvcontour), cv_retr_list, cursor, cvpoint )); cvzero (image02); cvzero (image04); // draw all outlines and use an elliptic fit for (; cont = cont-> h_next) {int I; int COUNT = cont-> total; // Number of outlines cvpoint center; cvsize size;/* number must be greater than 6, which is required by cvfitellipse_32f */If (count <6) {continue;} // allocate memory to the point set pointarray = (cvpoint *) malloc (count * sizeof (cvpoint); pointarray2d32f = (cvpoint2d32f *) malloc (count * sizeof (cvpoint2d32f); // allocate the memory to the oval Data box = (cvbox2d32f *) malloc (sizeof (cvbox2d32f )); // obtain the Point Set (this method is worth learning) cvcvtseqtoarray (cont, pointarray, cv_whole_seq); // convert the cvpoint point set to the cvbox2d32f set for (I = 0; I <count; I ++) {pointarray2d32f [I]. X = (float) pointarray [I]. x; pointarray2d32f [I]. y = (float) pointarray [I]. y;} // fit the current profile cvfitellipse (pointarray2d32f, Count, box); // draw the current profile cvdrawcontours (image04, cont, cv_rgb (255,255,255), cv_rgb (255,255,255, 8, cvpoint (); // converts the elliptical data from a floating point to an integer to represent the 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; // draw an oval cvellipse (image04, center, size, box-> angle, 0,360, cv_rgb, 255), 1, cv_aa, 0); free (pointarray); free (pointarray2d32f); free (box);} cvshowimage ("result", image04 );}

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.