用C++写一个opencv的圆形孔洞检测代码
时间: 2023-06-04 08:05:36 浏览: 315
以下是使用C语言编写OpenCV圆形孔洞检测代码的示例:
```c
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv) {
Mat image;
if (argc != 2) {
printf("Usage: %s <Image_Path>\n", argv[0]);
return -1;
}
image = imread(argv[1], IMREAD_GRAYSCALE);
if (image.empty()) {
printf("Could not open or find the image!\n");
return -1;
}
GaussianBlur(image, image, Size(9, 9), 2, 2);
std::vector<Vec3f> circles;
HoughCircles(image, circles, HOUGH_GRADIENT, 1, image.rows / 8, 200, 100, 0, 0);
for (size_t i = 0; i < circles.size(); i++) {
Vec3i c = circles[i];
Point center = Point(c[0], c[1]);
int radius = c[2];
circle(image, center, radius, Scalar(0, 255, 0), 2, LINE_AA);
}
namedWindow("Circle Detection", WINDOW_NORMAL);
imshow("Circle Detection", image);
waitKey(0);
return 0;
}
```
该程序需要一个图片作为参数进行运行,使用以下命令来执行:
```bash
./circle_detection image.jpg
```
输出结果将显示包含检测到的圆形孔洞的图像窗口。
阅读全文