您的位置:首页 > 理论基础

【练习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;
}


结果图片:






要言妙道:

借鉴参考:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: