OpenCV基础线条图案函数使用及案例

 

第一部分:Drawing Functions

1、Drawing Line

函数: cv2.line()

例如:cv2.line(img,(0,0),(511,511),(255,0,0),5)

2、Drawing Rectangle

函数: cv2.rectangle()

例如:cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

3、Drawing Circle

函数: cv2.circle()

例如:cv2.circle(img,(447,63), 63, (0,0,255), -1)

4、Drawing Ellipse

函数: cv2.ellipse()

例如:cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

5、Drawing Polygon

函数:cv2.ellipse()

例如:cv2.polylines(img,[pts],True,(0,255,255))

cv2.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is a much better and faster way to draw a group of lines than calling cv2.line() for each line.

6、Adding Text to Imags

函数: cv2.putText()

例如:

cv2.putText(img,'OpenCV',(10,500),font,4,(255,255,255),2,cv2.LINE_AA)

7、The meaning of the some common arguments

  • img : The image where you want to draw the shapes
  • color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness : Thickness of the line or circle etc. If **-1** is passed for closed figures like circles, it will fill the shape. default thickness = 1
  • lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves.
  • Sum all the above functions codes

# -*- coding: utf-8 -*-
import numpy as np
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)          # zeros()函数和np.uint8还不是很理解
# Draw a diagonal blue line with thickness of 5 px
cv2.line(img,(0,0),(511,511),(255,0,0),5)      # 参数1:图像; 参数2:开始坐标; 参数3:结束坐标; 参数4:颜色; 参数5:线条像素
# Drawing Rectangle
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)  # 参数说明同上
# Drawing Circle
cv2.circle(img,(447,63), 63, (0,0,255), -2)   # 参数1:图像; 参数2:圆心坐标; 参数3:圆半径; 参数4:颜色; 参数5:线条像素(负数表示全填充)
# Drawing Ellipse
cv2.ellipse(img,(256,256),(100,50),0,0,180,120,-1)  # 参数1:图像; 参数2:中心位置; 参数3:两个半径长度; 参数4:椭圆旋转角度;
# 参数5:开始旋转角度; 参数6:结束旋转角度; 参数7:颜色; 参数7:线条像素
# Drawing Polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32) # 定义多边形向量
pts = pts.reshape((-1,1,2))  # 这个参数还不是很理解
cv2.polylines(img,[pts],True,(0,255,255))  # 第三个参数设置成False,表示只把所有点联系起来,不是闭合的图形,第四个参数为颜色
# Adding Text to Images
font = cv2.FONT_HERSHEY_SIMPLEX  # 设置字体,可以通过 cv2.putText()查看
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)  # 参数1:图像; 参数2:要写的字体; 参数3:字开始坐标; 参数4:字体;
# 参数5:字体大小; 参数6:颜色; 参数7:  ; 参数7:线条像素
cv2.imshow("Block image",img)   # 展现图片
k = cv2.waitKey(0) & 0xFF         # 等待外部键盘输入
if k == 27:         # wait for ESC key to exit
cv2.destroyAllWindows()       # 销毁
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('D:\drawing.png',img)     #将图片保存到D盘路径下
cv2.destroyAllWindows()                 #销毁图片

8、参考资料

资料1:

http://docs.opencv.org/3.3.0/dc/da5/tutorial_py_drawing_functions.html

资料2( cv2.ellipse()函数参数详解):

http://docs.opencv.org/3.3.0/d6/d6e/group__imgproc__draw.html#ga28b2267d35786f5f890ca167236cbc69

资料3(cv2.putText() 函数参数详解):

http://docs.opencv.org/3.3.0/d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576

资料4(函数ellipse讨论):

http://answers.opencv.org/question/14541/angles-in-ellipse-function/

 

第二部分:Mouse as a Paint-Brush

1、鼠标事件汇总

['EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP']

函数:cv2.setMouseCallback()

 

2、double-click to draw circles

# -*- coding: utf-8 -*-
import cv2
import numpy as np
events = [i for i in dir(cv2) if 'EVENT' in i]
print( events )   #返回的事件结果

# mouse callback function
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
if cv2.waitKey(20) & 0xFF == 27:  # 按ESC退出
break
cv2.destroyAllWindows()

3、More Advanced Demo

# -*- coding: utf-8 -*-
import cv2
import numpy as np
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
# mouse callback function
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)

img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == ord('m'):
mode = not mode
elif k == 27:
break
cv2.destroyAllWindows()

4、参考资料

资料1:

http://docs.opencv.org/3.3.0/db/d5b/tutorial_py_mouse_handling.html

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: