Tekst


SUBMITTED BY: Guest

DATE: Jan. 16, 2017, 3:56 a.m.

FORMAT: Text only

SIZE: 8.8 kB

HITS: 1466

  1. #include "opencv2/imgproc/imgproc.hpp"
  2. #include "opencv2/videoio/videoio.hpp"
  3. #include "opencv2/calib3d.hpp"
  4. #include "opencv2/highgui/highgui.hpp"
  5. #include "opencv2/highgui.hpp"
  6. #include <iostream>
  7. #include <fstream>
  8. #include <algorithm>
  9. #include <ctype.h>
  10. using namespace cv;
  11. using namespace std;
  12. int main()
  13. {
  14. int largest_area = 0;
  15. int largest_contour_index = 0;
  16. int a = 0;
  17. cout << "1 ideal\n" << "2 blurred\n" << "3 gradient\n";
  18. cin >> a;
  19. Mat IdealImage;
  20. switch (a)
  21. {
  22. case 1:
  23. {
  24. IdealImage = imread("ideal.png");
  25. break;
  26. }
  27. case 2:
  28. {
  29. IdealImage = imread("blurred.png");
  30. break;
  31. }
  32. case 3:
  33. {
  34. IdealImage = imread("gradient.png");
  35. break;
  36. }
  37. }
  38. int rows = IdealImage.rows;
  39. int cols = IdealImage.cols;
  40. Mat EmptyBoard = imread("ref.png");
  41. Mat GrayImage, ThresholdedImage, ReferenceImage, ContouredImage, IdealImageTransformed, GrayImage1; //Mat dst(IdealImage.row, IdealImage.cols, CV_8UC1, Scalar::all(0));
  42. cvtColor(EmptyBoard, GrayImage, COLOR_BGR2GRAY);
  43. adaptiveThreshold(GrayImage, ThresholdedImage, 100, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, 0);
  44. vector<vector<Point>> contours;
  45. vector<Vec4i> hierarchy;
  46. vector<vector<Point>>approximatedContour;
  47. findContours(ThresholdedImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
  48. for (int i = 0; i < contours.size(); i++) {
  49. double a = contourArea(contours[i], false);
  50. if (a > largest_area)
  51. {
  52. largest_area = a;
  53. largest_contour_index = i;
  54. }
  55. }
  56. Scalar color(255, 255, 255); //drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy, 0);
  57. vector<vector<Point>>largest_polynomial(1);
  58. approxPolyDP(Mat(contours[largest_contour_index]), largest_polynomial[0], 10, true);
  59. Rect boundRect = boundingRect(contours[largest_contour_index]);
  60. Point ver1 = largest_polynomial[0][0];
  61. Point ver2 = largest_polynomial[0][1];
  62. Point ver3 = largest_polynomial[0][2];
  63. Point ver4 = largest_polynomial[0][3];
  64. polylines(IdealImage, largest_polynomial[0], true, color, 1, 8, 0);
  65. vector<Point2f>Quad;
  66. vector<Point2f>Square;
  67. Quad.push_back(Point2f(largest_polynomial[0][0].x, largest_polynomial[0][0].y));
  68. Quad.push_back(Point2f(largest_polynomial[0][1].x, largest_polynomial[0][1].y));
  69. Quad.push_back(Point2f(largest_polynomial[0][3].x, largest_polynomial[0][3].y));
  70. Quad.push_back(Point2f(largest_polynomial[0][2].x, largest_polynomial[0][2].y));
  71. Square.push_back(Point2f(boundRect.x, boundRect.y));
  72. Square.push_back(Point2f(boundRect.x, boundRect.y + boundRect.height));
  73. Square.push_back(Point2f(boundRect.x + boundRect.width, boundRect.y));
  74. Square.push_back(Point2f(boundRect.x + boundRect.width, boundRect.y + boundRect.height));
  75. Mat TransformedMatrix = getPerspectiveTransform(Quad, Square);
  76. warpPerspective(IdealImage, IdealImageTransformed, TransformedMatrix, IdealImage.size());
  77. Mat WarpedPerspective = IdealImageTransformed(boundRect);
  78. Mat grayscaled, binarized;
  79. cvtColor(WarpedPerspective, grayscaled, CV_BGR2GRAY);
  80. threshold(grayscaled, binarized, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
  81. Mat tak;
  82. if (a == 1) { adaptiveThreshold(grayscaled, tak, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 31, 28); }
  83. if (a == 2) { adaptiveThreshold(grayscaled, tak, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 27, 10); }
  84. if (a == 3) { adaptiveThreshold(grayscaled, tak, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 41, 24); }
  85. //imshow("tak", tak);
  86. vector<vector<Point>>lettercontours;
  87. vector<Vec4i> hierarchyoflet;
  88. findContours(tak, lettercontours, hierarchyoflet, CV_RETR_LIST, CV_CHAIN_APPROX_TC89_L1);
  89. int idx = 0;
  90. Scalar color2(255, 0, 0);
  91. //vector<Point>contour;
  92. double tablica[100][7];
  93. int b = 0;
  94. //fstream humom("D", ios::out);
  95. int c = 0;
  96. double hu[7];
  97. fstream humom("humombase.txt", ios::out);
  98. fstream dupa("a.txt", ios::out);
  99. // fstream chuj("a.txt", ios::out);
  100. vector<Point> koordynat;
  101. vector<Point> kontur = lettercontours.at(4);
  102. for (int i = 0; idx >= 0; idx = hierarchyoflet[idx][0], i++)
  103. {
  104. //int xparam = lettercontours[i][2].x;
  105. Point koordynat = kontur.at(4);
  106. int b = koordynat.x;
  107. Moments lettermoments;
  108. double area = contourArea(lettercontours[i]);
  109. if (area >= 162 && area <= 500)
  110. {
  111. drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3);
  112. lettermoments = moments(lettercontours[i]);
  113. HuMoments(lettermoments, hu);
  114. humom << hu[0] << " " << hu[1] << " " << hu[2] << " " << hu[3] << " " << hu[4] << " " << hu[5] << " " << hu[6] << "\n";
  115. if (hu[0] > 0.35 && hu[0] < 0.36 && hu[1]> 0.047 && hu[1] < 0.054 && hu[2]> 3.1336e-05 && hu[2] < 0.00033) { dupa << "M"; }
  116. //0.357834 0.0528427 0.000212741 1.52876e-05 - 2.2733e-10 - 5.24759e-07 - 8.41673e-10
  117. //0.351721 0.0471768 0.000123528 2.02553e-05 8.49384e-10 3.05756e-06 - 5.52353e-10
  118. //0.353218 0.0524782 6.62608e-05 1.78519e-05 6.11112e-10 2.93039e-06 5.92793e-11
  119. if (hu[0] > 0.25 && hu[0] < 0.254 && hu[1]> 0.006207 && hu[1] < 0.006208) { dupa << "R"; }
  120. //0.253799 0.00620756 8.20748e-05 0.000118764 - 4.62818e-09 7.75944e-06 - 1.07735e-08
  121. if (hu[0] > 0.209 && hu[0] < 0.22 && hu[1]> 0.0026 && hu[1] < 0.0028 && hu[2]> 0.0055 && hu[2] < 0.0061) { dupa << "A"; }
  122. //0.211435 0.00277891 0.00559733 0.000347034 - 4.21318e-07 - 1.80716e-05 2.37545e-07
  123. //0.209258 0.00267701 0.00556412 0.000330352 -3.46123e-07 -1.49303e-05 2.84248e-07
  124. //0.213268 0.00270815 0.00609144 0.000331404 -4.5811e-07 -1.71538e-05 1.08849e-07
  125. if (hu[0] > 0.40 && hu[0] < 0.44 && hu[1]> 0.004 && hu[1] < 0.064 && hu[2]> 0.0004 && hu[2] < 0.0035) { dupa << "O"; }
  126. //0.401631 0.0431767 0.000441394 9.52397e-05 -1.7502e-08 -1.8505e-05 8.65982e-09 340 380
  127. //0.439863 0.0633895 0.00345674 0.000441106 - 4.53871e-07 - 0.000101384 - 3.0114e-07
  128. if (hu[0] > 0.265 && hu[0] < 0.281 && hu[1]> 0.0046 && hu[1] < 0.0103 && hu[2]> 0.00027 && hu[2]< 0.0041) { dupa << "S"; }
  129. //0.26567 0.00460785 0.00027196 6.60365e-06 2.00793e-10 4.03965e-07 1.94934e-10 280 291
  130. //0.275741 0.00979134 0.00274575 4.91276e-05 -8.69577e-09 -8.15549e-07 -1.58098e-08
  131. //0.280532 0.0102846 0.00407578 0.000111535 -3.8332e-08 1.06307e-05 6.46982e-08
  132. if (hu[0] > 0.24255 && hu[0] < 0.24256 && hu[1]> 0.0176 && hu[1] < 0.0177 && hu[2] > 0.0034656 && hu[2] < 0.00346565) { dupa << "L"; }
  133. // 0.242559 0.017601 0.00346563 0.000493662 2.61852e-07 - 3.77269e-05 5.90231e-07
  134. if (hu[0] > 0.316 && hu[0] < 0.317 && hu[1]> 0.027 && hu[1] < 0.0276 && hu[2] > 0.01 && hu[2] < 0.011) { dupa << "P"; }
  135. //0.316841 0.0275557 0.0107918 0.00148656 5.94034e-06 0.000232005 -4.05691e-07
  136. if (hu[0] > 0.22 && hu[0] < 0.239 && hu[1]> 0.014 && hu[1] < 0.01461 && hu[2] > 0.0019 && hu[2] < 0.0042) { dupa << "T"; }
  137. //0.238834 0.0143339 0.00419507 6.10304e-05 -3.08808e-08 -4.80126e-06 2.99048e-11 200 210
  138. //0.227097 0.0146042 0.00194496 9.9943e-05 3.05285e-08 -5.88191e-06 3.17749e-08 180 200
  139. if (hu[0]> 0.24 && hu[0] < 0.261 && hu[1]> 0.0035 && hu[1] < 0.008 && hu[2] > 0.0003 && hu[2] < 0.0015) { dupa << "E"; }
  140. //0.251685 0.00774301 0.000544931 0.000897187 - 5.07325e-07 - 7.49691e-05 - 3.69002e-07 215 225
  141. //0.243329 0.00388335 0.000332522 0.000625236 2.30959e-07 -3.14723e-05 1.67128e-07 225 245
  142. //0.260123 0.00573609 0.0013481 0.00109925 -1.21903e-06 -8.05672e-05 5.5191e-07 275 280
  143. if (hu[0] > 0.228304 && hu[0] < 0.228306 && hu[1]> 0.0257696 && hu[1] < 0.0257698 && hu[2] > 4.586e-05 && hu[2] < 4.5869e-05) { dupa << "I"; }
  144. //0.228305 0.0257697 4.58629e-05 4.27387e-06 - 4.46381e-11 - 5.51293e-07 3.98471e-11
  145. }
  146. //waitkey(0);
  147. //lettercom
  148. //tak = target.clne;
  149. //{ //mu[i] = moments(hierarchyoflet[i], true);
  150. // void HuMoments(const Moments& moments, double hu[7]);
  151. //
  152. //}
  153. //Moments mom = moments(contourArea(lettercontours[i]));
  154. //vector<Moments> mom(hierarchyoflet.size());
  155. // double area = contourArea(lettercontours[i]);
  156. //if (a == 1 && area > 100 && area < 500) { drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3); }
  157. //if (a == 2 && area > 120 && area < 500) { drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3); }
  158. //if (a == 3 && area > 100 && area < 490) { drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3); }
  159. //imshow("tak", tak);
  160. //waitKey(1);
  161. /*b = b + 1;
  162. if (b > 200)
  163. {
  164. imshow("IdealImage", IdealImage);
  165. imshow("done", binarized);
  166. imshow("tak", tak);
  167. waitKey(1);
  168. }*/
  169. }
  170. imshow("tak", tak);
  171. //imshow("IdealImage", IdealImage);
  172. //imshow("done", binarized);
  173. //imshow("tak", tak);
  174. waitKey();
  175. return 0;
  176. }

comments powered by Disqus