Circumscribed rectangle
Take the maximum circumscribed rectangle of the contour
Rect boundingRect( InputArray array );
- array: the input grayscale image or 2D point set, and the data type is vector or Mat.
The function can calculate the maximum circumscribed rectangle containing the object contour or 2D point set in the input image. The function has only one parameter, which can be gray image or 2D point set. The parameter type of gray image is Mat, and the parameter type of 2D point set is vector or Mat. The return value of this function is a Rect type variable, which can directly draw a rectangle with the rectangle() function. The return value has four parameters. The first two parameters are the coordinates of the first pixel in the upper left corner of the maximum circumscribed rectangle, and the last two parameters represent the width and height of the maximum circumscribed rectangle respectively.
Minimum circumscribed rectangle of contour
RotatedRect minAreaRect( InputArray points );
- Points: the set of 2D points entered
The function can calculate the smallest circumscribed rectangle according to the input 2D point set. The return value of the function is a variable of RotatedRect type, including the center position of the rectangle, the width and height of the rectangle and the rotation angle of the rectangle. The RotatedRect class has two important methods and properties, which can output the four vertices and center coordinates of the rectangle. The method of outputting the coordinates of four vertices is points(). Assuming that the variable of RotatedRect class is rrect, you can use rrect Points (points) command to read, where the variable stored in the coordinates is an array of Point2f type. The attribute of the output rectangle center coordinate is center. Assuming that the variable of RotatedRect class is rrect, you can use opt = rrect Center command, where the variable stored in the coordinates is a variable of Point2f type.
Circumscribed polygon
void approxPolyDP( InputArray curve, OutputArray approxCurve, double epsilon, bool closed );
- curve: enter contour pixels.
- approxCurve: the polygon approximation result is given in the form of polygon vertex coordinates.
- epsilon: approximation accuracy, that is, the maximum distance between the original curve and the approximation curve.
- Closed: indicates whether the approximation curve is a closed curve. true indicates that the curve is closed, that is, the last vertex is connected with the first vertex.
The function obtains the best approximation polygon according to the input contour.
The first parameter of the function is the 2D pixels of the input contour, and the data type is vector or Mat.
The second parameter is the approximation result of the polygon, which is output in the form of polygon vertex coordinates. It is CV_32SC2 type N × 1, the geometric shape of the contour can be preliminarily judged by the number of vertices of the output result.
The third parameter is the precision of polygon approximation, that is, the maximum distance between the original curve and the approximation curve.
The fourth parameter is whether the approximation curve is a closed curve. true indicates that the curve is closed, that is, the last vertex is connected with the first vertex.
Simple example
// // Created by smallflyfly on 2021/6/22. // #include "opencv2/opencv.hpp" #include <iostream> using namespace std; using namespace cv; int main() { Mat im = imread("rice.jfif"); // resize(im, im, Size(0, 0), 0.5, 0.5); Mat gray; cvtColor(im, gray, CV_BGR2GRAY); Mat imBin; threshold(gray, imBin, 150, 255, THRESH_BINARY); vector<vector<Point>> contours; findContours(imBin, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); Mat im1 = im.clone(); Mat im2 = im.clone(); for (auto & contour : contours) { // Maximum circumscribed matrix Rect rect = boundingRect(contour); rectangle(im, rect, Scalar(0, 0, 255), 1); // Minimum circumscribed rectangle RotatedRect rotatedRect = minAreaRect(contour); Point2f pts[4]; rotatedRect.points(pts); Point2f pt = rotatedRect.center; for (int i = 0; i < 4; ++i) { if (i == 3) { line(im1, pts[i], pts[0], Scalar(255, 255, 0), 1); } else { line(im1, pts[i], pts[i+1], Scalar(255, 255, 0), 1); } } circle(im1, pt, 1, Scalar(0, 0, 255), -1); // Circumscribed polygon Mat ploys; approxPolyDP(contour, ploys, 5, true); // draw ploy Vec2i pt1, pt2; for (int i = 0; i < ploys.rows; ++i) { if (i == ploys.rows - 1) { pt1 = ploys.at<Vec2i>(i); pt2 = ploys.at<Vec2i>(0); } else { pt1 = ploys.at<Vec2i>(i); pt2 = ploys.at<Vec2i>(i+1); } line(im2, pt1, pt2, Scalar(0, 0, 255), 2); } } imshow("im", im); imshow("im1", im1); imshow("im2", im2); waitKey(0); destroyAllWindows(); return 0; }