opencv 查找并绘制轮廓
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat srcImage = imread("1.jpg",0);
imshow("yuantu",srcImage);
Mat dstImage = Mat::zeros(srcImage.rows,srcImage.cols,CV_8SC3);
srcImage = srcImage > 119;
imshow("fazhitu",srcImage);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(srcImage,contours,hierarchy,RETR_CCOMP,CHAIN_APPROX_NONE);
int index = 0;
for(; index >= 0;index = hierarchy[index][0])
{
//drawChessboardCorners(InputOutputArray image,Size patternSize,InputArray corners,bool patternWasFound)
Scalar color(rand()&255,rand()&255,rand()&255);
drawContours(dstImage,contours,index,color,FILLED,8,hierarchy);
}
imshow("lunkuotu",srcImage);
waitKey(0);
return 0;
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat srcImage = imread("1.jpg",0);
imshow("yuantu",srcImage);
Mat dstImage = Mat::zeros(srcImage.rows,srcImage.cols,CV_8SC3);
srcImage = srcImage > 119;
imshow("fazhitu",srcImage);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(srcImage,contours,hierarchy,RETR_CCOMP,CHAIN_APPROX_NONE);
int index = 0;
for(; index >= 0;index = hierarchy[index][0])
{
//drawChessboardCorners(InputOutputArray image,Size patternSize,InputArray corners,bool patternWasFound)
Scalar color(rand()&255,rand()&255,rand()&255);
drawContours(dstImage,contours,index,color,FILLED,8,hierarchy);
}
imshow("lunkuotu",srcImage);
waitKey(0);
return 0;
}
运行图如下