How to find corners on a Image using OpenCv

后端 未结 5 1098
春和景丽
春和景丽 2020-12-02 05:11

I´m trying to find the corners on a image, I don´t need the contours, only the 4 corners. I will change the perspective using 4 corners.

I´m using Opencv, but I need

5条回答
  •  猫巷女王i
    2020-12-02 05:26

    1. find contours with RETR_EXTERNAL option.(gray -> gaussian filter -> canny edge -> find contour)
    2. find the largest size contour -> this will be the edge of the rectangle
    3. find corners with little calculation

      Mat m;//image file
      findContours(m, contours_, hierachy_, RETR_EXTERNAL);
      auto it = max_element(contours_.begin(), contours_.end(),
          [](const vector &a, const vector &b) {
              return a.size() < b.size(); });
      Point2f xy[4] = {{9000,9000}, {0, 1000}, {1000, 0}, {0,0}};
      for(auto &[x, y] : *it) {
          if(x + y < xy[0].x + xy[0].y) xy[0] = {x, y};
          if(x - y > xy[1].x - xy[1].y) xy[1] = {x, y};
          if(y - x > xy[2].y - xy[2].x) xy[2] = {x, y};
          if(x + y > xy[3].x + xy[3].y) xy[3] = {x, y};
       }
      

    xy[4] will be the four corners. I was able to extract four corners this way.

提交回复
热议问题