opencv3逼近多边形曲线-approxPolyDP函数在图像中的应用

来源:互联网 发布:mmd数据 编辑:程序博客网 时间:2024/05/21 20:26
#include<opencv2/opencv.hpp>#include<iostream>#include<vector>using namespace cv;using namespace std;int main(){Mat srcImage = imread("1.jpg");imshow("【原图】", srcImage);//先对图像进行空间的转换(为了之后要提取二值图像)Mat grayImage;cvtColor(srcImage, grayImage, CV_BGR2GRAY);//对图像进行滤波,达到较好的效果GaussianBlur(grayImage, grayImage, Size(3, 3), 0, 0);imshow("【滤波后的图像】", grayImage);//用边缘检测的方式获取二值图像Mat cannyImage;Canny(grayImage, cannyImage, 128, 255, 3);//在二值图像中提取轮廓vector<vector<Point>> contours;vector<Vec4i> hierarchy;findContours(cannyImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));//对每个轮廓的点集 找逼近多边形vector<vector<Point>> approxPoint(contours.size());for (int i = 0; i < (int)contours.size(); i++){approxPolyDP(contours[i], approxPoint[i], 3, true);}/******************************************绘制曲线的方式********************************************///用绘制轮廓的函数   绘制曲线Mat drawImage = Mat::zeros(srcImage.size(), CV_8UC3);for (int i = 0; i < (int)contours.size(); i++){drawContours(drawImage, contours, i, Scalar(255, 255, 255), 1);}imshow("【绘制后的图像】", drawImage);waitKey(0);return 0;}

0 0
原创粉丝点击