代码在下边,运行程序后,逼近的轮廓长度和之前计算的轮廓长度没有差别,初学,不知道是否代码有问题,或者是因为轮廓没有凸包,所以近似结果比较好,?
——————————————————————————————————————————————————
#include "stdafx.h"
#include "highgui.hpp"#include "cv.hpp"
#include "iostream"
using namespace std;
using namespace cv;
int main()
{
//读入图像
Mat src = imread("H:\\circle.png", 0);
if (src.empty())
{
cout << "Load image error!" << endl;
return(-1);
}
imshow("source image", src);
//canny
Mat edges;
Canny(src, edges, 100, 255);
//检测轮廓
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(edges, contours, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
cout << "The contours number is " <<contours.size() << endl;
//画轮廓
Scalar color = Scalar(255, 255, 255);
for (int i = 0; i < contours.size(); i++)
{
drawContours(edges, contours, i, color, 1, 8);
}
imshow("contours image", edges);
cout << "---------------------------------------------------" << endl;
//计算轮廓长度
for (int i = 0; i < contours.size(); i++)
{
double con_length = arcLength(contours[i], true);
cout << "用轮廓长度计算函数计算出来的第" << i << "个轮廓长度为:" << con_length << endl;
}
//使用approxPolyDP逼近
// 1.创建存放近似点的vector
vector<vector<Point>>contours_90(contours.size());
vector<vector<Point>>contours_66(contours.size());
vector<vector<Point>>contours_11(contours.size());
vector<vector<Point>>contours_10(contours.size());
// 2.用来存放逼近图像
Mat img_90(edges.size(), CV_8UC3, Scalar::all(0));
Mat img_66(edges.size(), CV_8UC3, Scalar::all(0));
Mat img_11(edges.size(), CV_8UC3, Scalar::all(0));
Mat img_10(edges.size(), CV_8UC3, Scalar::all(0));
cout << " 9 0 ---------------------------------------------" << endl;
for (int i = 0; i < contours.size(); i++) {
//逐点近似
approxPolyDP(contours[i], contours_90[i],1.0/90, true);
drawContours(img_90, contours_90, i, color);
double con_length_90 = arcLength(contours_90[i], true);
cout << "用轮廓长度计算函数计算出来的第" << i << "个轮廓长度为:" << con_length_90 << endl;
}
imshow("90", img_90);
cout << " 6 6 ---------------------------------------------" << endl;
for (int i = 0; i < contours.size(); i++) {
approxPolyDP(contours[i], contours_66[i], 1.0 / 66, true);
drawContours(img_66, contours_66, i, color);
double con_length_66 = arcLength(contours_66[i], true);
cout << "用轮廓长度计算函数计算出来的第" << i << "个轮廓长度为:" << con_length_66 << endl;
}
imshow("66", img_66);
cout << " 1 1 ---------------------------------------------" << endl;
for (int i = 0; i < contours.size(); i++) {
approxPolyDP(contours[i], contours_11[i], 1.0 / 11, true);
drawContours(img_11, contours_11, i, color);
double con_length_11 = arcLength(contours_11[i], true);
cout << "用轮廓长度计算函数计算出来的第" << i << "个轮廓长度为:" << con_length_11 << endl;
}
imshow("11", img_11);
cout << " 1 0 ---------------------------------------------" << endl;
for (int i = 0; i < contours.size(); i++) {
approxPolyDP(contours[i], contours_10[i], 1.0 / 10, true);
drawContours(img_10, contours_10, i, color);
double con_length_10 = arcLength(contours_10[i], true);
cout << "用轮廓长度计算函数计算出来的第" << i << "个轮廓长度为:" << con_length_10 << endl;
}
imshow("10", img_10);
while (1) {
if (waitKey(15) == 27)
break;
}
return 0;
}