Via OpenCv Snake algorithm

Source: Internet
Author: User

/** Code function: manually select the initial edge, using the cvsnakeimage algorithm iterative optimization * modified: jink2005 2009-11-18* forum:Http://www.aiseminar.cn/bbs*/#include"cv.h"#include"highgui.h"#include<iostream>#include<vector>#include<opencv2/legacy/legacy.hpp>//#pragma comment (lib, "highgui.lib")//#pragma comment (lib, "cv.lib")//#pragma comment (lib, "cvaux.lib")//#pragma comment (lib, "cxcore.lib")Std::vector<cvpoint>Initcontour;iplimage* temp;//= cvcreateimage (cvgetsize (img), ipl_depth_8u, 1);/** function to draw contour lines using straight lines to connect contour points*/voidShowcontent (iplimage *Img) {    if(temp = =NULL) Temp= Cvcreateimage (cvgetsize (img), ipl_depth_8u,1);    Cvcopyimage (img, temp);  for(inti =0; I < initcontour.size (); i++) {cvcircle (temp, initcontour[i],2, Cvscalarall (155)); if(i >0) cvline (temp, initcontour[i-1], initcontour[i], Cvscalarall ( -),1); } cvshowimage ("Srcimage", temp);}voidOn_mouse (int Event,intXintYintFlagsvoid*Ptr) {    if(Event==Cv_event_lbuttondown)        {initcontour.push_back (cvpoint (x, y)); /*Initcontour.push_back (cvpoint (130,130));        For (int i=130;i<660;i=i+10) {initcontour.push_back (cvpoint (i,130));        } for (int i=130;i<660;i=i+20) {initcontour.push_back (cvpoint (660,i));        } for (int i=660;i>130;i=i-10) {initcontour.push_back (cvpoint (i,660));        } for (int i=660;i>130;i=i-20) {initcontour.push_back (cvpoint (130,i)); } initcontour.push_back (cvpoint (130,130));*/showcontent ((iplimage*) ptr); }}intMainintargcChar*Argv[]) {iplimage* Srcimage =NULL; if(ARGC = =2&& (srcimage = Cvloadimage (Char*) argv[1], cv_load_image_grayscale)! =0)        ; Else //load a picture in the working directory with the file name Apple.jpg. {srcimage= Cvloadimage ("test1.jpg", cv_load_image_grayscale); }    if(srcimage = =NULL) {std::cout<<"Can ' t find the image file!"<<std::endl; return-1;    } initcontour.clear (); Cvnamedwindow ("Srcimage"); Cvshowimage ("Srcimage", srcimage); Cvsetmousecallback ("Srcimage", on_mouse, srcimage); Charc;  while(Charc = Cvwaitkey (0))        if(c = ='s'|| c = ='S')             break; //set parameters used by the snake algorithm    floatAlpha =1.0; floatBeta =0.5; floatGamma =1.0;    Cvsize size; Size.width=3; Size.Height=3;    Cvtermcriteria criteria; Criteria.type=cv_termcrit_iter; Criteria.max_iter= -; Criteria.epsilon=0.1; intItetime = -;  for(intITE =0; Ite < itetime; Ite++)//cvsnakeimage own cycle, Why do you want it here? Manually control the number of cycles! {cvpoint* pts =Newcvpoint[initcontour.size ()];  for(inti =0; I < initcontour.size (); i++) {pts[i]=initcontour[i]; }        //using the snake algorithm to modify the contourCvsnakeimage (srcimage, pts, initcontour.size (), &alpha, &beta, &gamma, cv_value, size, criteria,1); intSize =initcontour.size (); //clears the original outline and updates to the new contour pointInitcontour.clear ();  for(inti =0; I < size; i++) {initcontour.push_back (pts[i]); //linear interpolation of contour points, insertion of intermediate points            intNext = (i +1) %size; Cvpoint NE=pts[next]; if(size < -) {cvpoint Mid= Cvpoint ((pts[i].x + ne.x)/2, (pts[i].y + ne.y)/2);            Initcontour.push_back (mid); }        }        Delete[]pts;        Showcontent (srcimage); Cvwaitkey (); //manual control increases the number of iterations, pressing any key} showcontent (srcimage);    Cvwaitkey (); return 0;}
Snake

The left mouse button to draw a good outline, the S key control contour contraction

Via OpenCv Snake algorithm

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.