Advertisement
DanilaG

Untitled

Sep 23rd, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. Point figurePos(Mat img, figure_t figure);
  2.  
  3. Point figurePos(Mat img, figure_t figure) {
  4.     Mat hsv;
  5.     doMasc(img, &hsv, figure, AllRealFigures);
  6.     vector<vector<Point>> contours;
  7.     findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  8.     int squareMax = 500, squareBuff;
  9.     int index = -1;
  10.     for (int j = 0; j < contours.size(); j++) {
  11.         squareBuff = contourArea(contours[j]);
  12.         if (squareBuff > squareMax) {
  13.             squareMax = squareBuff;
  14.             index = j;
  15.         }
  16.     }
  17.     if (index == -1) {
  18.         return { 0, 0 };
  19.     }
  20.     RotatedRect rect = minAreaRect(contours[index]);
  21.     Point2f vertices[4];
  22.     rect.points(vertices);
  23.     Point2f rightPoint = vertices[0];
  24.     for (int i = 1; i < 4; i++) {
  25.         if (rightPoint.x < vertices[i].x) {
  26.             rightPoint = vertices[i];
  27.         }
  28.     }
  29.     rightPoint.y = rect.center.y;
  30.     circle(img, rightPoint, 1, Scalar(0, 255, 0), 3);
  31.     return rightPoint;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement