OpenCV growth Path: Extraction and description of lines and contours

Source: Internet
Author: User
Tags scalar

http://ronny.blog.51cto.com/8801997/1394139

OpenCV growth Path: Extraction and description of lines and contours

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://ronny.blog.51cto.com/8801997/1394139

The focus of content-based image analysis is to extract the representative features of the image, and the lines, outlines and blocks are often the most characteristic elements, this article is aimed at these important image features, study their usage in OpenCV, and do some simple basic applications.

First, canny detection contour

In the previous article mentioned Sobel edge detection, and rewritten the soble C + + code to be consistent with the algorithm in MATLAB, and soble edge detection is based on a single threshold, we can not take into account the low threshold of rich edges and high thresholds when the edge is missing these two problems. The canny operator is a good remedy for this shortcoming, and from now on, canny edge detection is the best edge detection algorithm in image contour extraction.

Canny edge detection adopts double threshold value method, high threshold value is used to detect important, significant lines, contours in the image, and the low threshold value is used to ensure that there is no loss of detail, and the lower threshold is more abundant, but many edges are not our concern. Finally, a search algorithm is used to keep the lines with overlapping edges of the high threshold values in the lower thresholds, and the other lines are deleted.

This article does not canny the algorithm principle to further explain, later will be in the image processing algorithm related article detailed introduction.

Below we use the canny function in OpenCV to detect the edge of the image

12345678910111213 int main(){    Mat I=imread("../cat.png");    cvtColor(I,I,CV_BGR2GRAY);                                                   Mat contours;    Canny(I,contours,125,350);    threshold(contours,contours,128,255,THRESH_BINARY);    namedWindow("Canny");    imshow("Canny",contours);    waitKey();    return0;}

The display results are as follows:

Second, straight line detection

Straight lines appear in the image frequency is very high, and the line as the image of the characteristics of the basic content of the image analysis has a very important role, this paper through the OPENCV in the Hough transform to detect the image of the line.

Let's look at the basic Hough transform function houghlines, which has the following prototype:

1 voidHoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, doublestn=0 );

Its input is a two-value contour image, which is often the result image of edge detection; its output is an array of multiple vec2f points, each element in the array is a two-dollar floating-point data pair <rou,theta>,rou the distance from the origin of the coordinate line, Theta represents the angle. The 3rd and 4th parameters represent the step size, because the Hough transform is actually an exhaustive algorithm, and Rho represents the stride length of the distance, and Theta represents the step size of the angle. The 5th parameter is a threshold setting that is directly the minimum number of votes, and knowing the Hough principle, this parameter should be easy to understand.

From the output of this function we can see that the resulting line is not specified in the image of the beginning and end of the point, we need to calculate, if we want to direct display in the image will be more troublesome, and there will be many angles close to the line, in fact, they are repeated, in order to solve these problems, OPENCV also provides a function houghlinesp (). Its output is a vector of vec4i. Each element of the vector represents a straight line, consisting of a 4-dollar floating-point array, a group of first two points, and a group of two points representing the starting and ending points of the line in the image.

1 voidHoughLinesP(InputArray image, OutputArray lines, double rho, double theta,int threshold, double minLineLength=0, doublemaxLineGap=0 );

Explain the last two parameters, Minlinelength specifies the minimum width in the detection line, if the lower than the minimum width is discarded, maxlinegap specifies a line through the same point, if the distance is less than Maxlinegap will be merged.

Here is an example of using HOUGHLINESP to detect a line:

123456789101112131415161718 intmain(){    Mat image=imread("../car.png");    Mat I;    cvtColor(image,I,CV_BGR2GRAY);                               Mat contours;    Canny(I,contours,125,350);    threshold(contours,contours,128,255,THRESH_BINARY);    vector<Vec4i> lines;    // 检测直线,最小投票为90,线条不短于50,间隙不小于10    HoughLinesP(contours,lines,1,CV_PI/180,80,50,10);    drawDetectLines(image,lines,Scalar(0,255,0));    namedWindow("Lines");    imshow("Lines",image);    waitKey();    return0;}

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

123456789101112 voiddrawDetectLines(Mat& image,constvector<Vec4i>& lines,Scalar & color){    // 将检测到的直线在图上画出来    vector<Vec4i>::const_iterator it=lines.begin();    while(it!=lines.end())    {        Point pt1((*it)[0],(*it)[1]);        Point pt2((*it)[2],(*it)[3]);        line(image,pt1,pt2,color,2); //  线条宽度设置为2        ++it;    }}

In fact, the Hough transform can detect many fixed shapes, such as circles, squares, and so on. Their principle is basically the same, all constructs a voting matrix. OPENCV provides a function for detecting a circle houghcircles, whose output is a vector of vec3i,vector each element contains 3 floating-point numbers, the first 2 is the center coordinate of the circle, and the last is the radius.

Three, the outline of the extraction and description

In target recognition we first have to extract the target of interest, and the common step is to extract the target foreground map by color or texture (a black and white image, the target is shown in white in the image), then we will analyze the foreground map to further extract the target, What is often used here is to extract the contour of the target.

The function of extracting target contour in OpenCV is findcontours, its input image is a two-value image, and the output is a set of contour points of each connected area:vector<vector<point>>. The size of the outer vector represents the number of outlines in the image, and the vector size represents the number of points on the contour. Let's look at the usage of the function using an example.

123456789101112131415161718 intmain()  {      usingnamespacecv;                 Mat image=imread("../shape.png");      cvtColor(image,image,CV_BGR2GRAY);     vector<vector<Point>> contours;     // find     findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);    // draw     Mat result(image.size(),CV_8U,Scalar(0));     drawContours(result,contours,-1,Scalar(255),2);                namedWindow("contours");     imshow("contours",result);     waitKey();     return0;}

The above program contains 2 functions, the first is to find the contour function, its third parameter describes the type of the look-up contour, here we use the outer contour, but also to find all the contours, that is, including some of the holes, such as the image of the character arm and the shape of the waist between the contour. The 4th parameter describes the method of contour representation, the parameters in the program outline includes all points, you can also use other parameters to let a bit of straight place, only the line start and end point of the location, the specific parameters can be used to refer to the description of the function in the manual.

The second function drawcontours is a contour function, its 3rd parameter program set 1 means that all the contours are drawn, you can also specify the outline to be drawn the ordinal number.

After extracting to the contour, in fact, we are more concerned about if these contours are converted to the features that can be used, that is, the description of the contour problem, there are many ways to choose, such as vectorization for polygons, rectangles, ellipses and so on. Some of these functions are provided in the OPENCV.

12345678910111213141516171819202122232425262728 // 轮廓表示为一个矩形Rect r = boundingRect(Mat(contours[0]));rectangle(result, r, Scalar(255), 2);// 轮廓表示为一个圆floatradius;Point2f center;minEnclosingCircle(Mat(contours[1]), center, radius);circle(result, Point(center), static_cast<int>(radius), Scalar(255), 2);// 轮廓表示为一个多边形vector<Point> poly;approxPolyDP(Mat(contours[2]), poly, 5, true);vector<Point>::const_iterator itp = poly.begin();while(itp != (poly.end() - 1)){    line(result, *itp, *(itp + 1), Scalar(255), 2);    ++itp;}line(result, *itp, *(poly.begin()), Scalar(255), 2);// 轮廓表示为凸多边形vector<Point> hull;convexHull(Mat(contours[3]), hull);vector<Point>::const_iterator ith = hull.begin();while(ith != (hull.end() - 1)){    line(result, *ith, *(ith + 1), Scalar(255), 2);    ++ith;}line(result, *ith, *(hull.begin()), Scalar(255), 2);

In the program we draw rectangles, circles, polygons, and convex polygons in turn. The final effect is as follows:

The analysis of the connected area is far from over, we can further calculate the other properties of each connected area, such as: center of gravity, central moment and other features, which have the opportunity to write later.

Here are a few functions to try: Minarearect: Calculates an external rectangle with a minimum area, Contourarea can calculate the area of the connected area within the contour; Pointpolygentest can be used to determine whether a point is within a polygon. Mathshapes can compare the similarity of two shapes, a function that is quite useful.

OpenCV growth Path: Extraction and description of lines and contours

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.