不使用级联分类器,测量图像中螺栓的尺寸
时间: 2024-03-15 18:46:15 浏览: 23
要测量图像中螺栓的尺寸,可以使用以下步骤:
1. 读取图像并转换为灰度图像
```c++
Mat image = imread("bolts.jpg", IMREAD_COLOR);
Mat gray_image;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
```
2. 对灰度图像进行二值化处理
```c++
Mat binary_image;
threshold(gray_image, binary_image, 0, 255, THRESH_BINARY | THRESH_OTSU);
```
3. 对二值化图像进行形态学操作,去除噪声并连接目标
```c++
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(binary_image, binary_image, MORPH_OPEN, kernel);
morphologyEx(binary_image, binary_image, MORPH_CLOSE, kernel);
```
4. 对处理后的图像进行轮廓检测,并筛选出符合条件的轮廓
```c++
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary_image, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
vector<RotatedRect> rects;
for (size_t i = 0; i < contours.size(); i++) {
RotatedRect rect = minAreaRect(contours[i]);
if (rect.size.width > 50 && rect.size.height > 50 && rect.size.width < 200 && rect.size.height < 200) {
rects.push_back(rect);
}
}
```
5. 在原图像中画出符合条件的轮廓,并输出目标的尺寸
```c++
for (size_t i = 0; i < rects.size(); i++) {
Rect rect = rects[i].boundingRect();
rectangle(image, rect, Scalar(0, 255, 0), 2);
cout << "Bolt " << i << " size: " << rect.width << " x " << rect.height << endl;
}
```
完整代码如下:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 读取图像并转化为灰度图像
Mat image = imread("bolts.jpg", IMREAD_COLOR);
Mat gray_image;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
// 对灰度图像进行二值化
Mat binary_image;
threshold(gray_image, binary_image, 0, 255, THRESH_BINARY | THRESH_OTSU);
// 对二值化图像进行形态学操作
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(binary_image, binary_image, MORPH_OPEN, kernel);
morphologyEx(binary_image, binary_image, MORPH_CLOSE, kernel);
// 对处理后的图像进行轮廓检测,并筛选出符合条件的轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary_image, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
vector<RotatedRect> rects;
for (size_t i = 0; i < contours.size(); i++) {
RotatedRect rect = minAreaRect(contours[i]);
if (rect.size.width > 50 && rect.size.height > 50 && rect.size.width < 200 && rect.size.height < 200) {
rects.push_back(rect);
}
}
// 在原图像中画出符合条件的轮廓,并输出目标的尺寸
for (size_t i = 0; i < rects.size(); i++) {
Rect rect = rects[i].boundingRect();
rectangle(image, rect, Scalar(0, 255, 0), 2);
cout << "Bolt " << i << " size: " << rect.width << " x " << rect.height << endl;
}
// 显示结果图像
namedWindow("Detected Bolts", WINDOW_NORMAL);
imshow("Detected Bolts", image);
waitKey(0);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)