Advertisement
DanilaG

Untitled

Sep 27th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. int isFigure(Mat img, figure_t figure) {
  2.     Mat hsv;
  3.     vector<vector<Point>> contours;
  4.     doMasc(img, &hsv, figure, AllRealFigures);
  5.     findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  6.     bool ans = 0;
  7.     for (int j = 0; j < contours.size(); j++)
  8.         ans = ans || (MAX_FIGURE_SQUARE < contourArea(contours[j]));
  9.     return ans;
  10. }
  11.  
  12. figure_t GetFigure(Mat img) {
  13.     Mat hsv;
  14.     vector<vector<Point>> contours;
  15.     figure_t maxFoundFigure = Free_f;
  16.     int buffDistance;
  17.     Rect FigureRect;
  18.     Point center = ((img.size().width / 2.0), (img.size().height / 2.0));
  19.     int distanceFigure = 10000000;//distance2Center((img.size().width), (img.size().height), center);
  20.     for (int i = 0; i < NUMBER_REAL_FIGURE; i++) {
  21.         doMasc(img, &hsv, (figure_t)(i + 1), AllRealFigures);
  22.         findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  23.         //waitKey(0);
  24.         for (int j = 0; j < contours.size(); j++) {
  25.             if (MAX_FIGURE_SQUARE < contourArea(contours[j])) {
  26.                 Rect rect = boundingRect(contours[j]);
  27.                 buffDistance = distance2Center(rect.x + (rect.width / 2.0), rect.y + (rect.height / 2.0), center);
  28.                 if (buffDistance < distanceFigure) {
  29.                     if (((i == 2) || (i == 5))) { //j or i
  30.                         RotatedRect r_rect = minAreaRect(contours[j]);
  31.                         Size size;
  32.                         size.width = min(r_rect.size.height, r_rect.size.width);
  33.                         size.height = max(r_rect.size.height, r_rect.size.width);
  34.                         if (((i == 2) && (size.height / (double)size.width) < 3.2) || ((i == 5) && (size.height / (double)size.width) >= 3.2)) {
  35.                             distanceFigure = buffDistance;
  36.                             maxFoundFigure = (figure_t)(i + 1);
  37.                             FigureRect = rect;
  38.                         }
  39.                         continue;
  40.                     }
  41.                     distanceFigure = buffDistance;
  42.                     maxFoundFigure = (figure_t)(i + 1);
  43.                     FigureRect = rect;
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     return maxFoundFigure;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement