bool Get_Character_ROI(License& License_ROI, vector<License>& Character_ROI) { Mat gray; cvtColor(License_ROI.mat, gray, COLOR_BGR2GRAY); Mat thresh; threshold(gray, thresh, 0, 255, THRESH_BINARY | THRESH_OTSU); Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); Mat close; morphologyEx(thresh, close, MORPH_CLOSE, kernel); vector<vector<Point>>contours; findContours(close, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); for (int i = 0; i < contours.size(); i++) { double area = contourArea(contours[i]); //由于我们筛选出来的轮廓是无序的,故后续我们需要将字符重新排序 if (area > 200) { Rect rect = boundingRect(contours[i]); //计算外接矩形宽高比 double ratio = double(rect.height) / double(rect.width); if (ratio > 1) { Mat roi = License_ROI.mat(rect); resize(roi, roi, Size(50, 100), 1, 1, INTER_LINEAR); Character_ROI.push_back({ roi ,rect }); } } } //将筛选出来的字符轮廓 按照其左上角点坐标从左到右依次顺序排列 //冒泡排序 for (int i = 0; i < Character_ROI.size() - 1; i++) { for (int j = 0; j < Character_ROI.size() - 1 - i; j++) { if (Character_ROI[j].rect.x > Character_ROI[j + 1].rect.x) { License temp = Character_ROI[j]; Character_ROI[j] = Character_ROI[j + 1]; Character_ROI[j + 1] = temp; } } } if (Character_ROI.size() != 7) { return false; } return true; }
时间: 2024-04-26 07:20:58 浏览: 16
这段代码用于从车牌的ROI区域中分割出每个字符的ROI区域,并按照从左到右的顺序排列。具体流程如下:
1. 将车牌的ROI区域转换为灰度图像。
2. 对灰度图像进行二值化处理并进行闭运算,以便于分割字符。
3. 使用findContours()函数查找图像中的轮廓。
4. 对于每个轮廓,计算其面积和外接矩形宽高比。
5. 如果面积和宽高比符合要求,则将其截取出来作为字符的ROI区域。
6. 将所有的字符ROI区域按照从左到右的顺序进行排序,方便后续的识别处理。
7. 如果分割出的字符ROI区域不足7个,则返回false表示分割失败;否则返回true表示分割成功。
在上述流程中,使用了OpenCV中的函数cvtColor()、threshold()、getStructuringElement()、morphologyEx()、findContours()、boundingRect()、resize()等函数来进行图像转换、二值化、闭运算、轮廓查找、矩形区域计算、图像缩放等操作。通过这些操作,可以将车牌的字符分割出来,并按照从左到右的顺序进行排列,方便后续的字符识别处理。
相关问题
std::vector<bool> binary_vec;
这是一个定义了名为 `binary_vec` 的空的 `std::vector` 对象,它的元素类型是 `bool`,也就是布尔类型。 `std::vector` 是 C++ 标准库中的一个容器类,它可以存储一系列同种类型的元素,并且支持动态调整大小、随机访问、插入和删除等操作。在这里,`std::vector<bool>` 是一个特化版本,它进行了一些优化以节省内存空间,但同时也牺牲了一些标准 `std::vector` 的功能。由于 `std::vector<bool>` 的实现方式与其他标准容器类不同,因此在使用时需要注意一些细节。
vector<vector<int> >& startEnd 按第一列排序
要按照二维向量 `startEnd` 的第一列进行排序,可以使用 `std::sort` 函数结合自定义的比较函数来实现。以下是一个示例代码:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
bool compare(const std::vector<int>& a, const std::vector<int>& b) {
return a[0] < b[0];
}
int main() {
std::vector<std::vector<int>> startEnd = {{3, 4}, {1, 2}, {2, 3}, {4, 5}};
// 按第一列排序
std::sort(startEnd.begin(), startEnd.end(), compare);
// 输出排序后的结果
for (const auto& vec : startEnd) {
std::cout << vec[0] << " " << vec[1] << std::endl;
}
return 0;
}
```
运行以上代码,输出结果为:
```
1 2
2 3
3 4
4 5
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)