【练习8.5】轮廓长度计算机cvApproxPoly逼近
2015-05-19 18:52
267 查看
题目要求 | 程序代码 | 结果图片 | 要言妙道 | 借鉴参考 |
a、检测轮廓并计算轮廓长度
b、分别使用1/90,1/66,1/11,1/10做为精度参数,使用cvApproxPoly逼近,计算轮廓长度并画出结果
程序代码:
// OpenCVExerciseTesting.cpp : 定义控制台应用程序的入口点。 // // string file_full_name = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第8章\\r20.jpg"; #include "stdafx.h" #include<string> #include <cv.h> #include <highgui.h> #include <iostream> #include<math.h> #include <opencv2/legacy/legacy.hpp> //#pragma comment(lib, "opencv_legacy2411.lib") using namespace cv; using namespace std; //函数声明-->--->-->--->-->--->-->--->// //<--<--<--<--<--<--<--<--<--函数声明// int _tmain(int argc, _TCHAR* argv[]) { string file_full_name = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第8章\\r20.jpg"; IplImage * image_source = cvLoadImage(file_full_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE); CV_Assert(image_source); IplImage * image_binary = cvCloneImage(image_source); cvZero(image_binary); cvThreshold(image_source, image_binary, 125, 255, CV_THRESH_BINARY); CvMemStorage *storage = cvCreateMemStorage(); CvSeq* first_contour=NULL; int contour_num; contour_num = cvFindContours(image_binary, storage, &first_contour, sizeof(CvContour), CV_RETR_LIST); cout << "轮廓数" << contour_num << endl; double contour_length; //for (CvSeq * c = first_contour; c != NULL; c = c->h_next) //{ // contour_length = cvContourPerimeter(c); // cout << "周长" << contour_length << endl; //} contour_length = cvContourPerimeter(first_contour); cout << "周长" << contour_length << endl; double perimeter = 126.7; double parameters[4] = { 126.7 / 90, 126.7 / 66, 126.7 / 11, 126.7 / 10 }; CvMemStorage* storage_approx = cvCreateMemStorage(); IplImage *image_approx = cvCloneImage(image_binary); cvZero(image_approx); CvSeq *seq_approx=NULL; string window_name = "Approx窗口";; for (int i = 0; i < 4; ++i) { seq_approx = cvApproxPoly(first_contour, sizeof(CvContour), storage_approx, CV_POLY_APPROX_DP, parameters[i], 0); contour_length = cvContourPerimeter(seq_approx); cout << contour_length << endl; window_name = window_name + "."; cvDrawContours(image_approx, seq_approx, cvScalar(255), cvScalar(125), 0); cvShowImage(window_name.c_str(), image_approx); } cvWaitKey(0); cvReleaseImage(&image_source); cvReleaseImage(&image_binary); cvReleaseImage(&image_approx); cvDestroyAllWindows(); return 0; }
结果图片:
借鉴参考:
相关文章推荐
- cvApproxPoly 多边形逼近轮廓
- cvApproxPoly_OpenCV_介绍
- cvApproxPoly的学习
- cvApproxPoly的学习(找任意形状)
- cvApproxPoly的学习
- opencv 霍夫变换cvHoughLines2 cvHoughCircles cvApproxPoly
- approxPolyDP-轮廓近似
- 【练习8.1】查找轮廓、寻找关键点cvFindDominantPoints、访问序列中的元素
- 计算机网络小练习2
- 惠州学院计算机基础课程配套练习系统
- 第三周练习题目—二分法 影子的最大长度
- 计算机视觉(CV)领域的公司
- 练习1-16 修改打印最长文本行的程序的主程序main,使之可以打印任意长度的输入行的长度,并尽可能多地打印文本。
- 计算机英语单词练习五
- 创建包围轮廓的矩形和圆形边界框--boundingRect()、minEnclosingCircle()和approxPolyDP()
- (69)TreeSet练习:按照长度为主关键字,自然顺序为次关键字排序
- 《黑马程序员》 TreeSet集合练习之 按照字符串长度排序
- 计算机视觉CV 圈子
- 国内从事计算机视觉(CV)领域的公司
- [计算机网络] 以太网数据帧(802.3)最大与最小长度