Python OpenCV using Notes (ii) (Simple geometric image drawing)

Source: Internet
Author: User

Simple geometric images generally include points, lines, matrices, circles, ellipses, polygons, and so on. First of all, recognize OpenCV's definition of pixel points. One pixel of the image has 1 or 3 values, a grayscale value for the grayscale image, and a pixel value of 3 values for the color image, showing a different color.
Then there is a point to make a variety of polygons.

(i) Draw a line first

Function: Cv2.line (img,point pt1,point pt2,color,thickness=1,line_type=8 shift=0)
The representative with a value has a default value, which does not have to be given. You can see that this function mainly accepts the coordinates of the parameter two points, the color of the line (color image is a 1*3 array) as follows:

import cv2importas npfromimportas pltimg = np.zeros((512,512),np.uint8)#生成一个空灰度图像cv2.line(img,(0,0),(511,511),255,5)plt.imshow(img,‘gray‘)

import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.line(img,(0,0),(511,511),(155,155,155),5)plt.imshow(img,‘brg‘)

(ii) Draw a rectangle

Functions: Cv2.rectangle (IMG, (380,0), (511,111), (255,0,0), 3), need to determine the two points of the rectangle (upper left and bottom right), color, the type of line (not set to default).
For example:

import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.rectangle(img,(20,20),(411,411),(55,255,155),5)plt.imshow(img,‘brg‘)

(iii) Draw round

It is also simple to draw a circle, just to determine the center and radius, the function:
Cv2.circle (IMG, (380,0), 63, (255,0,0), 3), such as:

import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.circle(img,(200,200),50,(55,255,155),1)#修改最后一个参数plt.imshow(img,‘brg‘)

import cv2importas npfromimportas pltimg = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像cv2.circle(img,(200,200),50,(55,255,155),8)#修改最后一个参数plt.imshow(img,‘brg‘)

(d) Draw an ellipse

The

Ellipse is more complex, involving the long axis short axis, ellipse center, rotation angle, and so on, see the OpenCV Reference manual:

Image as follows:

An example is as follows:

import  cv2import  numpy as  npfrom  Matplotlib import  pyplot as  pltimg = Np.zeros (( Span class= "Hljs-number" >512 , 512 , 3 ), np.uint8)  #生成一个空彩色图像  cv2.ellipse (img, 256 , Span class= "Hljs-number" >256 ), (150 , 100  ), 0 , 0 , 180 , Span class= "Hljs-number" >250 ,-1 )  #注意最后一个参数-1, Indicates that the image is populated, is not populated by default, and if omitted, only the ellipse outlines the  plt.imshow (img,)  

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python OpenCV using Notes (ii) (Simple geometric image 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.