123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #include "MeterRead.h"
- constexpr auto PI = 3.1415926;
- constexpr auto CV_AA = 16;
- Point circle_center;
- int circle_radius;
- double MeterRead::GetResult(cv::Mat sourceMat, bool IsShowWnd)
- {
- Mat mat;
- sourceMat.copyTo(mat);
- if (IsShowWnd) {
- //imshow("原图", mat);
- waitKey(1);
- }
- int matCols = 500;
- Size size(500, 420);
- resize(mat, mat, size, (float)matCols / mat.cols, (float)matCols / mat.cols);
- int width = mat.cols;
- int height = mat.rows;
- Mat grayMat;
- cvtColor(mat, grayMat, COLOR_BGR2GRAY); // 转换为灰度图
- Mat gaussMat;
- GaussianBlur(grayMat, gaussMat, Size(3, 3), 0, 0);
- if (IsShowWnd) {
- //imshow("高斯模糊图", gaussMat);
- waitKey(1);
- }
- Mat dst;
- threshold(gaussMat, dst,120, 255, THRESH_BINARY_INV);//二值化阈值处理
- Mat biMat;
- bilateralFilter(dst, biMat, 9, 50, 50);
- if (IsShowWnd) {
- //imshow("滤波去噪图", biMat);
- waitKey(1);
- }
- Mat cannyMat;
- Canny(biMat, cannyMat,50, 200,3);//边缘检测
- if (IsShowWnd) {
- //imshow("边缘检测", cannyMat);
- waitKey(1);
- }
- vector<cv::Vec4i> lines;
- double begin_angle = 0;
- double end_angle = 0;
- double det_angle = 0;
- HoughLinesP(cannyMat, lines, 1, CV_PI / 180, 95, 120, 26);
- for (int i = 0; i < lines.size(); ++i) {
- Vec4i line_ = lines[i];
- double k = ((double)line_[3] - line_[1]) / ((double)line_[2] - line_[0]); //直线的斜率
- double angle = (atan(k) * 180 / PI); //直线角度
-
- line(mat, Point(line_[0], line_[1]), Point(line_[2], line_[3]), Scalar(0, 255, 0), 2, CV_AA);
- if (angle >= 0 && angle < 3.8)
- {
- begin_angle = 0;
- }
- if (angle >= 3.8 && angle < 87)
- {
- det_angle = angle;
- }
- if ((angle >= -90 && angle < -87) || (angle >= 87 && angle <= 90))
- {
- end_angle = angle;
- }
- }
- //imshow("霍夫直线检测",mat);
- waitKey(1);
- float resutValue =(det_angle/(abs(end_angle) - begin_angle)) * 300;
- return resutValue;
- }
-
|