DoublePointerAlgorithm.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "DoublePointerAlgorithm.h"
  2. #include <iostream>
  3. #include <SegDetector.h>
  4. #include <NanoDetector.h>
  5. #include <opencv2\imgproc\types_c.h>
  6. #include <opencv2/imgproc/imgproc_c.h>
  7. #include "LineHandle.h"
  8. #include<numeric>
  9. #include<windows.h>
  10. using namespace std;
  11. using namespace cv;
  12. int DoublePointerAlgorithm::GetResult(cv::Mat sourceMat, bool IsShowWnd )
  13. {
  14. Mat mat;
  15. sourceMat.copyTo(mat);
  16. int matCols = 500;
  17. Size size;
  18. resize(mat, mat, size, (float)matCols/mat.cols , (float)matCols/mat.cols );
  19. int width = mat.cols;// .width;
  20. int height = mat.rows;
  21. Mat grayMat;
  22. cvtColor(mat, grayMat, COLOR_BGR2GRAY); // 转换为灰度图
  23. //Mat thMat;
  24. adaptiveThreshold(grayMat, grayMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 2);
  25. if (IsShowWnd) {
  26. imshow("二值化图adaptiveThreshold" , grayMat);
  27. }
  28. Mat biMat;
  29. bilateralFilter(grayMat, biMat, 9, 50, 50);
  30. if (IsShowWnd) {
  31. imshow("滤波去噪图", biMat);
  32. }
  33. //Mat gaussMat;
  34. GaussianBlur(biMat, grayMat, Size(3, 3), 0, 0);
  35. if (IsShowWnd) {
  36. imshow("高斯模糊图", grayMat);
  37. }
  38. medianBlur(grayMat, grayMat, 3);//中值滤波
  39. if (IsShowWnd) {
  40. imshow("中值滤波图", grayMat);
  41. }
  42. adaptiveThreshold(grayMat, grayMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 2);
  43. if (IsShowWnd) {
  44. imshow("二次二值化图", grayMat);
  45. }
  46. Mat cannyMat;
  47. vector<Vec3f> circles;
  48. vector<Vec4i> mylines;
  49. //Canny(grayMat, cannyMat, 50, 100);//边缘检测
  50. //if (IsShowWnd) {
  51. // imshow("边缘检测图", cannyMat);
  52. // waitKey(1);
  53. //}
  54. // HoughCircles(cannyMat, circles, HOUGH_GRADIENT, 10, 1, 100, 100, 0,20);
  55. //cvtColor(cannyMat, dst_img, CV_GRAY2BGR);
  56. //HoughLines(,)
  57. //vector<Vec4i> newLises;
  58. //Mat resultCopy;
  59. //grayMat.copyTo(resultCopy);
  60. //for (size_t i = 0; i < circles.size(); i++)
  61. //{
  62. // if (abs(circles[i][0] - width / 2) < 50 && abs(circles[i][1] - height / 2) < 50)
  63. // {
  64. // Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  65. // int radius = cvRound(circles[i][2]);
  66. // // circle center
  67. // circle(resultCopy, center, 3, Scalar(0, 0, 0), -1, 5, 0);
  68. // // circle outline
  69. // circle(resultCopy, center, radius, Scalar(0, 0, 0), 3, 3, 0);
  70. // if (IsShowWnd)
  71. // {
  72. // imshow("test234", resultCopy);
  73. // waitKey(300);
  74. // }
  75. // }
  76. //}
  77. int g_nthreshold = 39;
  78. int c_cen = min(height, width);
  79. int w = grayMat.cols;
  80. int h = grayMat.rows;
  81. uchar* uc_pixel;
  82. for (int row = 0; row < h; row++) {
  83. uc_pixel = grayMat.data + row * grayMat.step;
  84. for (int col = 0; col < w; col++) {
  85. uc_pixel[0] = 255 - uc_pixel[0];
  86. uc_pixel[1] = 255 - uc_pixel[1];
  87. uc_pixel[2] = 255 - uc_pixel[2];
  88. uc_pixel += grayMat.channels();
  89. }
  90. }
  91. if (IsShowWnd) {
  92. imshow("反色图", grayMat);
  93. }
  94. //des uc_pixel
  95. HoughLinesP(grayMat, mylines, 1, CV_PI / 180, 100, 100, 3);
  96. LineHandle lineHandle;
  97. Vec4i lineX(0, height / 2, width, height / 2);
  98. Vec4i lineY(width / 2, 0, width / 2, height);
  99. /* line(grayMat, Point(0, height / 2), Point(width, height / 2), Scalar(0, 0, 0), 1);
  100. line(grayMat, Point(width / 2, 0), Point(width / 2, height), Scalar(0, 0, 0), 1);*/
  101. vector<Point2f> tu1;
  102. //Mat mat(,)
  103. Mat darkMat(height, width, CV_8UC3, Scalar(0, 0, 0));
  104. for (size_t i = 0; i < mylines.size(); i++)
  105. {
  106. Vec4i cho_l = mylines[i];
  107. // line(grayMat, Point(cho_l[0], cho_l[1]), Point(cho_l[2], cho_l[3]), Scalar(0, 0, 0), 1);
  108. Point2f crossPoints;
  109. lineHandle.crossPointsOfLines(cho_l, lineX, crossPoints);
  110. //cv::circle(grayMat, crossPoints, 2, cv::Scalar(0, 0, 255), -1, cv::FILLED);
  111. if (abs(crossPoints.x - width / 2) < 50)
  112. {
  113. line(darkMat, Point(cho_l[0], cho_l[1]), Point(cho_l[2], cho_l[3]), Scalar(255, 0, 0), 10);
  114. /* circle(grayMat, Point(cho_l[2], cho_l[3]), 2, cv::Scalar(255, 0, 0), -1, cv::FILLED);
  115. circle(grayMat, Point(cho_l[0], cho_l[1]), 2, cv::Scalar(0, 255, 255), -1, cv::FILLED);*/
  116. Point2f pt1(cho_l[2] - cho_l[0], cho_l[3] - cho_l[1]);
  117. Point2f pt2(1, 0);
  118. float theta = atan2(pt1.x, pt1.y) - atan2(pt2.x, pt2.y);
  119. if (theta > CV_PI)
  120. theta -= 2 * CV_PI;
  121. if (theta < -CV_PI)
  122. theta += 2 * CV_PI;
  123. theta = theta * 180.0 / CV_PI;
  124. float a = pow((cho_l[2] - width / 2), 2) + pow((cho_l[3] - height / 2), 2);
  125. tu1.push_back(Point2f(sqrtf(a), theta));
  126. }
  127. }
  128. /*vector< Point2f> sorttu;
  129. sort(tu1, sorttu,);*/
  130. Point ptStart(width / 2, 0);
  131. Point ptCenter(width / 2, height / 2);
  132. Mat darkMatCopy;
  133. darkMat.copyTo(darkMatCopy);
  134. vector<pair<int, int>> num_sum;
  135. //vector<,>
  136. for (size_t i = 0; i <360; i++)
  137. {
  138. Point ptStart1(ptCenter.x + sin(i * 1 * CV_PI / 180) * (c_cen / 2), ptCenter.y - cos(i * 1 * CV_PI / 180) * (c_cen / 2));
  139. Point ptStart2(ptCenter.x + sin(i * 1* CV_PI / 180) * (c_cen / 2 - 80), ptCenter.y - cos(i * 1 * CV_PI / 180) * (c_cen / 2 - 80));
  140. line(darkMatCopy, ptStart2, ptStart1, Scalar(255, 0, 0), 10);
  141. if (IsShowWnd)
  142. {
  143. imshow("test", darkMatCopy);
  144. waitKey(10);
  145. }
  146. int matdis = LineHandle::CalcMatSum(darkMatCopy) - LineHandle::CalcMatSum(darkMat);
  147. //cout << "像素综合cha " << i << ":" << matdis << endl;
  148. int angle = i;
  149. if (angle>342)
  150. {
  151. angle = 360 - angle;
  152. }
  153. num_sum.push_back(make_pair(angle, matdis));
  154. darkMat.copyTo(darkMatCopy);
  155. }
  156. sort(num_sum.begin(), num_sum.end(), LineHandle::ComparePairSecond);
  157. int resultValue = 0;
  158. int secondPoint = -1;
  159. for (size_t i = 0; i < num_sum.size(); i++)
  160. {
  161. if (abs(num_sum[0].first - num_sum[i].first) > 20)
  162. {
  163. if (i<20)
  164. {
  165. secondPoint = num_sum[i].first;
  166. break;
  167. }
  168. else if (num_sum[i].second < 80*10*255)
  169. {
  170. secondPoint = num_sum[i].first;
  171. break;
  172. }
  173. }
  174. /*if (abs(num_sum[i].first - 360)<18 || abs(num_sum[i].first - 0) < 18)
  175. {
  176. }*/
  177. }
  178. if (secondPoint!=-1)
  179. {
  180. int seondScore = num_sum[0].first / 36;
  181. if (num_sum[0].first % 36>18)
  182. {
  183. seondScore += 1;
  184. }
  185. int firstScore = secondPoint / 36;
  186. if (secondPoint % 36 > 18)
  187. {
  188. firstScore += 1;
  189. }
  190. resultValue = firstScore * 10 + seondScore;
  191. }
  192. else if (secondPoint == -1)
  193. {
  194. int seondScore = num_sum[0].first / 36;
  195. if (num_sum[0].first % 36 > 18)
  196. {
  197. seondScore += 1;
  198. }
  199. resultValue = seondScore * 10 + seondScore;
  200. }
  201. cout << "结果 :" << resultValue << endl;
  202. //if (5 * (num_sum[0].second) < num_sum[1].second)
  203. //{
  204. // resultValue = num_sum[0].first + 10 * num_sum[0].first;
  205. // cout << "结果 :" << resultValue << endl;
  206. //}
  207. //else
  208. //{
  209. // resultValue = num_sum[0].first + 10 * num_sum[1].first;
  210. // cout << "结果 :" << resultValue << endl;
  211. //}
  212. //if (IsShowWnd)
  213. //{
  214. // //waitKey(1000*10);
  215. // //destroyAllWindows();
  216. //}
  217. return resultValue;
  218. }