In-page index
Topic Requirements |
Program code |
Result picture |
Homilies Miao DAO |
Learn from Reference |
Title Requirements:
A. Detecting contours and calculating contour Lengths
b, using 1/90,1/66,1/11,1/10 as the accuracy parameter, using Cvapproxpoly approximation, calculate the contour length and draw the result
Program code:
1 //OpenCVExerciseTesting.cpp: Defines the entry point of the console application. 2 //3 //string file_full_name = "d:\\work\\work_programming\\source\\image\\opencvexerciseimage\\ 8th chapter \\r20.jpg";4 5 6#include"stdafx.h"7#include <string>8#include <cv.h>9#include Ten#include <iostream> One#include <math.h> A -#include <opencv2/legacy/legacy.hpp> - //#pragma comment (lib, "Opencv_legacy2411.lib") the - using namespaceCV; - using namespacestd; - + //function declaration---->-->--->-->--->-->--->// - + A //<--<--<--<--<--<--<--<--<--Function Declaration// at - int_tmain (intARGC, _tchar*argv[]) - { - stringFile_full_name ="d:\\work\\work_programming\\source\\image\\opencvexerciseimage\\ 8th Chapter \\r20.jpg"; - -Iplimage * Image_source =Cvloadimage (File_full_name.c_str (), cv_load_image_grayscale); in Cv_assert (image_source); - toIplimage * Image_binary =cvcloneimage (image_source); + Cvzero (image_binary); - theCvthreshold (Image_source, Image_binary, the,255, cv_thresh_binary); * $Cvmemstorage *storage =cvcreatememstorage ();Panax Notoginsengcvseq* first_contour=NULL; - intContour_num; theContour_num = Cvfindcontours (image_binary, storage, &first_contour,sizeof(Cvcontour), cv_retr_list); + Acout <<"Number of outlines"<< Contour_num <<Endl; the + Doublecontour_length; - //For (CVSEQ * c = first_contour; c = NULL; c = c->h_next) $ //{ $ //contour_length = Cvcontourperimeter (c); - //cout << "perimeter" << contour_length << Endl; - //} theContour_length =Cvcontourperimeter (first_contour); -cout <<"Perimeter"<< Contour_length <<Endl;Wuyi the DoublePerimeter =126.7; - Doubleparameters[4] = {126.7/ -,126.7/ the,126.7/ One,126.7/Ten }; Wucvmemstorage* Storage_approx =cvcreatememstorage (); - AboutIplimage *image_approx =cvcloneimage (image_binary); $ Cvzero (Image_approx); -Cvseq *seq_approx=NULL; - - stringWindow_name ="approx window";; A for(inti =0; I <4; ++i) + { theSeq_approx = Cvapproxpoly (First_contour,sizeof(Cvcontour), Storage_approx, CV_POLY_APPROX_DP, Parameters[i],0); -Contour_length =Cvcontourperimeter (Seq_approx); $cout << contour_length <<Endl; theWindow_name = Window_name +"."; theCvdrawcontours (Image_approx, Seq_approx, Cvscalar (255), Cvscalar ( the),0); the Cvshowimage (Window_name.c_str (), image_approx); the } - inCvwaitkey (0); the theCvreleaseimage (&image_source); AboutCvreleaseimage (&image_binary); theCvreleaseimage (&Image_approx); the cvdestroyallwindows (); the + return 0; -}
Result Picture:
Homilies Miao DAO:
For reference:
"Exercise 8.5" Contour length computer Cvapproxpoly approximation