ArresterCircularZeroThreeALGO.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "ArresterCircularZeroThreeALGO.h"
  2. using namespace std;
  3. using namespace cv;
  4. double ArresterCircularZeroThreeALGO::GetResult(cv::Mat sourceMat, bool IsShowWnd)
  5. {
  6. Mat mat;
  7. sourceMat.copyTo(mat);
  8. if (IsShowWnd) {
  9. imshow("原图", mat);
  10. waitKey(1);
  11. }
  12. int matCols = 400;
  13. Size size(400, 190);
  14. resize(mat, mat, size, (float)matCols / mat.cols, (float)matCols / mat.cols);
  15. /* if (mat.cols - matCols >0)
  16. {
  17. resize(mat, mat, size, (float)matCols/mat.cols , (float)matCols / mat.cols);
  18. }
  19. else if (mat.cols - matCols < 0)
  20. {
  21. resize(mat, mat, size, (float)matCols /mat.cols , (float)matCols /mat.cols );
  22. }*/
  23. int width = mat.cols;// .width;
  24. int height = mat.rows;
  25. Mat grayMat;
  26. cvtColor(mat, grayMat, COLOR_BGR2GRAY); // 转换为灰度图
  27. //Mat thMat;
  28. adaptiveThreshold(grayMat, grayMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 201, 1);
  29. if (IsShowWnd) {
  30. imshow("二值化图adaptiveThreshold", grayMat);
  31. waitKey(1);
  32. }
  33. Mat biMat;
  34. bilateralFilter(grayMat, biMat, 9, 50, 50);
  35. if (IsShowWnd) {
  36. imshow("滤波去噪图", biMat);
  37. waitKey(1);
  38. }
  39. //Mat gaussMat;
  40. GaussianBlur(biMat, grayMat, Size(3, 3), 0, 0);
  41. if (IsShowWnd) {
  42. imshow("高斯模糊图", grayMat);
  43. waitKey(1);
  44. }
  45. medianBlur(grayMat, grayMat, 3);//中值滤波
  46. if (IsShowWnd) {
  47. imshow("中值滤波图", grayMat);
  48. waitKey(1);
  49. }
  50. double t1 = getTickCount();
  51. Mat ROI_1 = Mat(height, width, CV_8UC3, Scalar::all(255));
  52. cvtColor(ROI_1, ROI_1, COLOR_BGR2GRAY); // 转换为灰度图
  53. vector<Mat> vImgs;
  54. Mat result;
  55. vImgs.push_back(grayMat);
  56. vImgs.push_back(ROI_1);
  57. vconcat(vImgs, result); //垂直方向拼接
  58. //hconcat(vImgs, result); //水平方向拼接
  59. if (IsShowWnd)
  60. {
  61. imshow("拼接图", result);
  62. waitKey(1);
  63. }
  64. vector<Vec3f> circles;
  65. Mat whiteMat(height, width, CV_8UC3, Scalar::all(255));
  66. //Mat resizeMat(,)
  67. /* for (size_t i = 0; i < 200; i++)
  68. {
  69. hough_value += i;
  70. HoughCircles(grayMat, circles, HOUGH_GRADIENT, 1, 10, 100, hough_value);
  71. }*/
  72. Mat cannyMat;
  73. Canny(result, cannyMat, 50, 100);//边缘检测
  74. if (IsShowWnd) {
  75. imshow("边缘检测图", cannyMat);
  76. waitKey(1);
  77. }
  78. HoughCircles(cannyMat, circles, HOUGH_GRADIENT, 3, 30, 100, 200, 50, 200);
  79. vector<Vec4i> mylines;
  80. //HoughLinesP(grayMat, mylines, 1, CV_PI / 180, 100,5, 1);
  81. //for (size_t i = 0; i < mylines.size(); i++)
  82. //{
  83. // Vec4i cho_l = mylines[i];
  84. // //cho_l
  85. // line(whiteMat, Point(cho_l[0], cho_l[1]), Point(cho_l[2], cho_l[3]), Scalar(255, 0, 0), 1);
  86. //}
  87. vector<Vec3f> rightCircles;
  88. Mat resultCopy;
  89. result.copyTo(resultCopy);
  90. int distance2Center = 50;
  91. for (size_t i = 0; i < circles.size(); i++)
  92. {
  93. if (circles[i][1] > height)
  94. {
  95. if (abs(circles[i][0] - width / 2) < distance2Center) //圆心横坐标和纵坐标在拼接后图像中心附近
  96. {
  97. if (abs(circles[i][1] - height) < distance2Center)
  98. {
  99. rightCircles.push_back(circles[i]);
  100. if (IsShowWnd)
  101. {
  102. Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  103. int radius = cvRound(circles[i][2]);
  104. // circle center
  105. circle(resultCopy, center, 3, Scalar(0, 0, 0), -1, 5, 0);
  106. // circle outline
  107. circle(resultCopy, center, radius, Scalar(0, 0, 0), 3, 3, 0);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. if (IsShowWnd) {
  114. namedWindow("霍夫圆图");
  115. imshow("霍夫圆图", resultCopy);
  116. waitKey(1);
  117. }
  118. adaptiveThreshold(result, result, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 1);
  119. if (IsShowWnd) {
  120. imshow("二次二值化图", result);
  121. waitKey(1);
  122. }
  123. //int w = result.cols;
  124. //int h = result.rows;
  125. //uchar* uc_pixel;
  126. //for (int row = 0; row < h; row++) {
  127. // uc_pixel = result.data + row * result.step;
  128. // for (int col = 0; col < w; col++) {
  129. // //uc_pixel[0];
  130. // //sum += uc_pixel[0];
  131. // uc_pixel[1] = 255;
  132. // uc_pixel[2] = 255;
  133. // uc_pixel[0] = 255;
  134. // uc_pixel += 3;
  135. // }
  136. //}
  137. vector<int> zeroPositions;
  138. vector<pair<int, int>> rightNumSums;
  139. for (size_t j = 0; j < rightCircles.size(); j++)
  140. {
  141. Point ptCenter(cvRound(rightCircles[j][0]), cvRound(rightCircles[j][1]));
  142. int radius = cvRound(rightCircles[j][2]);
  143. Mat darkMatCopy;
  144. result.copyTo(darkMatCopy);
  145. vector<pair<int, int>> num_sum;
  146. //vector<,>
  147. for (size_t i = 300; i < 421; i++)
  148. {
  149. //Point ptStart(ptCenter.x - radius - 10, ptCenter.y);
  150. Point ptStart1(ptCenter.x + sin(i * CV_PI / 180) * (radius + 15), ptCenter.y - cos(i * CV_PI / 180) * (radius + 15));
  151. Point ptStart2(ptCenter.x + sin(i * CV_PI / 180) * (radius - 35), ptCenter.y - cos(i * CV_PI / 180) * (radius - 35));
  152. line(darkMatCopy, ptStart2, ptStart1, Scalar(0, 0, 0), 3);
  153. if (IsShowWnd)
  154. {
  155. imshow("SemicircularMeter", darkMatCopy);
  156. //Sleep(1000);
  157. waitKey(100);
  158. /*string str;
  159. std::cin >> str;*/
  160. }
  161. int matdis = abs(LineHandle::CalcMatSum(darkMatCopy) - LineHandle::CalcMatSum(result));
  162. /* cout << "像素综合darkMatCopy" << i << ":" << LineHandle::CalcMatSum(darkMatCopy) << endl;
  163. cout << "像素综合result" << i << ":" << LineHandle::CalcMatSum(result) << endl;
  164. cout << "像素综合cha " << i << ":" << matdis << endl;*/
  165. num_sum.push_back(make_pair(i, matdis));
  166. if (num_sum.size() > 3)
  167. {
  168. if (num_sum[num_sum.size() - 3].second - matdis > 20000 && zeroPositions.size() <= j + 1)
  169. {
  170. zeroPositions.push_back(i);
  171. }
  172. }
  173. result.copyTo(darkMatCopy);
  174. }
  175. std::sort(num_sum.begin(), num_sum.end(), LineHandle::ComparePairSecond);
  176. if (num_sum.size() > 0)
  177. {
  178. rightNumSums.push_back(num_sum[0]);
  179. }
  180. }
  181. std::sort(rightNumSums.begin(), rightNumSums.end(), LineHandle::ComparePairSecond);
  182. if (rightNumSums.size() == 0)
  183. {
  184. return 0.01;
  185. }
  186. std::sort(zeroPositions.begin(), zeroPositions.end());
  187. if (zeroPositions.size() == 0)
  188. {
  189. zeroPositions.push_back(310);
  190. }
  191. if (zeroPositions[0] > 330)
  192. {
  193. zeroPositions[0] = 310;
  194. }
  195. float resutValue = (float)(rightNumSums[0].first - zeroPositions[0]) / 30 + 0.01;
  196. if (abs(resutValue) > 3)
  197. {
  198. resutValue = 0.01;
  199. }
  200. return abs(resutValue);
  201. }