OpenCV (c + +) Basic drawing

Source: Internet
Author: User
Tags scalar

Reference:
1, https://docs.opencv.org/3.2.0/
2, https://github.com/opencv/opencv/ Basic Drawing use CV:: Point in the image defined 2D points. Use CV:: Scalar and why it is useful use the OPENCV function CV:: line draw a thread using the OPENCV function CV:: Ellipse draw an ellipse using the OPENCV function CV:: Rectangle draw a rectangle using the OPENCV function CV :: Circle Draw a circle using the OPENCV function CV:: Fillpoly draw a filled polygon OpenCV theory

For this tutorial, we will use a large number of two structures: CV:: Point and CV:: Scalar: Point

It represents a 2D point, which is specified by its image coordinates x x and Y y. We can define it as:

Point pt;
Pt.x = ten;
Pt.y = 8;

Or

Point pt = Point  (10, 8);
ScalarRepresents a vector of 4 elements. Scalar types are widely used for OPENCV passing pixel values. In this tutorial, we will use it extensively to represent BGR color values (3 parameters). If you do not intend to use the last parameter, you do not need to define it. Let's look at an example, if we ask for a color parameter, we give:
Scalar (A, B, c)

We will define the BGR color, for example: Blue = A, green = b and red = C Code This code is in your OpenCV sample folder.
Otherwise, you can get it from here

#include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #define W
using namespace CV;
void Myellipse (Mat img, double angle);
void Myfilledcircle (Mat img, point Center);
void MyPolygon (Mat img);
void Myline (Mat img, point Start, point end);
  int main (void) {char atom_window[] = "Drawing 1:atom";
  Char rook_window[] = "Drawing 2:rook";
  Mat atom_image = Mat::zeros (W, W, CV_8UC3);
  Mat rook_image = Mat::zeros (W, W, CV_8UC3);
  Myellipse (Atom_image, 90);
  Myellipse (atom_image, 0);
  Myellipse (Atom_image, 45);
  Myellipse (Atom_image,-45);
  Myfilledcircle (Atom_image, point (W/2, W/2));
  MyPolygon (Rook_image); 
         Rectangle (rook_image, point (0, 7*W/8), Point (W, W), Scalar (0, 255, 255), filled,
  Line_8);
  Myline (rook_image, point (0, 15*W/16), point (W, 15*w/16));
  Myline (Rook_image, point (W/4, 7*W/8), point (W/4, W)); Myline (Rook_image, PoiNT (W/2, 7*W/8), point (W/2, W));
  Myline (Rook_image, point (3*W/4, 7*W/8), point (3*W/4, W));
  Imshow (Atom_window, atom_image);
  MoveWindow (Atom_window, 0, 200);
  Imshow (Rook_window, rook_image);
  MoveWindow (Rook_window, W, 200);
  Waitkey (0);
return (0);
  } void Myellipse (Mat img, double angle) {int thickness = 2;
  int linetype = 8; Ellipse (IMG, point (W/2, W/2), Size (W/4, W/16), angle, 0, 360, Scalar (255,
0, 0), thickness, linetype);
      } void Myfilledcircle (Mat img, point Center) {Circle (IMG, center, W/32, Scalar (0, 0, 255),
Filled, line_8);
  } void MyPolygon (Mat img) {int linetype = Line_8;
  Point Rook_points[1][20];
  Rook_points[0][0] = Point (W/4, 7*W/8);
  ROOK_POINTS[0][1] = Point (3*W/4, 7*W/8);
  ROOK_POINTS[0][2] = Point (3*W/4, 13*W/16);
  ROOK_POINTS[0][3] = Point (11*W/16, 13*W/16); ROOK_POINTS[0][4] = Point (19*W/32,  3*W/8);
  ROOK_POINTS[0][5] = Point (3*W/4, 3*W/8);
  ROOK_POINTS[0][6] = Point (3*W/4, W/8);
  ROOK_POINTS[0][7] = Point (26*W/40, W/8);
  ROOK_POINTS[0][8] = Point (26*W/40, W/4);
  ROOK_POINTS[0][9] = Point (22*W/40, W/4);
  ROOK_POINTS[0][10] = Point (22*W/40, W/8);
  ROOK_POINTS[0][11] = Point (18*W/40, W/8);
  ROOK_POINTS[0][12] = Point (18*W/40, W/4);
  ROOK_POINTS[0][13] = Point (14*W/40, W/4);
  ROOK_POINTS[0][14] = Point (14*W/40, W/8);
  ROOK_POINTS[0][15] = Point (W/4, W/8);
  ROOK_POINTS[0][16] = Point (W/4, 3*W/8);
  ROOK_POINTS[0][17] = Point (13*W/32, 3*W/8);
  ROOK_POINTS[0][18] = Point (5*W/16, 13*W/16);
  ROOK_POINTS[0][19] = Point (W/4, 13*W/16);
  Const point* Ppt[1] = {rook_points[0]};
  int npt[] = {20};
Fillpoly (IMG, PPT, NPT, 1, Scalar (255, 255, 255), linetype);
  } void Myline (Mat img, point Start, point end) {int thickness = 2; int LInetype = Line_8;
Line (IMG, start, end, Scalar (0, 0, 0), thickness, linetype); }
explain

1. Since we're going to draw two examples (an atom and a rook), we have to create two images and two windows to display them.

Char atom_window[] = "Drawing 1:atom";
  Char rook_window[] = "Drawing 2:rook";
  Mat atom_image = Mat::zeros (W, W, CV_8UC3);
  Mat rook_image = Mat::zeros (W, W, CV_8UC3);

2, we created a function to draw different geometric shapes. For example, we use Myellipse and myfilledcircle to draw atom:

Myellipse (Atom_image,);
  Myellipse (atom_image, 0);
  Myellipse (Atom_image,);
  Myellipse (Atom_image, -45);
  Myfilledcircle (Atom_image, point (W/2, W/2));

3, in order to draw out rook we use Myline,rectangle and MyPolygon:

MyPolygon (rook_image);
  Rectangle (rook_image, point
         (0, 7*W/8), point
         (W, W),
         Scalar (0, 255, 255),
         filled, line_8
         );
  Myline (rook_image, point (0, 15*W/16), point (W, 15*w/16));
  Myline (Rook_image, point (W/4, 7*W/8), point (W/4, W));
  Myline (Rook_image, point (W/2, 7*W/8), point (W/2, W));
  Myline (Rook_image, point (3*W/4, 7*W/8), point (3*W/4, W));

4, let's look at the contents of each function: Myline

void Myline (Mat img, point Start, point end)
{
  int thickness = 2;
  int linetype = Line_8;
  Line (IMG,
    start, End
    ,
    Scalar (0, 0, 0),
    thickness,
    linetype);

As we can see, myline simply calls the function CV:: line, which performs the following actions: From point start to point end to draw a wire the line displayed in the image img Centerline color is defined by scalar (0,0,0), which is the RGB value corresponding to the black The line thickness is set to thickness (in this case 2) the row is a 8-connected (Linetype = 8) myellipse

void Myellipse (Mat img, double angle)
{
  int thickness = 2;
  int linetype = 8;
  Ellipse (IMG, point
       (W/2, W/2),
       Size (W/4, w/16),
       angle,
       0,
       360, Scalar
       (255, 0, 0),
       thic Kness,
       linetype);

From the above code, we can observe the function CV:: Ellipse draws an ellipse, makes: The ellipse is shown in the image img in the center of the ellipse in the point (W/2,W/2) , and is surrounded in a box (W/4,W/16) Ellipse is the angle angle The ellipse extends between 0 and 360 the color of an arc diagram will be Scalar (255,0,0), which means that the BGR value is blue. The thickness of the ellipse is 2. Myfilledcircle

void Myfilledcircle (Mat img, point Center)
{
  Circle (img,
      Center,
      w/32,
      Scalar (0, 0, 255), 
  filled,
      line_8);

Similar to an elliptical function, we can observe that the circle is received as a parameter: the center of the circle that displays the Circle ( img ) is the radius of the circle of Point Center ( Center ): w/32 Circle Color: Scalar (0,0,255) , which means red in BGR because of thickness =-1, the circle will be filled. MyPolygon

void MyPolygon (Mat img) {int linetype = Line_8;
  Point Rook_points[1][20];
  Rook_points[0][0] = Point (W/4, 7*W/8);
  ROOK_POINTS[0][1] = Point (3*W/4, 7*W/8);
  ROOK_POINTS[0][2] = Point (3*W/4, 13*W/16);
  ROOK_POINTS[0][3] = Point (11*W/16, 13*W/16);
  ROOK_POINTS[0][4] = Point (19*W/32, 3*W/8);
  ROOK_POINTS[0][5] = Point (3*W/4, 3*W/8);
  ROOK_POINTS[0][6] = Point (3*W/4, W/8);
  ROOK_POINTS[0][7] = Point (26*W/40, W/8);
  ROOK_POINTS[0][8] = Point (26*W/40, W/4);
  ROOK_POINTS[0][9] = Point (22*W/40, W/4);
  ROOK_POINTS[0][10] = Point (22*W/40, W/8);
  ROOK_POINTS[0][11] = Point (18*W/40, W/8);
  ROOK_POINTS[0][12] = Point (18*W/40, W/4);
  ROOK_POINTS[0][13] = Point (14*W/40, W/4);
  ROOK_POINTS[0][14] = Point (14*W/40, W/8);
  ROOK_POINTS[0][15] = Point (W/4, W/8);
  ROOK_POINTS[0][16] = Point (W/4, 3*W/8);
  ROOK_POINTS[0][17] = Point (13*W/32, 3*W/8); ROOK_POINTS[0][18] = Point (5*w/, 13*W/16);
  ROOK_POINTS[0][19] = Point (W/4, 13*W/16);
  Const point* Ppt[1] = {rook_points[0]};
  int npt[] = {20};
Fillpoly (IMG, PPT, NPT, 1, Scalar (255, 255, 255), linetype); }

To draw a filled polygon, we use the function CV:: Fillpoly. We note that the polygon will draw the vertex of the polygon on the img is the set of points in ppt The total number of vertices to draw is the NPT the number of polygons to draw is only 1 * The color of the polygon is Scalar (255,255,255) definition, it is a white BGR value rectangle

Rectangle (rook_image, point
         (0, 7*W/8), point
         (W, W),
         Scalar (0, 255, 255),
         filled, line_8
         );

Finally we have CV:: Rectangle function (We don't create a special function for this person). We note that the rectangle will be drawn on the rook_image * Point (0,7 w/8) and **point (w,w) to define the rectangle's two relative vertex rectangles of color by Scalar (0, 255,255) Given that it is yellow BGR value because the thickness value is given by filled ( -1) , the rectangle is filled.

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.