drawContours c++
时间: 2023-11-23 17:55:25 浏览: 149
以下是drawContours函数的C++示例:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("example.jpg");
if (image.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
Mat result = Mat::zeros(image.size(), CV_8UC3);
for (size_t i = 0; i < contours.size(); i++)
{
drawContours(result, contours, i, Scalar(0, 0, 255), 2, LINE_8, hierarchy);
}
imshow("Result", result);
waitKey(0);
return 0;
}
```
该示例首先读取一张图像,将其转换为灰度图像并进行二值化处理。然后使用findContours函数查找轮廓,并使用drawContours函数将轮廓绘制在新的图像上。最后显示结果图像并等待用户按下任意键退出程序。
阅读全文