Extraction and description of eight straight lines and outlines in opencv tutorials

Source: Internet
Author: User
Tags scalar

 

Content-based image analysis focuses on extracting representative features from images, while lines, outlines, and blocks are often the elements that best reflect features, this article focuses on these important image features, studies their usage in opencv, and makes some basic applications.

1. Detect the contour with the help of the "quick bi" command

Sobel edge detection was mentioned in the previous article, and soble's c ++ code was rewritten to make it consistent with the algorithm in MATLAB. soble edge detection is based on a single threshold, we cannot take into account the low threshold value of the rich edge and the high threshold value of the edge missing issues. However, the Canny operator makes up for this deficiency. From the current point of view, it is the best edge detection algorithm for image contour extraction.

The dual-threshold value method is used for edge detection. The high threshold value is used to detect important and significant lines and outlines in the image, while the low threshold value is used to ensure that the details are not lost, the edge detected by the low threshold is more abundant, but many edges are not of our concern. Finally, a search algorithm is used to retain the overlapping lines between the low threshold and the high threshold, and delete other lines.

In this article, we will not further describe the principle of the image processing algorithm.

Next, we use the canny function in opencv to detect the image edge.

 1 int main() 2 { 3     Mat I=imread("../cat.png"); 4     cvtColor(I,I,CV_BGR2GRAY); 5                                                 6     Mat contours; 7     Canny(I,contours,125,350); 8     threshold(contours,contours,128,255,THRESH_BINARY); 9     namedWindow("Canny");10     imshow("Canny",contours);11     waitKey();12     return 0;13 }

The display effect is as follows:

Ii. Line Detection

Straight lines appear frequently in images, while straight lines act as image features and play an important role in Image Analysis of basic content, in this paper, the line in the image is detected by using the Hough Transformation in opencv.

Let's take a look at the basic hoghlines transformation function. Its prototype is as follows:

void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 );

Its input is a two-value contour image, often the result image obtained by edge detection; its output is an array containing multiple vec2f points, each element in the array is a binary floating point data pair <Rou, theta>. Rou represents the distance from the coordinate origin of a straight line, and theta represents the angle. The 3rd and 4th parameters represent the step size, because the Hough transformation is actually a poor algorithm, and the rock represents the distance step, and the theta represents the angle step size. The 5th parameter is the minimum number of direct votes for a threshold value setting. It is easy to understand this parameter because it is based on the principle of Hough.

From the output results of this function, we can see that the resulting straight line does not specify the start point and end point in the image. We need to calculate it ourselves, if we want to display the image directly, it will be troublesome and there will be many straight lines with close angles. They are actually repeated. To solve these problems, opencv also provides the houghlinesp () function (). Its output is a vector of vec4i. Each element of a vector represents a straight line, which is composed of a 4-element floating point number group. The first two vertices are in a group, and the last two vertices are in a group, representing the start and end points of a straight line in the image.

void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta,int threshold, double minLineLength=0, double maxLineGap=0 );

In the last two parameters, minlinelength specifies the minimum width of the detection line. If the value is lower than the minimum width, minlinelength is discarded. maxlinegap uses the same straight line. If the distance is smaller than maxlinegap, It is merged.

The following is an example of using houghlinesp to detect a straight line:

1 int main () 2 {3 mat image = imread (".. /car.png "); 4 mat I; 5 cvtcolor (image, I, cv_bgr2gray); 6 7 mat contours; 8 Kan (I, contours, 125,350); 9 threshold (contours, contours, 128,255, thresh_binary); 10 vector <vec4i> lines; 11 // check the straight line. The minimum voting value is 90, the line is not shorter than 50, and the gap is not less than 1012 houghlinesp (contours, lines, 1, cv_pi/180,255, 50, 10); 13 drawdetectlines (image, lines, scalar (, 0); 14 namedwindow ("lines"); 15 imshow ("lines ", image); 16 waitkey (); 17 return 0; 18}

The above program saves the detected lines in the lines variable, and we need to draw them further on the image:

1 void drawdetectlines (MAT & image, const vector <vec4i> & lines, scalar & color) 2 {3 // draw a 4 vector <vec4i>:: const_iterator it = lines. begin (); 5 while (it! = Lines. end () 6 {7 point pt1 (* It) [0], (* It) [1]); 8 point pt2 (* It) [2], (* It) [3]); 9 line (image, pt1, pt2, color, 2); // The line width is set to 210 + + it; 11} 12}

In fact, it can detect many fixed shapes, such as circles and squares. The principles are basically the same. They all construct a voting matrix. Opencv provides the houghcircles function for Circle Detection. Its output is a vector of vec3i. Each element of the vector contains three floating point numbers, and the first two are the center coordinates of the circle, the last one is the radius.

3. contour extraction and description

In target recognition, we first need to extract the target that we are interested in. Generally, the common steps are to extract the foreground image of the target by color or texture (a black-and-white image, the target is displayed in white.) Next, we need to analyze the foreground image and extract the target. What we usually use here is to extract the target contour.

The findcontours function is used to extract the target contour in opencv. Its input image is a binary image that outputs a set of contour points in each connected area: vector <vector <point>. The size of the outer vector represents the number of outlines in the image, and the size of the vector represents the number of vertices on the contour. The following example shows the function usage.

 1 int main() 2 { 3 using namespace cv; 4   5 Mat image=imread("../shape.png"); 6 cvtColor(image,image,CV_BGR2GRAY); 7 vector<vector<Point>> contours; 8 // find 9 findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);10 // draw11 Mat result(image.size(),CV_8U,Scalar(0));12 drawContours(result,contours,-1,Scalar(255),2);13  14 namedWindow("contours");15 imshow("contours",result);16 waitKey();17 return 0;18 }

The above program contains two functions, the first is the lookup contour function, and its third parameter describes the type of the lookup contour. Here we use the outer contour, you can also find all the outlines, including the holes, like the outlines formed between the arm and waist of an image. The first parameter describes the contour representation method. The parameter description in the program describes that the contour includes all vertices. You can also use other parameters to make a straight line. Only the start and end points of the straight line are saved, for details about the parameter usage, refer to the introduction to functions in the manual.

The second function, drawcontours, is a function used to draw outlines. In its 3rd parameter programs, "-1" indicates that all outlines are drawn. You can also specify the sequence number of the outlines to be drawn.

After extracting the contour, we are more concerned about converting the contour into usable features, that is, the profile description problem. There are multiple methods to choose from, for example, the vector is polygon, rectangle, and elliptic. Opencv provides some such functions.

1 // The contour is represented as a rectangle 2 rect r = boundingrect (MAT (contours [0]); 3 rectangle (result, R, scalar (255), 2 ); 4 // The contour is represented as a circle 5 float radius; 6 point2f center; 7 minenclosingcircle (MAT (contours [1]), center, radius); 8 circle (result, point (center), static_cast <int> (RADIUS), scalar (255), 2); 9 // The contour is represented as a polygon 10 vector <point> poly; 11 approxpolydp (MAT (contours [2]), poly, 5, true); 12 vector <point >:: const_iterator IBD = Poly. Begin (); 13 while (IBD! = (Poly. end ()-1) 14 {15 line (result, * administrator, * (User Privilege + 1), scalar (255), 2); 16 ++ administrator; 17} 18 line (result, * UI, * (Poly. begin (), scalar (255), 2); 19 // The contour is expressed as a convex polygon 20 vector <point> hull; 21 convexhull (MAT (contours [3]), hull); 22 vector <point>: const_iterator ith = hull. begin (); 23 while (ith! = (Hull. end ()-1) 24 {25 line (result, * ith, * (ith + 1), scalar (255), 2); 26 + ith; 27} 28 line (result, * ith, * (Hull. begin (), scalar (255), 2 );

In the program, we draw rectangles, circles, polygon, and convex polygon in sequence. The final result is as follows:

The Analysis of connected areas is far from over. We can further calculate other attributes of each connected area, such as the center of gravity, center moment, and other features. These contents will be available in the future.

Use the following functions: minarearect: calculate an external rectangle with the smallest area, and the connected area can calculate the area of the connected area in the contour. pointpolygentest can be used to determine whether a point is in a polygon. Mathshapes can compare the similarity between two shapes. It is a useful function.

Extraction and description of eight straight lines and outlines in opencv tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.