• 转载:approxPolyDP函数


    本文转载http://blog.csdn.net/qq_18343569/article/details/47999257

    1、approxPolyDP函数

    函数的作用:

    对图像轮廓点进行多边形拟合

    2、函数的调用形式

    C++: void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)

    参数详解;

    InputArray curve:一般是由图像的轮廓点组成的点集

    OutputArray approxCurve:表示输出的多边形点集

     double epsilon:主要表示输出的精度,就是另个轮廓点之间最大距离数,5,6,7,,8,,,,,

    bool closed:表示输出的多边形是否封闭

    3、OPENCV代码

     1 #include "opencv2/highgui/highgui.hpp"
     2 #include "opencv2/imgproc/imgproc.hpp"
     3 #include <iostream>
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 
     7 using namespace cv;
     8 using namespace std;
     9 
    10 Mat src; Mat src_gray;
    11 int thresh = 100;
    12 int max_thresh = 255;
    13 RNG rng(12345);
    14 
    15 /// Function header
    16 void thresh_callback(int, void*);
    17 
    18 /** @function main */
    19 int main(int argc, char** argv)
    20 {
    21     /// 加载源图像
    22     src = imread("D:6.jpg", 1);
    23 
    24     /// 转成灰度图并进行模糊降噪
    25     cvtColor(src, src_gray, CV_BGR2GRAY);
    26     blur(src_gray, src_gray, Size(3, 3));
    27 
    28     /// 创建窗体
    29     char* source_window = "Source";
    30     namedWindow(source_window, CV_WINDOW_AUTOSIZE);
    31     imshow(source_window, src);
    32 
    33     createTrackbar(" Threshold:", "Source", &thresh, max_thresh, thresh_callback);
    34     thresh_callback(0, 0);
    35 
    36     waitKey(0);
    37     return(0);
    38 }
    39 
    40 /** @function thresh_callback */
    41 void thresh_callback(int, void*)
    42 {
    43     Mat src_copy = src.clone();
    44     Mat threshold_output;
    45     vector<vector<Point> > contours;
    46     vector<Vec4i> hierarchy;
    47 
    48     /// 对图像进行二值化
    49     threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);
    50 
    51     /// 寻找轮廓
    52     findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    53     /*Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);*/
    54 
    55     /* 对每个轮廓计算其凸包*/
    56     vector<vector<Point> >poly(contours.size());
    57     for (int i = 0; i < contours.size(); i++)
    58     {
    59         approxPolyDP(Mat(contours[i]), poly[i], 5,true);
    60     }
    61 
    62     /* 绘出轮廓及其凸包*/
    63     Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
    64     for (int i = 0; i< contours.size(); i++)
    65     {
    66         Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
    67         drawContours(drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point());
    68         drawContours(drawing, poly, i, color, 1, 8, vector<Vec4i>(), 0, Point());
    69     }
    70     /*vector<Point> poly;
    71     approxPolyDP(Mat(contours[3]), poly, 5, false);
    72     vector<Point>::const_iterator itp = poly.begin();
    73     while (itp != (poly.end() - 2))
    74     {
    75         line(drawing, *itp, *(itp + 1), Scalar(255), 2);
    76         ++itp;
    77     }
    78     line(drawing, *itp, *(poly.begin()), Scalar(255), 2);*/
    79     /// 把结果显示在窗体
    80     namedWindow("Hull demo", CV_WINDOW_AUTOSIZE);
    81     imshow("Hull demo", drawing);
    82 }
  • 相关阅读:
    PlayerPrefs存储Vector3等结构数据
    Kafka集群部署及測试
    火云开发课堂
    Thinking in Java:容器深入研究
    求int型数据在内存中存储时1的个数
    JAVA 几种多线程的简单实例 Thread Runnable
    Android利用Intent与其它应用交互
    kernel
    Azure DocumentDB 正式发布
    在公有云平台体验开源方案的自动部署
  • 原文地址:https://www.cnblogs.com/supercxm/p/8458931.html
Copyright © 2020-2023  润新知