OpenCV Tutorials--basic Drawing

Source: Internet
Author: User
Tags scalar

Point

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

Point pt;pt.x = 10;pt.y = 8;

or Point pt = Point (10, 8);

Scalar
  • Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel values.

  • Represent RGB color values (3 parameters). It's not necessary to define the last argument if it's not going to being used.

  • Scalar (A, B, c)

    We would be defining a RGB color such as: Red = C, Green = b and Blue = a

Create Black empty Imagesmat atom_image = Mat::zeros (W, W, CV_8UC3); Mat rook_image = Mat::zeros (W, W, CV_8UC3);
1. Draw a simple atom:///1.a. Creating ellipsesmyellipse (Atom_image, 90); Myellipse (atom_image, 0); Myellipse (Atom_image, 45); Myellipse (Atom_image, -45);///1.b. Creating circlesmyfilledcircle (Atom_image, point (w/2.0, w/2.0));

Line (img,start,end,scalar (0, 0, 0), thickness,linetype);
  • Draw a line from point start to point end
  • The line was displayed in the Image img
  • The line color are defined by Scalar (0, 0, 0) which are the RGB value correspondent to Black
  • The line thickness are set to thickness (in this case 2)
  • The line is a 8-connected one (linetype = 8)

Ellipse (Img,point (w/2.0, w/2.0), Size (w/4.0, w/16.0), Angle,0,360,scalar (255, 0, 0), thickness,linetype);
  • The ellipse is displayed in the Image img
  • The Ellipse center is located in the point (w/2.0, w/2.0) and are enclosed in a box of size (w/4.0, w/16.0)
  • The ellipse is rotated angle degrees
  • The ellipse extends an arc between 0 and degrees
  • The color of the figure would be a Scalar (255, 255, 0) which means blue in RGB value.
  • The ellipse ' s thickness is 2.

Circle (img,center,w/32.0,scalar (0, 0, 255), thickness,linetype);
  • The image where the circle would be displayed (img)
  • The center of the circle denoted as the point Center
  • The radius of the circle: w/32.0
  • The color of the circle: Scalar (0, 0, 255) which means Red in BGR
  • Since Thickness =-1, the circle would be drawn filled.

Fillpoly (Img,ppt,npt,1,scalar (255, 255, 255), linetype);
  • The polygon is drawn on img
  • The vertices of the polygon is the set of points in ppt
  • The total number of vertices to be drawn is NPT
  • The number of polygons to be drawn are only 1
  • The color of the polygon is defined by Scalar (255, 255, 255), which are the BGR value for White

Rectangle (rook_image,point (0, 7*w/8.0), Point (W, W), Scalar (0, 255, 255), -1,8);
  • The rectangle is drawn on rook_image
  • Opposite vertices of the rectangle is defined by * * Point (0, 7*w/8.0) * * and Point (W, W)
  • The color of the rectangle is given by Scalar (0, 255, 255) which are the BGR value for yellow
  • Since the thickness value is given by -1, the rectangle would be filled.

Are all calls to basic functions, note the creation of new images--zeros

It's also very sensible to define the window scale.

#include "stdafx.h" #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include < Opencv2/highgui/highgui.hpp> #define W 400using namespace cv;///Function headersvoid myellipse (Mat img, double angle)  void Myfilledcircle (Mat img, point Center), void MyPolygon (Mat img), void Myline (Mat img, point-Start, point end);/**  * @function Main * @brief main function */int main (void) {///Windows names char atom_window[] = "Drawing 1:atom";  Char rook_window[] = "Drawing 2:rook";  Create Black empty images Mat Atom_image = Mat::zeros (W, W, CV_8UC3);  Mat rook_image = Mat::zeros (W, W, CV_8UC3); 1.  Draw a simple atom:///-----------------------//1.a. Creating ellipses Myellipse (Atom_image, 90);  Myellipse (atom_image, 0);  Myellipse (Atom_image, 45);  Myellipse (Atom_image,-45);  1.b. Creating circles Myfilledcircle (atom_image, point (W/2, W/2)); 2. Draw a rook//------------------//2.a. Create a convex polyGon MyPolygon (Rook_image); 2.b. Creating rectangles Rectangle (rook_image, point (0, 7*W/8), Point (W, W), Scalar (0, 2  55, 255),-1, 8);  2.c. Create a few lines 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)); 3.  Display your stuff!  Imshow (Atom_window, atom_image);  MoveWindow (Atom_window, 0, 200);  Imshow (Rook_window, rook_image);  MoveWindow (Rook_window, W, 200);  Waitkey (0); return (0);} Function declaration/** * @function myellipse * @brief Draw a fixed-size ellipse with different angles */void myellips  E (Mat img, double angle) {int thickness = 2;  int linetype = 8; Ellipse (IMG, point (W/2, W/2), Size (W/4, W/16), angle, 0,, Scalar (255, 0, 0) , ThicknESS, Linetype);} /** * @function myfilledcircle * @brief Draw a fixed-size filled circle */void myfilledcircle (Mat img, point center) {I  NT thickness =-1;  int linetype = 8; Circle (IMG, center, W/32, Scalar (0, 0, 255), thickness, linetype);}   /** * @function MyPolygon * @function Draw a simple concave polygon (rook) */void mypolygon (Mat img) {int linetype = 8;  /** Create Some points */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);}  /** * @function myline * @brief Draw a simple line */void myline (Mat-img, point-Start, point end) {int thickness = 2;  int linetype = 8; Line (IMG, start, end, Scalar (0, 0, 0), thickness, linetype);}

OpenCV Tutorials--basic Drawing

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.