GPT答: |
7i24.cn
|
在C++中,可以使用OpenCV库来实现在二值化后的图片中查找轮廓。具体步骤如下:
1. 读取图像并将其转换为灰度图像。
```c++
Mat srcImage = imread("image.jpg");
Mat grayImage;
cvtColor(srcImage, grayImage, COLOR_BGR2GRAY);
```
2. 对灰度图像进行二值化处理。
```c++
Mat binaryImage;
threshold(grayImage, binaryImage, 0, 255, THRESH_BINARY | THRESH_OTSU);
```
3. 查找轮廓并绘制出来。
```c++
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binaryImage, contours, hierarchy, RETR_TREE,
CHAIN_APPROX_SIMPLE);
Mat contourImg = Mat::zeros(binaryImg.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++) {
Scalar color(rand() & 255), rand() & 255), rand() & 255));
drawContours(contourImg ,contours,i,color,-1); // -1表示填充轮廓
}
imshow("Contour Image", contourImg );
waitKey(0);
```
以上代码会生成一个包含所有轮廓的彩色图像,并显示出来。其中,`findContours()`函数用于查找所有的轮廓,返回一个包含每个轮廓点集合的向量;`drawContours()`函数用于绘制指定颜色和线宽的单个或多个闭合或开放曲线。
|