DoublePointerCountALGO原版.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "DoublePointerCountALGO.h"
  2. #include "LineHandle.h"
  3. using namespace std;
  4. using namespace cv;
  5. int DoublePointerCountALGO::GetResult(cv::Mat sourceMat, bool IsShowWnd)
  6. {
  7. Mat mat;
  8. sourceMat.copyTo(mat);
  9. int matCols = 500;
  10. Size size(500, 420);
  11. resize(mat, mat, size, (float)matCols / mat.cols, (float)matCols / mat.cols);
  12. int width = mat.cols;// .width;
  13. int height = mat.rows;
  14. Mat grayMat;
  15. cvtColor(mat, grayMat, COLOR_BGR2GRAY); // 转换为灰度图
  16. //Mat thMat;
  17. adaptiveThreshold(grayMat, grayMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 2);
  18. if (IsShowWnd) {
  19. imshow("二值化图adaptiveThreshold", grayMat);
  20. }
  21. Mat biMat;
  22. bilateralFilter(grayMat, biMat, 9, 50, 50);
  23. if (IsShowWnd) {
  24. imshow("滤波去噪图", biMat);
  25. }
  26. //Mat gaussMat;
  27. GaussianBlur(biMat, grayMat, Size(3, 3), 0, 0);
  28. if (IsShowWnd) {
  29. imshow("高斯模糊图", grayMat);
  30. }
  31. medianBlur(grayMat, grayMat, 3);//中值滤波
  32. if (IsShowWnd) {
  33. imshow("中值滤波图", grayMat);
  34. }
  35. adaptiveThreshold(grayMat, grayMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 2);
  36. if (IsShowWnd) {
  37. imshow("二次二值化图", grayMat);
  38. }
  39. Mat cannyMat;
  40. int g_nthreshold = 39;
  41. int c_cen = min(height, width);
  42. int w = grayMat.cols;
  43. int h = grayMat.rows;
  44. uchar* uc_pixel;
  45. for (int row = 0; row < h; row++) {
  46. uc_pixel = grayMat.data + row * grayMat.step;
  47. for (int col = 0; col < w; col++) {
  48. uc_pixel[0] = 255 - uc_pixel[0];
  49. uc_pixel[1] = 255 - uc_pixel[1];
  50. uc_pixel[2] = 255 - uc_pixel[2];
  51. uc_pixel += grayMat.channels();
  52. }
  53. }
  54. if (IsShowWnd) {
  55. imshow("反色图", grayMat);
  56. }
  57. //LineHandle lineHandle;
  58. Vec4i lineX(0, height / 2, width, height / 2);
  59. Vec4i lineY(width / 2, 0, width / 2, height);
  60. /* line(grayMat, Point(0, height / 2), Point(width, height / 2), Scalar(0, 0, 0), 1);
  61. line(grayMat, Point(width / 2, 0), Point(width / 2, height), Scalar(0, 0, 0), 1);*/
  62. vector<Point2f> tu1;
  63. //Mat mat(,)
  64. Mat darkMat(height, width, CV_8UC3, Scalar(0, 0, 0));
  65. vector<Vec4i> mylines;
  66. HoughLinesP(grayMat, mylines, 1, CV_PI / 180, 100, min(height / 6, width / 6), 3);
  67. for (size_t i = 0; i < mylines.size(); i++)
  68. {
  69. Vec4i cho_l = mylines[i];
  70. Point2f crossXPoints;
  71. LineHandle::crossPointsOfLines(cho_l, lineX, crossXPoints);
  72. Point2f crossYPoints;
  73. LineHandle::crossPointsOfLines(cho_l, lineY, crossYPoints);
  74. if (abs(crossXPoints.x - width / 2) < width / 6 && abs(crossXPoints.y - height / 2) < height / 6)
  75. {
  76. line(darkMat, Point(cho_l[0], cho_l[1]), Point(cho_l[2], cho_l[3]), Scalar(255, 0, 0), 10);
  77. /* circle(grayMat, Point(cho_l[2], cho_l[3]), 2, cv::Scalar(255, 0, 0), -1, cv::FILLED);
  78. circle(grayMat, Point(cho_l[0], cho_l[1]), 2, cv::Scalar(0, 255, 255), -1, cv::FILLED);*/
  79. Point2f pt1(cho_l[2] - cho_l[0], cho_l[3] - cho_l[1]);
  80. Point2f pt2(1, 0);
  81. float theta = atan2(pt1.x, pt1.y) - atan2(pt2.x, pt2.y);
  82. if (theta > CV_PI)
  83. theta -= 2 * CV_PI;
  84. if (theta < -CV_PI)
  85. theta += 2 * CV_PI;
  86. theta = theta * 180.0 / CV_PI;
  87. float a = pow((cho_l[2] - width / 2), 2) + pow((cho_l[3] - height / 2), 2);
  88. tu1.push_back(Point2f(sqrtf(a), theta));
  89. }
  90. }
  91. /*vector< Point2f> sorttu;
  92. sort(tu1, sorttu,);*/
  93. Point ptStart(width / 2, 0);
  94. Point ptCenter(width / 2, height / 2);
  95. Mat darkMatCopy;
  96. darkMat.copyTo(darkMatCopy);
  97. vector<pair<int, int>> num_sum;
  98. //vector<,>
  99. for (size_t i = 0; i < 360; i++)
  100. {
  101. Point ptStart1(ptCenter.x + sin(i * 1 * CV_PI / 180) * (c_cen / 2), ptCenter.y - cos(i * 1 * CV_PI / 180) * (c_cen / 2));
  102. Point ptStart2(ptCenter.x + sin(i * 1 * CV_PI / 180) * (c_cen / 2 - 100), ptCenter.y - cos(i * 1 * CV_PI / 180) * (c_cen / 2 - 100));
  103. line(darkMatCopy, ptStart2, ptStart1, Scalar(255, 0, 0), 10);
  104. if (IsShowWnd)
  105. {
  106. imshow("test", darkMatCopy);
  107. waitKey(10);
  108. }
  109. int matdis = LineHandle::CalcMatSum(darkMatCopy) - LineHandle::CalcMatSum(darkMat);
  110. //cout << "像素综合cha " << i << ":" << matdis << endl;
  111. int angle = i;
  112. if (angle > 342)
  113. {
  114. angle = 360 - angle;
  115. }
  116. num_sum.push_back(make_pair(angle, matdis));
  117. darkMat.copyTo(darkMatCopy);
  118. }
  119. sort(num_sum.begin(), num_sum.end(), LineHandle::ComparePairSecond);
  120. int resultValue = 0;
  121. int secondPoint = -1;
  122. for (size_t i = 0; i < num_sum.size(); i++)
  123. {
  124. if (abs(num_sum[0].first - num_sum[i].first) > 18 && abs(num_sum[0].first - num_sum[i].first) < 342) //360° 0°附近特殊处理
  125. {
  126. if (num_sum[i].second < 3 * (num_sum[num_sum.size() - 1].second) / 4)
  127. {
  128. secondPoint = num_sum[i].first;
  129. break;
  130. }
  131. }
  132. }
  133. if (secondPoint != -1)
  134. {
  135. int seondScore = num_sum[0].first / 36;
  136. if (num_sum[0].first % 36 > 18)
  137. {
  138. seondScore += 1;
  139. }
  140. int firstScore = secondPoint / 36;
  141. if (secondPoint % 36 > 18)
  142. {
  143. firstScore += 1;
  144. }
  145. resultValue = firstScore * 10 + seondScore;
  146. }
  147. else if (secondPoint == -1)
  148. {
  149. int seondScore = num_sum[0].first / 36;
  150. if (num_sum[0].first % 36 > 18)
  151. {
  152. seondScore += 1;
  153. }
  154. resultValue = seondScore * 10 + seondScore;
  155. }
  156. return resultValue;
  157. }