Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of drawing rectangles and circles around the OpenCV outline

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

OpenCV outline drawing rectangular box and circular box around the method is what, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can harvest.

Introduction to drawing around contours

There is no concept, just draw the surrounding figure for the contour obtained. For example, the figure below draws the contour obtained on the left to obtain the image on the right:

Related API Reduce polygon contour points: approxixPolyDP

Function role: based on RDP algorithm, the purpose is to reduce the number of polygon contour points

Function prototype:

//Reduce polygon contour points approxixPolyDP InputArray curve, //Generally, it is a point set Mat(vector) composed of outline points of the image OutputArray approxCurve, //indicates the output polygon point set double epsilon, //mainly indicates the accuracy of the output, that is, the maximum distance between two contour points, 5, 6, 7, 8,,,, bool closed //indicates whether the output polygon is closed)

RDP algorithm introduction:

Judge whether the distance between the start point (current point) and the end point is less than epsilon, if less than epsilon, end, not less than execution 2

Select point C at the last two positions of starting point (current point)A, judge whether the distance between them is less than epsilon, if less than epsilon, point C and their middle point B are discarded, if not less than epsilon, execute 3.

The distance between A and B, B and C is judged. If one is less than epsilon, then point B is abandoned, otherwise it is reserved. Then point C is used as the starting point (current point) to repeat the steps 1, 2 and 3 until the end point (where the result is a series of satisfactory points).

Draw rectangle around outline: boundingRect, minAreaRect

cv::boundingRect(InputArray points) Get the coordinates of the upper-left intersection and lower-right corner of the smallest rectangle around the outline, draw a rectangle

cv::minAreaRect(InputArray points) Gets a rotated rectangle, returns a rotated rectangle

Draw circles and ellipses around contours: minEnclosingCircle, fitEllipse//Get the smallest ellipse around contours cv::minEnclosingCircle(InputArray points, //Get the smallest area Circle Point2f& center, //Center position Output parameters float& radius //radius of circle output parameter)//get minimum ellipse around contour cv::fitEllipse(InputArray points) drawing steps

Change an image to a binary image

Find contours, find image contours

Find the smallest enclosing rectangle and circle, rotate rectangle and ellipse on contour points through correlation API

Draw Around

code example

#include #include #include #include #include using namespace std;using namespace cv;Mat src, gray_src, drawImg;int threshold_v = 170;int threshold_max = 255;const char* output_win = "rectangle-demo";RNG rng(12345);void Contours_Callback(int, void*);int main(int argc, char** argv) { src = imread("./ test2.jpg"); if (! src.data) { printf("could not load image...\ n"); return -1; } cvtColor(src, gray_src, CV_BGR2GRAY); blur(gray_src, gray_src, Size(3, 3), Point(-1, -1)); const char* source_win = "input image"; namedWindow(source_win, CV_WINDOW_AUTOSIZE); namedWindow(output_win, CV_WINDOW_AUTOSIZE); imshow(source_win, src); createTrackbar("Threshold Value:", output_win, &threshold_v, threshold_max, Contours_Callback); Contours_Callback(0, 0); waitKey(0); return 0;}void Contours_Callback(int, void*) { Mat binary_output; vector contours; vector hierachy; threshold(gray_src, binary_output, threshold_v, threshold_max, THRESH_BINARY); imshow("binary image", binary_output); findContours(binary_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1)); vector contours_ploy(contours.size()); vector ploy_rects(contours.size()); vector ccs(contours.size()); vector radius(contours.size()); vector minRects(contours.size()); vector myellipse(contours.size()); for (size_t i = 0; i

< contours.size(); i++) { approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true); ploy_rects[i] = boundingRect(contours_ploy[i]); minEnclosingCircle(contours_ploy[i], ccs[i], radius[i]); if (contours_ploy[i].size() >

5) { myellipse[i] = fitEllipse(contours_ploy[i]); minRects[i] = minAreaRect(contours_ploy[i]); } // draw it drawImg = Mat::zeros(src.size(), src.type()); Point2f pts[4]; for (size_t t = 0; t

< contours.size(); t++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); //rectangle(drawImg, ploy_rects[t], color, 2, 8); //circle(drawImg, ccs[t], radius[t], color, 2, 8); if (contours_ploy[t].size() >

5) { ellipse(drawImg, myellipse[t], color, 1, 8); minRects[t].points(pts); for (int r = 0; r < 4; r++) { line(drawImg, pts[r], pts[(r + 1) % 4], color, 1, 8); } imshow(output_win, drawImg); Return: Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report