C++ Opencv——图像处理——轮廓

findContours

Mat grayImage;//输入处理后的二值图
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Mat showImage = Mat::zeros(grayImage.size(), CV_32SC1);
Mat showedge = Mat::zeros(grayImage.size(), CV_8UC1);
findContours(grayImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));

drawContours

drawContours(要绘制的图、所有轮廓容器、轮廓编号、颜色填充、线宽);

for (size_t i = 0; i < contours.size(); i++)
{
	//这里static_cast<int>(i+1)是为了分水岭的标记不同,区域1、2、3。。。。这样才能分割
	drawContours(showImage, contours, static_cast<int>(i), Scalar::all(static_cast<int>(i + 1)), 2);//指定要绘制轮廓的编号
}

 boundingRect

// 矩形边框 boundingRect
Rect rect = boundingRect(Mat(contours[i]));
rectangle(edge_dst_, rect, Scalar(theRNG().uniform(0, 255), theRNG().uniform(0, 255), theRNG().uniform(0, 255)), 3);

 minAreaRect

//最小外接边框
RotatedRect box = minAreaRect(Mat(contours[i]));  //计算每个轮廓最小外接矩形
Point2f rect[4];
box.points(rect);  //把最小外接矩形四个端点复制给rect数组
float angle = box.angle;
for (int j = 0; j<4; j++)
{
	line(edge_dst_, rect[j], rect[(j + 1) % 4], Scalar(1), 2, 8);  //绘制最小外接矩形每条边
}

猜你喜欢

转载自blog.csdn.net/weixin_41275726/article/details/85096007