你可以给我一个fincounter函数的案例吗
时间: 2023-12-18 13:05:40 浏览: 31
当然可以。下面是一个使用 OpenCV 的 `findContours()` 函数查找图像中轮廓的示例代码:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 读取输入图像
Mat img = imread("input.png");
// 转换为灰度图像
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// 进行阈值化操作
Mat thresh;
threshold(gray, thresh, 127, 255, THRESH_BINARY);
// 查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(thresh, contours, hierarchy, RET_TREE, CHAIN_APPROX_SIMPLE);
// 在原图像上绘制轮廓
drawContours(img, contours, -1, Scalar(0, 0, 255), 2);
// 显示结果
imshow("Contours", img);
waitKey(0);
destroyAllWindows();
return 0;
}
```
在上面的代码中,我们首先读取输入图像并将其转换为灰度图像。然后使用 `cv::threshold()` 函数进行阈值化操作,将图像转换为二值图像。接下来使用 `findContours()` 函数查找图像中的轮廓,并将轮廓绘制在原图像上。最后显示结果图像。
注意,`findContours()` 函数的第一个参数是二值图像,第二个参数是轮廓的输出数组,第三个参数是轮廓的层次信息,第四个参数是轮廓的检索模式,第五个参数是轮廓的近似方法。在上面的代码中,我们使用了 `RETR_TREE` 作为轮廓的检索模式,表示检索所有轮廓,并重构嵌套轮廓的整个层次结构,使用 `CHAIN_APPROX_SIMPLE` 作为近似方法,表示压缩水平、垂直和斜线段,仅保留其端点。