接着上面的继续,如何使用markerContours向量在图片中标记出轮廓,用c++帮我实现
时间: 2024-05-14 08:14:59 浏览: 5
以下是一个使用markerContours向量在图片中标记轮廓的示例代码:
```c
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("example.jpg");
Mat grayImage;
cvtColor(image, grayImage, COLOR_BGR2GRAY);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(grayImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
vector<vector<Point>> markerContours;
for (size_t i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > 1000)
{
markerContours.push_back(contours[i]);
}
}
Mat markerImage = Mat::zeros(image.size(), CV_8UC3);
drawContours(markerImage, markerContours, -1, Scalar(0, 0, 255), 2);
imshow("Original Image", image);
imshow("Marker Image", markerImage);
waitKey(0);
return 0;
}
```
在这个例子中,我们首先读取一张图片并将其转换为灰度图像。然后,我们使用`findContours`函数查找图像的轮廓,并将它们存储在`contours`向量中。
接下来,我们使用一个循环遍历`contours`向量中的每个轮廓,并检查其面积是否大于1000像素。如果是,我们将该轮廓添加到`markerContours`向量中。
最后,我们创建一个名为`markerImage`的空图像,并使用`drawContours`函数将`markerContours`向量中的所有轮廓标记为红色。然后,我们将原始图像和标记图像显示在屏幕上,并等待用户按下任意键。
运行该代码,您将看到标记了所有面积大于1000像素的轮廓的图片。