qt中,使用opencv算法,针对一张灰度图,给出一个算法,使它能够依据一些参数,识别图中白色圆形的算法,要求输出圆心坐标、圆面积、圆度等参数
时间: 2024-03-01 10:50:51 浏览: 160
好的,下面是一个使用OpenCV在Qt中识别灰度图中圆形的算法示例,同时输出圆心坐标、圆面积和圆度等参数:
```c++
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <QDebug>
cv::Mat qimage_to_mat(const QImage& qimage) {
cv::Mat mat;
switch (qimage.format()) {
case QImage::Format_ARGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (void*)qimage.constBits(), qimage.bytesPerLine());
break;
case QImage::Format_RGB32:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (void*)qimage.constBits(), qimage.bytesPerLine());
cv::cvtColor(mat, mat, cv::COLOR_BGRA2BGR);
break;
case QImage::Format_RGB888:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC3, (void*)qimage.constBits(), qimage.bytesPerLine());
cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);
break;
case QImage::Format_Grayscale8:
mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC1, (void*)qimage.constBits(), qimage.bytesPerLine());
break;
default:
qWarning() << "QImage format not supported.";
break;
}
return mat;
}
QImage mat_to_qimage(const cv::Mat& mat) {
QImage qimage;
switch (mat.type()) {
case CV_8UC4:
qimage = QImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
break;
case CV_8UC3:
qimage = QImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
break;
case CV_8UC1:
qimage = QImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Grayscale8);
break;
default:
qWarning() << "cv::Mat type not supported.";
break;
}
return qimage;
}
void find_circles(const QImage& image, int canny_threshold, int accumulator_threshold, double min_radius, double max_radius) {
cv::Mat mat = qimage_to_mat(image);
// 高斯模糊处理
cv::Mat blurred;
cv::GaussianBlur(mat, blurred, cv::Size(9, 9), 2, 2);
// Canny边缘检测
cv::Mat edges;
cv::Canny(blurred, edges, canny_threshold, canny_threshold * 2);
// Hough圆检测
std::vector<cv::Vec3f> circles;
cv::HoughCircles(edges, circles, cv::HOUGH_GRADIENT, 1, mat.rows / 8, accumulator_threshold, 20, min_radius, max_radius);
// 在原图上绘制圆
QImage result = image.copy();
QPainter painter(&result);
painter.setPen(QPen(Qt::red, 2));
for (const auto& circle : circles) {
cv::Point center(std::round(circle[0]), std::round(circle[1]));
int radius = std::round(circle[2]);
painter.drawEllipse(QPoint(center.x, center.y), radius, radius);
double area = M_PI * radius * radius;
double circularity = (4 * M_PI * area) / (circle[3] * circle[3]);
qDebug() << "Center: (" << center.x << ", " << center.y << "), Area: " << area << ", Circularity: " << circularity;
}
// 显示结果
cv::imshow("Result", qimage_to_mat(result));
cv::waitKey();
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QImage image("example.png");
int canny_threshold = 100;
int accumulator_threshold = 50;
double min_radius = 10;
double max_radius = 100;
find_circles(image, canny_threshold, accumulator_threshold, min_radius, max_radius);
return app.exec();
}
```
在这个示例中,我们首先定义了两个辅助函数`qimage_to_mat()`和`mat_to_qimage()`,用于将Qt中的QImage对象转换为OpenCV中的cv::Mat对象,以及将cv::Mat对象转换为QImage对象。然后我们定义了一个`find_circles()`函数,用于在灰度图中查找圆形。
在`find_circles()`函数中,我们首先对输入图像进行高斯模糊处理,以减少噪点干扰。然后使用Canny算法检测图像边缘。接下来,我们使用Hough圆变换算法查找图像中的圆形。最后,我们在原图上绘制圆,并输出圆心坐标、圆面积和圆度等参数。最后,我们将结果用QPainter绘制到QImage对象上,并使用OpenCV的imshow()函数显示结果。
在主函数中,我们首先加载输入图像,并定义一些算法参数。然后调用`find_circles()`函数进行圆形检测。
阅读全文