OPENCV Study Notes (10) Draw lines, rectangles, circles, ellipses, and polygon programs

Source: Internet
Author: User
Tags function definition

The first is a straight line program, and the end result is a straight line on the open picture.

#include <opencv2/opencv.hpp>
int main (int argc, char **argv[])
{
    iplimage* img =cvloadimage ("a.jpg ", -1);  
    Cvscalar color = Cv_rgb (255,0,0); 
    Cvline (Img,cvpoint (0,0), Cvpoint (100,100), color,10,8);
    Cvnamedwindow ("Test", cv_window_autosize);
    Cvshowimage ("Test", IMG);
    Cvwaitkey ();
    Cvreleaseimage (&img);
    Cvdestroywindow ("test");
}

1.1 Cvline
Function Prototypes:
void Cvline (
cvarr* Array,
Cvpoint pt1,
Cvpoint Pt2,
Cvscalar Color,
int thickness = 1,
int connectivity= 8
);
Baidu, Baidu Encyclopedia also has a parameter int shift=0
Shift: The number of decimal places for the coordinate point.
Note is: I tried, in the program to add this parameter is not an error, it can be seen that this parameter should be present.

The first property in the

Cvline () function is cvarr*. Here, it is generally a pointer to an image type iplimage*. The next two cvpoint is a simple data structure that includes only integer variables x and Y. We can use the cvpoint (int x,int y) function to quickly construct a variable of type cvpoint, which makes it easy to assign two integer variable values to the CVPOINT data structure. The
Next property is a color variable of type cvscalar. Cvscalar is also a data structure that is defined as follows:
typedef struct{
Double val[4];
}cvscalar; As you can see from the
, this structure is only a collection of four double-precision floating-point variables. Here, the first three represent red, green, and blue channels; no fourth is used (it is used only when appropriate for the alpha channel). A common handy macro is Cv_rgb (R, G, b), which takes three numbers as parameters and encapsulates them to cvscalar.
The next two properties are optional. Thickness is the thickness of the line (pixels), connectivity is set to anti-aliasing mode, the default value is "8 Connected", this is a more smooth line without aliasing. can also be set to "4 connected", so that the slash will overlap so that it looks too heavy, but the picture is much faster.

PT1 the first endpoint of a segment.
Pt2 the second endpoint of a segment.
The color of the color segment.
Thickness the thickness of the line segment (fill in the numbers on the line).
The type of the int connectivity segment.
8 (or 0)-8-connected line (8 adjacency) connector.
4-4-connected Line (4 adjacency) connector.
Cv_aa-antialiased Line (under Baidu is "anti-aliasing line").
1.2 Cv_rgb
Cv_rgb is a macro in OPENCV that creates a color value:
#define CV_RGB (R,g,b) cvscalar ((b), (g), (R)).
R (red), G (green), B (blue)
1.3 Cvpoint
One of the basic data types of OPENCV, which represents a two-dimensional point with an integer coordinate, and is a simple struct containing the integer type member x and Y.
So the program is Cvpoint (0,0) and the like.

The following is a rectangle program to draw:

#include <opencv2/opencv.hpp>
int main (int argc, char **argv[])
{
    iplimage* img =cvloadimage ("a.jpg ", -1);  
    Cvscalar color = Cv_rgb (0,0,255); 
    Cvrectangle (Img,cvpoint (0,0), Cvpoint (100,100), color,5,4,0);
    Cvnamedwindow ("Test", cv_window_autosize);
    Cvshowimage ("Test", IMG);
    Cvwaitkey ();
    Cvreleaseimage (&img);
    Cvdestroywindow ("test");
}

This program is almost exactly the same as the above line drawing program.
2.1 Cvrectangle
Cvrectangle () and Cvline () are almost as convenient. Cvrectangle () is used to draw rectangles. Except that there is no connectivity parameter, it is the same as the other parameters of Cvline (). Because the resulting rectangle is always parallel to the X and Y axes. Using Cvrectangle (), we only need to give two pairs of vertices, OpenCV easy to draw a rectangle.
Defined:
void Cvrectangle (
cvarr* Array,
Cvpoint pt1,
Cvpoint Pt2,
Cvscalar Color,
int Thickness=1,
);
which
Thickness
The thickness of the line that makes up the rectangle. When you take a negative value (such as cv_filled--can be written directly as -1,-2) The function draws a color-filled rectangle.
However, I found that if the addition of a parameter is also possible, is the int connectivity=8 This parameter-Baidu to have. int shift=0 also has.

Next is the drawing circle:

#include <opencv2/opencv.hpp>
int main (int argc, char **argv[])
{
    iplimage* img =cvloadimage ("a.jpg ", -1);  
    Cvscalar color = Cv_rgb (0,0,255); 
    Cvcircle (Img,cvpoint (200,300), 59,color,cv_filled);
    Cvnamedwindow ("Test", cv_window_autosize);
    Cvshowimage ("Test", IMG);
    Cvwaitkey ();
    Cvreleaseimage (&img);
    Cvdestroywindow ("test");
}

3.1 Cvcircle Round
void Cvcircle (
cvarr* Array,
Cvpoint Center,//center
Cvpoint radius,//Radius
Cvscalar Color,
int Thickness=1,
int connectivity=8
);
For many closed graphs, such as circles and matrices, the thickness parameter can also be set to Cv_fill with a value of 11, and the result is to fill the interior of the circle with the same color as the edge.

The program that draws the ellipse is similar to a circle, except for a function:
3.2 Cvellipse Oval
void Cvellipse (
cvarr* img,
Cvpoint center,//Center
Cvsize axes,//Shaft
Double angle,//radial angle (angle of horizontal to long axis
From the x-axis, the counter-clockwise direction is positive. )
Double start_angle,//starting angle (angle of the long axis to the starting edge)
Double end_angle,//End Angle (the angle of the long axis to the end point)
Cvscalar Color,
int Thickness=1,
int line_type=8
);

Here, the main new member is the Axes property, which is of type cvsize. The Cvsize function is very similar to Cvpoint and Cvscalar, a simple structure that contains only width and height. Like Cvpoint and Cvscalar, Cvsize also has a simple constructor cvsize (int height,int width) that returns a Cvsize data when needed. In this case, the height and width parameters represent the length of the ellipse in the half-axis length, respectively.
Angle refers to the angle of the deviation from the spindle, from the x-axis, the counter-clockwise direction is positive. Similarly, Start_angle and end_angle represent the angle at which arcs start and end positions. Therefore, a complete ellipse must have these values set to 0° and 360°, respectively.
If set to 0,180, it's half an ellipse--in a clockwise direction.

Next is a program for drawing polygons:

#include <cv.h>
#include 

4.1 cvfillpoly-to draw multiple solid polygons
Its structure
void Cvfillpoly (//Draw multiple solid polygons
cvarr* img,//Canvas Image
cvpoint** pts,//Point Sequence
int* array of npts,//count points
int contours,//displays several polygons
Cvscalar color,//Color
int Line_type = 8
);
In Cvfillpoly (), the point is provided by the Cvpoint array. It allows Cvfillpoly () to draw multiple polygons in a single call. In the same way, the NPT is an array of points, corresponding to the polygon.
Cvfillpoly () is stable and can handle complex problems such as self-intersecting polygons and polygon with holes. Unfortunately, however, the function is relatively slow to run.
Let's first look at this sentence Cvpoint * pt =new cvpoint[3];
Create a pointer to a pointer, then it must be pointing to the pointer--
New Cvpoint*[3]; is a pointer array in which each element is a pointer.
[3] represents 3 elements-the end of the 3 polygons below.
In the function definition:
cvpoint** pts,//Point Sequence
So, the PT we're creating here is the endpoint coordinates of the polygon we're going to create.
Pt[0] = new CVPOINT[4];
Pt[0] refers to the endpoint of the first polygon
PT[1] is the second (subscript starting from 0)
Then we are looking at new cvpoint[4]; It is an element inside the pointer array new CVPOINT*[3].
Pt[0][0] = Cvpoint (10,10);
PT[0][1] = Cvpoint (210,10);
PT[0][2] = Cvpoint (210,210);
PT[0][3] = Cvpoint (10,210);
The 4 cvpoint above are the array that this new cvpoint[4] points to.
It is then important to note that if you create a polygon with 5 points, you write a
Pt[0] = new CVPOINT[5];
Then the coordinates of the 5 points are the same--n points (tried).
Look again int arr[3]= {4,4,4};
What is this for?
The definition says: int* npts,//count Array
Each of these elements corresponds to the number of endpoints of our team's polygons one by one-the order is not chaotic.
If the first polygon defined is a 5-sided shape, then its first element is 5, and so on.
Take a look at the last parameter:
int contours,//displays several polygons
What is this for?
If we define 4 polygons, this parameter is 3, then only the first 3 defined polygons are displayed.

OpenCV there are other functions of drawing geometry, you can find it yourself, here are just a few common functions.

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.