opencv-python 影像 二

weixin_34138377發表於2017-12-18

http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_drawing_functions/py_drawing_functions.html
直線
cv2.line(img,(起始點),(終點),(顏色),px大小)

顏色為 bgr
>>> import cv2
>>> import numpy as np
>>> img=np.zeros((512,512,3),np.uint8)
>>> img = cv2.line(img,(0,0),(511,511),(255,0,0),5)
>>> cv2.imshow("xx",img)

矩形
img=cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

需要指定左上角,到右下角的座標,顏色,px大小
>>> img=cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
>>> cv2.imshow("xx",img)


img = cv2.circle(img,(447,63), 63, (0,0,255), -1)

需要注意的是 -1代表內切圓
2228269-1eefdadf26909fb7.png
image.png

橢圓
img = cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

未試過

多邊形
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,[pts],True,(0,255,255))

未試過
To draw a polygon, first you need coordinates of vertices. Make those points into an array of shape ROWSx1x2 where ROWS are number of vertices and it should be of type int32. Here we draw a small polygon of with four vertices in yellow color.

新增文字
cv2.putText()

但是顯示中文的貌似沒有對應的字型,不知道該怎麼新增
Text data that you want to write
Position coordinates of where you want put it (i.e. bottom-left corner where data starts).
Font type (Check cv2.putText() docs for supported fonts)
Font Scale (specifies the size of font)
regular things like color, thickness, lineType etc. For better look, lineType = cv2.LINE_AA is recommended.

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

相關文章