C++ findContours函数取出的轮廓有重叠
时间: 2023-11-04 07:53:36 浏览: 39
回答: 在C++中,使用findContours函数提取的轮廓可能会有重叠的情况发生。\[1\]这是因为findContours函数默认使用的轮廓检测模式是CV_RETR_EXTERNAL,即只提取外部轮廓。如果图像中存在内部轮廓或重叠的轮廓,它们将被忽略。要解决这个问题,可以尝试使用其他的轮廓检测模式,如CV_RETR_LIST或CV_RETR_TREE,以提取所有的轮廓,包括内部轮廓。另外,还可以使用CV_CHAIN_APPROX_NONE参数来保留所有轮廓的所有像素点,而不是进行轮廓的近似处理。\[1\]这样可以确保提取的轮廓不会有重叠的情况发生。
#### 引用[.reference_title]
- *1* *2* *3* [车牌识别easypr的详细介绍](https://blog.csdn.net/zdf511391565/article/details/116092746)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关问题
C++findcontours函数使用
使用C++中的`findContours`函数,需要先准备好输入的二值图像`image`,并创建一个向量`vector<vector<Point>> contours`来存储输出的轮廓信息。
下面是一个简单的示例代码,实现了从一张图片中提取出所有轮廓,并将其绘制在新的画布上:
```C++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 读取输入图像
Mat img = imread("input.jpg", CV_LOAD_IMAGE_GRAYSCALE);
// 检查图像是否读取成功
if (img.empty())
{
cout << "Could not read the image: " << endl;
return 1;
}
// 创建一个空的画布
Mat canvas(img.size(), CV_8UC3, Scalar(0, 0, 0));
// 阈值化图像
Mat thresh;
threshold(img, thresh, 100, 255, THRESH_BINARY);
// 查找轮廓
vector<vector<Point>> contours;
findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 绘制轮廓
drawContours(canvas, contours, -1, Scalar(0, 255, 0), 2);
// 显示结果
imshow("Contours", canvas);
waitKey(0);
return 0;
}
```
在上述代码中,首先读取了一张图片,然后创建了一个空的画布。接着,使用`threshold`函数对图像进行了阈值化处理,得到一个二值图像。然后,使用`findContours`函数查找轮廓,并将结果存储在`contours`向量中。最后,使用`drawContours`函数将所有轮廓绘制到画布上,并将结果显示出来。
需要注意的是,这里使用了`RETR_EXTERNAL`参数来指定只查找最外层轮廓。如果需要查找所有轮廓,可以使用`RETR_LIST`参数。
findContours函数 c++
findContours函数是OpenCV库中用于轮廓检测和分析的函数,它可以帮助我们找到图像中的轮廓,并计算出轮廓的面积、周长、几何矩、质心、凸包、最小外接矩形等特征。
以下是findContours函数的C++示例代码:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 读取图像
Mat image = imread("image.jpg", IMREAD_GRAYSCALE);
// 预处理图像
Mat binary;
threshold(image, binary, 128, 255, THRESH_BINARY);
// 寻找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 绘制轮廓
Mat result(image.size(), CV_8UC3, Scalar(0, 0, 0));
drawContours(result, contours, -1, Scalar(0, 0, 255), 2);
// 显示结果
imshow("Contours", result);
waitKey(0);
return 0;
}
```
这段代码首先读取一张灰度图像,然后对图像进行二值化处理。接下来,使用findContours函数找到图像中的轮廓,并将轮廓存储在contours向量中。最后,使用drawContours函数将轮廓绘制在结果图像上,并显示出来。