#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
using namespace std;
using namespace cv;
int threshval = 160;
int main()
{
Mat img = imread("f:/mei.jpeg");
Mat gray ;
cvtColor(img,gray,CV_RGB2GRAY);
namedWindow("image",WINDOW_AUTOSIZE);
imshow("image",img);
namedWindow("gray",WINDOW_AUTOSIZE);
gray = threshval<160?(gray<threshval):(gray>threshval);
imshow("gray",gray);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat dst = Mat::zeros(gray.size(),CV_8UC3);
findContours(gray,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
if(!contours.empty()&&!hierarchy.empty())
{
int idx = 0;
for(;idx>=0;idx=hierarchy[idx][0])
{
Scalar color(rand()%255,rand()%255,rand()%255);
drawContours(dst,contours,idx,color,CV_FILLED,8,hierarchy);
}
}
namedWindow("contours",WINDOW_AUTOSIZE);
imshow("contours",dst);
waitKey();
return 0;
}
opencv轮廓提取
最新推荐文章于 2024-05-01 19:21:34 发布