Advertisement
DanilaG

.C

Aug 21st, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.33 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "RobotOCV.h"
  3.  
  4. # define PI (3.14159265358979323846)
  5.  
  6. #define MAX_FIGURE_SQUARE (2000)
  7.  
  8. using namespace cv;
  9.  
  10. Mat imgHSVTest;
  11. int hMinTest = 0, hMaxTest = 255, sMinTest = 0, sMaxTest = 255, vMinTest = 0, vMaxTest = 255;
  12. Mat imgCannyTest;
  13. int CannyTestSize;
  14. Mat imgHoughCirclesTest;
  15. int min_distTest = 14, param1Test = 210, param2Test = 11, min_radiusTest = 0, max_radiusTest = 21;
  16.  
  17. void CalibrationChessboard(Mat img) {
  18.     namedWindow("Chessboard", WINDOW_NORMAL);
  19.     resizeWindow("Chessboard", 600, 600);
  20.     Size patternsize(8, 6);
  21.     Mat gray;
  22.     vector<Point2f> corners;
  23.     cvtColor(img, gray, CV_BGR2GRAY);
  24.     bool patternfound = findChessboardCorners(gray, patternsize, corners,
  25.         CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE
  26.         + CALIB_CB_FAST_CHECK);
  27.     if (patternfound)
  28.         cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1),
  29.             TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
  30.     drawChessboardCorners(img, patternsize, Mat(corners), patternfound);
  31.     imshow("Chessboard", img);
  32.  
  33.     int numSquares = patternsize.width * patternsize.height;
  34.     vector<vector<Point3f>> object_points;
  35.     vector<vector<Point2f>> image_points;
  36.     vector<Point3f> objectPoints;
  37.     double a = 0.1;
  38.     for (int y = 0; y < patternsize.height; ++y) {
  39.         for (int x = 0; x < patternsize.width; ++x)
  40.             objectPoints.push_back(Point3f(x*a, y*a, 0));
  41.     }
  42.     for (int n = 0; n<numSquares; ++n)
  43.         object_points.push_back(objectPoints);
  44.  
  45.     Mat intrinsic = Mat(3, 3, CV_32FC1);
  46.     Mat distCoeffs;
  47.     vector<Mat> rvecs;
  48.     vector<Mat> tvecs;
  49.  
  50.     for (int n = 0; n < numSquares; ++n)
  51.         image_points.push_back(corners);
  52.  
  53.     intrinsic.ptr<float>(0)[0] = 1;
  54.     intrinsic.ptr<float>(1)[1] = 1;
  55.     calibrateCamera(object_points, image_points, img.size(), intrinsic, distCoeffs, rvecs, tvecs);
  56.  
  57.     Mat imageUndistorted;
  58.  
  59.     undistort(img, imageUndistorted, intrinsic, distCoeffs);
  60.  
  61.     imshow("win1", img);
  62.     imshow("win2", imageUndistorted);
  63. }
  64.  
  65. void OnChangeTestTrackBar(int i) {
  66.     Mat out;
  67.     if (hMinTest > hMaxTest) {
  68.         Mat buff;
  69.         inRange(imgHSVTest, Scalar(hMinTest, sMinTest, vMinTest), Scalar(255, sMaxTest, vMaxTest), buff);
  70.         inRange(imgHSVTest, Scalar(0, sMinTest, vMinTest), Scalar(hMaxTest, sMaxTest, vMaxTest), out);
  71.         out = out | buff;
  72.     }
  73.     else {
  74.         inRange(imgHSVTest, Scalar(hMinTest, sMinTest, vMinTest), Scalar(hMaxTest, sMaxTest, vMaxTest), out);
  75.     }
  76.     imshow("TestPhoto", out);
  77. }
  78.  
  79. void TestPhoto(char *name) {
  80.     Mat img = imread(name);
  81.     namedWindow("TestPhoto", WINDOW_NORMAL);
  82.     resizeWindow("TestPhoto", 600, 600);
  83.     cvtColor(img, imgHSVTest, CV_BGR2HSV);
  84.     cvCreateTrackbar("HMin", "TestPhoto", &hMinTest, 255, OnChangeTestTrackBar);
  85.     cvCreateTrackbar("hMax", "TestPhoto", &hMaxTest, 255, OnChangeTestTrackBar);
  86.     cvCreateTrackbar("sMin", "TestPhoto", &sMinTest, 255, OnChangeTestTrackBar);
  87.     cvCreateTrackbar("sMax", "TestPhoto", &sMaxTest, 255, OnChangeTestTrackBar);
  88.     cvCreateTrackbar("vMin", "TestPhoto", &vMinTest, 255, OnChangeTestTrackBar);
  89.     cvCreateTrackbar("vMax", "TestPhoto", &vMaxTest, 255, OnChangeTestTrackBar);
  90.     namedWindow("Photo", WINDOW_NORMAL);
  91.     resizeWindow("Photo", 600, 600);
  92.     imshow("Photo", img);
  93.     OnChangeTestTrackBar(0);
  94. }
  95.  
  96. void OnChangeCannyTest(int p) {
  97.     Mat out;
  98.     Canny(imgCannyTest, out, CannyTestSize /2, CannyTestSize, 3);
  99.     imshow("CannyTest", out);
  100. }
  101.  
  102. void CannyTest(Mat img) {
  103.     namedWindow("CannyTest", WINDOW_NORMAL);
  104.     resizeWindow("CannyTest", 600, 600);
  105.     imgCannyTest = img;
  106.     cvCreateTrackbar("Canny", "CannyTest", &CannyTestSize, 255, OnChangeCannyTest);
  107.     OnChangeCannyTest(0);
  108. }
  109.  
  110. void OnChangeHoughCirclesTest(int p) {
  111.     vector<Vec3f> circles;
  112.     Mat out = imgHoughCirclesTest.clone();
  113.     HoughCircles(out, circles, CV_HOUGH_GRADIENT, 1, min_distTest, param1Test, param2Test, min_radiusTest, max_radiusTest);
  114.     //HoughCircles(out, circles, CV_HOUGH_GRADIENT, 3, 10, 255, 20, 0, 200);
  115.     for (int i = 0; i < circles.size(); i++) {
  116.         Vec3i c = circles[i];
  117.         Point center = Point(c[0], c[1]);
  118.         // circle center
  119.         circle(out, center, 3, Scalar(255, 255, 255), -1, 8, 0);
  120.     }
  121.     imshow("HoughCirclesTest", out);
  122. }
  123.  
  124. void HoughCirclesTest(Mat img) {
  125.     namedWindow("HoughCirclesTest", WINDOW_NORMAL);
  126.     resizeWindow("HoughCirclesTest", 600, 600);
  127.     imgHoughCirclesTest = img;
  128.     cvCreateTrackbar("min_dist", "HoughCirclesTest", &min_distTest, 255, OnChangeHoughCirclesTest);
  129.     cvCreateTrackbar("param1", "HoughCirclesTest", &param1Test, 255, OnChangeHoughCirclesTest);
  130.     cvCreateTrackbar("param2", "HoughCirclesTest", &param2Test, 255, OnChangeHoughCirclesTest);
  131.     cvCreateTrackbar("min_radius", "HoughCirclesTest", &min_radiusTest, 255, OnChangeHoughCirclesTest);
  132.     cvCreateTrackbar("max_radius", "HoughCirclesTest", &max_radiusTest, 255, OnChangeHoughCirclesTest);
  133.     OnChangeHoughCirclesTest(0);
  134. }
  135.  
  136.  
  137. int distance2Center(int x, int y, Point center) {
  138.     return sqrt((x - center.x) * (x - center.x) + (y - center.y) * (y - center.y));
  139. }
  140.  
  141. void doMasc(Mat img, Mat *masc, figure_t figure, const cv_figure_t *AllMasc) {
  142.     int i = (int)figure - 1;
  143.     cvtColor(img, *masc, CV_BGR2HSV);
  144.     if (AllMasc[i].hMin > AllMasc[i].hMax) {
  145.         Mat buff1, buff2;
  146.         inRange(*masc, Scalar(AllMasc[i].hMin, AllMasc[i].sMin, AllMasc[i].vMin), Scalar(255, AllMasc[i].sMax, AllMasc[i].vMax), buff1);
  147.         inRange(*masc, Scalar(0, AllMasc[i].sMin, AllMasc[i].vMin), Scalar(AllMasc[i].hMax, AllMasc[i].sMax, AllMasc[i].vMax), buff2);
  148.         *masc = buff1 | buff2;
  149.     }
  150.     else {
  151.         inRange(*masc, Scalar(AllMasc[i].hMin, AllMasc[i].sMin, AllMasc[i].vMin), Scalar(AllMasc[i].hMax, AllMasc[i].sMax, AllMasc[i].vMax), *masc);
  152.     }
  153.     erode(*masc, *masc, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  154.     dilate(*masc, *masc, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  155.     dilate(*masc, *masc, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  156.     erode(*masc, *masc, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  157. }
  158.  
  159. figure_t GetFigure(Mat img, Rect *rectFigure) {
  160.     Mat hsv;
  161.     vector<vector<Point>> contours;
  162.     figure_t maxFoundFigure = Free_f;
  163.     int buffDistance;
  164.     Rect FigureRect;
  165.     Point center = ((img.size().width / 2.0), (img.size().height / 2.0));
  166.     int distanceFigure = 10000000;//distance2Center((img.size().width), (img.size().height), center);
  167.     for (int i = 0; i < NUMBER_REAL_FIGURE; i++) {
  168.         doMasc(img, &hsv, (figure_t)(i + 1), AllRealFigures);
  169.         findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  170.         //waitKey(0);
  171.         for (int j = 0; j < contours.size(); j++) {
  172.             if (MAX_FIGURE_SQUARE < contourArea(contours[j])) {
  173.                 Rect rect = boundingRect(contours[j]);
  174.                 buffDistance = distance2Center(rect.x + (rect.width / 2.0), rect.y + (rect.height / 2.0), center);
  175.                 if (buffDistance < distanceFigure) {
  176.                     //if (((i == 2) || (i == 5))) { //j or i
  177.                     //  RotatedRect r_rect = minAreaRect(contours[j]);
  178.                     //  Size size;
  179.                     //  size.width = min(r_rect.size.height, r_rect.size.width);
  180.                     //  size.height = max(r_rect.size.height, r_rect.size.width);
  181.                     //  if (((i == 2) && (size.height / (double)size.width) < 2) || ((i == 5) && (size.height / (double)size.width) >= 2)) {
  182.                     //      distanceFigure = buffDistance;
  183.                     //      maxFoundFigure = (figure_t)(i + 1);
  184.                     //      FigureRect = rect;
  185.                     //  }
  186.                     //  continue;
  187.                     //}
  188.                     distanceFigure = buffDistance;
  189.                     maxFoundFigure = (figure_t)(i + 1);
  190.                     FigureRect = rect;
  191.                 }
  192.             }
  193.         }
  194.     }
  195.     *rectFigure = FigureRect;
  196.     return maxFoundFigure;
  197. }
  198.  
  199.  
  200. figure_t GetFigureFromStack(Mat img) {
  201.     Mat hsv;
  202.     vector<vector<Point>> contours;
  203.     int squareMax = 700, squareBuff;
  204.     figure_t maxFoundFigure = Free_f;
  205.     for (int i = 0; i < NUMBER_REAL_FIGURE; i++) {
  206.         doMasc(img, &hsv, (figure_t)(i + 1), AllFiguresInStack);
  207.         findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  208.         for (int j = 0; j < contours.size(); j++) {
  209.             squareBuff = contourArea(contours[j]);
  210.             if (squareBuff > squareMax) {
  211.                 squareMax = squareBuff;
  212.                 maxFoundFigure = (figure_t)(i + 1);
  213.             }
  214.         }
  215.     }
  216.     return maxFoundFigure;
  217. }
  218.  
  219. int getImgFrame(Mat img, Mat *imgFrame) {
  220.     Mat hsv;
  221.     cvtColor(img, hsv, CV_BGR2HSV);
  222.     inRange(hsv, Scalar(0, 0, 140), Scalar(255, 255, 255), hsv);
  223.     erode(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  224.     dilate(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  225.     dilate(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  226.     erode(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  227.     vector<vector<Point>> contours;
  228.     findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  229.     Size imgSize = img.size();
  230.     int indFrame = -1;
  231.     for (int i = 0; i < contours.size(); i++) {
  232.         for (int j = 0; j < contours[i].size(); j++) {
  233.             int nextInd = (j + 1) % contours[i].size();
  234.             if ((contours[i][j].y == contours[i][nextInd].y) && (contours[i][j].y == 0) &&
  235.                 (((contours[i][j].x <= (imgSize.width / 2)) && ((imgSize.width / 2) <= contours[i][nextInd].x)) ||
  236.                 ((contours[i][j].x >= (imgSize.width / 2)) && ((imgSize.width / 2) >= contours[i][nextInd].x)))) {
  237.                 indFrame = i;
  238.             }
  239.         }
  240.     }
  241.     if (indFrame == -1)
  242.         return 1;
  243.     RotatedRect rect = minAreaRect(contours[indFrame]);
  244.     Rect brect = rect.boundingRect();
  245.     //rectangle(img, brect, Scalar(0, 255, 0));
  246.     *imgFrame = img(brect);
  247.     return 0;
  248. }
  249.  
  250. figure_t GetFigureFromFrame(Mat img) {
  251.     Mat hsv, imgFrame;
  252.     if (getImgFrame(img, &imgFrame))
  253.         imgFrame = img;
  254.     vector<vector<Point>> contours;
  255.     int squareMax = 700, squareBuff;
  256.     figure_t maxFoundFigure = Free_f;
  257.     for (int i = 0; i < NUMBER_REAL_FIGURE; i++) {
  258.         doMasc(imgFrame, &hsv, (figure_t)(i + 1), AllFiguresInFrame);
  259.         findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  260.         for (int j = 0; j < contours.size(); j++) {
  261.             squareBuff = contourArea(contours[j]);
  262.             if (squareBuff > squareMax) {
  263.                 if (((i == 2) || (i == 5))) { //j or i
  264.                     RotatedRect rect = minAreaRect(contours[j]);
  265.                     if (((i == 2) && (rect.size.height / (double)rect.size.width) < 2.5) || ((i == 5) && (rect.size.height / (double)rect.size.width) >= 2.5)) {
  266.                         squareMax = squareBuff;
  267.                         maxFoundFigure = (figure_t)(i + 1);
  268.                     }
  269.                     continue;
  270.                 }
  271.                 squareMax = squareBuff;
  272.                 maxFoundFigure = (figure_t)(i + 1);
  273.             }
  274.         }
  275.     }
  276.     return maxFoundFigure;
  277. }
  278.  
  279. Point GetHole(Mat img_start, figure_t figure, Rect rectFigure) {
  280.     Mat img = img_start(rectFigure);
  281.     Mat hsv;
  282.  
  283.     int i = (int)figure - 1;
  284.     cvtColor(img, hsv, CV_BGR2HSV);
  285.     inRange(hsv, Scalar(AllFiguresHoles[i].hMin, AllFiguresHoles[i].sMin, AllFiguresHoles[i].vMin), Scalar(AllFiguresHoles[i].hMax, AllFiguresHoles[i].sMax, AllFiguresHoles[i].vMax), hsv);
  286.     erode(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  287.     dilate(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  288.     dilate(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  289.     erode(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));
  290.  
  291.     cvtColor(img, img, CV_RGB2GRAY);//
  292.     bitwise_and(img, hsv, img);//
  293.     GaussianBlur(img, img, Size(3, 3), 1);
  294.     HoughCirclesTest(img);
  295.     //Canny(img, img, 75, 255, 3);
  296.     //vector<Vec3f> circles;
  297.     //HoughCircles(img, circles, CV_HOUGH_GRADIENT, 1, 13, 100, 12, 1, 30);
  298.     //for (int i = 0; i < circles.size(); i++) {
  299.     //  Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  300.     //  int radius = cvRound(circles[i][2]);
  301.     //  circle(img, center, 3, Scalar(255, 255, 255), -1, 8, 0);
  302.     //}
  303.     imshow("out", img);
  304.     return {0, 0};
  305. }
  306.  
  307. int cmp_left_point_x(const void *a, const void *b) {
  308.     Point p1 = *((Point *)a), p2 = *((Point *)b);
  309.     return p1.x - p2.x;
  310. }
  311.  
  312. int AngFigure(Mat img, figure_t figure, double *ang) {
  313.     Mat hsv;
  314.     doMasc(img, &hsv, figure, AllRealFigures);
  315.     vector<vector<Point>> contours;
  316.     findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  317.     int squareMax = 500, squareBuff;
  318.     int index = -1;
  319.     for (int j = 0; j < contours.size(); j++) {
  320.         squareBuff = contourArea(contours[j]);
  321.         if (squareBuff > squareMax) {
  322.             squareMax = squareBuff;
  323.             index = j;
  324.         }
  325.     }
  326.     if (index == -1) {
  327.         return 1;
  328.     }
  329.     RotatedRect rect = minAreaRect(contours[index]);
  330.     int ang_ofset = 0;
  331.         Mat dst, cdst;
  332.         Canny(hsv, dst, 50, 200, 3);
  333.         cvtColor(dst, cdst, CV_GRAY2BGR);
  334.         vector<Vec4i> lines;
  335.         HoughLinesP(dst, lines, 1, CV_PI / 180, 15, SupportSideLengthFigure[int(figure) - int(O_f)], 10); // Z S 33 //L J 55// точность в длине, угол, значение акума >, минимальная длина, максимальный разрыв между точкаи
  336.         double sum_ang = 0;
  337.         if (lines.size() == 0) {
  338.             return 1;
  339.         }
  340.         *ang = 360;
  341.         for (size_t i = 0; i < lines.size(); i++){
  342.             Vec4i l = lines[i];
  343.             int minInd;
  344.             if (l[0] < l[2]) {
  345.                 minInd = 0;
  346.             }
  347.             else {
  348.                 minInd = 2;
  349.             }
  350.             double tg = (-l[minInd + 1] + l[(minInd + 2) % 4 + 1]) / (double)(-l[minInd] + l[(minInd + 2) % 4]);
  351.             double arctan;
  352.             if (tg == INFINITY) {
  353.                 arctan = PI / (float)2;
  354.             }
  355.             else {
  356.                 arctan = atan(tg);
  357.             }
  358.             if (O_f == figure) {
  359.                 if (fabs(atan(tg)) < fabs(*ang))
  360.                     *ang = arctan;
  361.             }
  362.             else {
  363.                 if (arctan < 0)
  364.                     arctan += PI;
  365.                 sum_ang += arctan;
  366.             }
  367.             line(img, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255, 255, 255), 5, CV_AA);
  368.         }
  369.         if (figure != O_f) {
  370.             *ang = (sum_ang / lines.size());
  371.             if (*ang > (PI / (float)2))
  372.                 *ang -= PI;
  373.         }
  374.         *ang *= (180 / PI);
  375.         return 0;
  376. }
  377.  
  378. Point getCenter(Mat img, figure_t figure) {
  379.     Mat hsv;
  380.     doMasc(img, &hsv, figure, AllRealFigures);
  381.     vector<vector<Point>> contours;
  382.     findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  383.     int squareMax = 500, squareBuff;
  384.     int index = -1;
  385.     for (int j = 0; j < contours.size(); j++) {
  386.         squareBuff = contourArea(contours[j]);
  387.         if (squareBuff > squareMax) {
  388.             squareMax = squareBuff;
  389.             index = j;
  390.         }
  391.     }
  392.     if (index == -1) {
  393.         return {0, 0};
  394.     }
  395.     RotatedRect rect = minAreaRect(contours[index]);
  396.     //circle(img, rect.center, 1, Scalar(0, 255, 0), 3);
  397.     return rect.center;
  398. }
  399.  
  400. int BuseFrame(Mat img, frame_t frame) {
  401.     //Если плохо работает, то написать аккомулятор точек и выравнить изображение
  402.     const int offset_img_x = 10,
  403.         offset_img_y = 14,
  404.         block_size_x = 17,
  405.         block_size_y = 22;
  406.     Mat imgFrame, hsv;
  407.     if(getImgFrame(img, &imgFrame))
  408.         return 1;
  409.     cvtColor(imgFrame, hsv, CV_BGR2HSV);
  410.     inRange(hsv, Scalar(0, 60, 0), Scalar(255, 255, 255), hsv);
  411.     const int smooth = 15;
  412.     erode(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(smooth, smooth)));
  413.     dilate(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(smooth, smooth)));
  414.     dilate(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(smooth, smooth)));
  415.     erode(hsv, hsv, getStructuringElement(MORPH_ELLIPSE, Size(smooth, smooth)));
  416.     Size sizeFrame = imgFrame.size();
  417.     for(int i = 0; i < FRAME_SIZE_X; i++)
  418.         for (int j = 0; j < FRAME_SIZE_Y; j++) {
  419.             frame[i][j] = int(hsv.at<uchar>(Point(sizeFrame.width - offset_img_x - int(block_size_x * i), offset_img_y + block_size_y * j)));
  420.             //cout << int(hsv.at<uchar>(Point(sizeFrame.width - offset_img_x - int(block_size_x * i), offset_img_y + block_size_y * j))) << " " << i << " " << j << endl;
  421.             //circle(imgFrame, Point(sizeFrame.width - offset_img_x - int(block_size_x * i), offset_img_y + block_size_y * j), 1, Scalar(0, 255, 0));
  422.         }
  423.    
  424.     /*vector<vector<Point>> contours;
  425.     int areaSum = 0;
  426.     findContours(hsv, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  427.     for (int j = 0; j < contours.size(); j++)
  428.         areaSum += contourArea(contours[j]);*/
  429.     //430 площадь одной
  430.     return 0;
  431.  
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement