代码示例
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char const *argv[])
{
Mat gray = imread("3.jpg", 0), edge;
imshow("原图",gray);
gray = gray > 120; // 阈值化
Mat out = Mat::zeros(gray.size(), CV_8UC3);
// 定义轮廓和层次结构
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
// 查找轮廓
findContours( gray, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE );
// 遍历所有轮廓
for( int i = 0; i >= 0; i = hierarchy[i][0] ) {
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( out, contours, i, color, FILLED, 8, hierarchy );
}
imshow( "轮廓图", out );
waitKey(0);
return 0;
}
image.png
image.png
网友评论