opencv学习之路(23)、轮廓查找与绘制(二)——访问轮廓每个点

一、简介

二、画出每个轮廓的每个点

 1 #include "opencv2/opencv.hpp"
 2 using namespace cv;
 3 
 4 void main()
 5 {
 6     Mat src=imread("E://22.jpg");
 7     Mat temp=src.clone();
 8     //转灰度图,二值化
 9     cvtColor(src,src,CV_BGR2GRAY);
10     threshold(src,src,100,255,THRESH_BINARY);
11     imshow("thres",src);
12     //查找并绘制轮廓
13     vector<vector<Point>>contours;
14     vector<Vec4i>hierarcy;
15     findContours(src,contours,hierarcy,CV_RETR_TREE,CV_LINK_RUNS);
16     /*drawContours(temp,contours,-1,Scalar(0,255,0),2,8);    绘制并显示所有轮廓
17     imshow("contours",temp);*/
18 
19     //绘制轮廓的每一个点
20     for(int i=0; i<contours.size(); i++){
21         for(int j=0; j<contours[i].size(); j++){
22             circle(temp,Point(contours[i][j].x,contours[i][j].y),3,Scalar(0,255,0),2,8); 
23         }
24     }
25     imshow("contours",temp);
26     waitKey(0);
27 }

轮廓太密集了,稍微改改参数

     for(int j=0; j<contours[i].size(); j+=15){
            circle(temp,Point(contours[i][j].x,contours[i][j].y),3,Scalar(0,255,0),2,8); 
        }

稍微改改,有另一种效果(动态射线)

1 for(int i=0; i<contours.size(); i++){
2         for(int j=0; j<contours[i].size(); j+=15){
3             circle(temp,Point(contours[i][j].x,contours[i][j].y),3,Scalar(0,255,0),2,8); 
4             line(temp,Point(10,10),Point(contours[i][j].x,contours[i][j].y), Scalar(0, 0, 255), 1, 8);
5             waitKey(100);
6             imshow("contours", temp);
7         }
8     }

改改又是一种效果

三、孔洞填充

 1 #include "opencv2/opencv.hpp"
 2 using namespace cv;
 3 
 4 void main()
 5 {
 6     Mat src=imread("E://33.jpg");
 7     Mat temp=src.clone();
 8     //转灰度图,二值化
 9     cvtColor(src,src,CV_BGR2GRAY);
10     threshold(src,src,100,255,THRESH_BINARY);
11     imshow("thres",src);
12     //查找并绘制轮廓
13     vector<vector<Point>>contours;
14     vector<Vec4i>hierarcy;
15     findContours(src,contours,hierarcy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
16     
17     ///孔洞填充
18     findContours(src, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //查找轮廓
19     drawContours(temp, contours, -1, Scalar(0, 255, 0), -1, 8); //绘制轮廓
20     imshow("contours",temp);
21     waitKey(0);
22 }

稍微改改

原文地址:https://www.cnblogs.com/little-monkey/p/7423992.html