程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Opencv中繪制直線,矩形,圓,橢圓,多邊形(包括多邊形填充),繪制文本,以及綜合實現(python實現)

編輯:Python

文章目錄

    • 1.畫線
    • 2.橢圓
    • 3.繪制圓
    • 4.畫矩形
    • 5.繪制多邊形
    • 6.繪制文本
    • 7.綜合練習

1.畫線

line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)

Img:輸入的原始圖像;
Pt1:起始點坐標;
Pt2:結束點坐標;
Color:使用的顏色;
Thickness:線的寬度;
lineType:線的類型;
Shift:按坐標比例縮放;

def drawline(img_path='images/bg.png'):
img=cv2.imread(img_path)
img=cv2.resize(src=img,dsize=(450,450))
img=cv2.line(img=img,pt1=(0,0),pt2=(350,350),color=(0,255,0),thickness=3)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


2.橢圓

ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness=None, lineType=None, shift=None):

Img:輸入的原始圖像;
Center:中心點坐標;
Axes:長軸和短軸的長度;
Angle:橢圓中心沿逆時針選擇角度;
startAngle:順時針開始的角度;
endAngle:順時針結束的角度;
Color:使用的顏色;
Thickness:線的寬度;
lineType:線的類型;
Shift:按坐標比例縮放;

def drawellipse(img_path='images/bg.png'):
img = cv2.imread(img_path)
img = cv2.resize(src=img, dsize=(450, 450))
img = cv2.ellipse(img=img,center=(200,200),axes=(100,50),angle=0,startAngle=0,endAngle=360,color=(0,255,0),thickness=2)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


3.繪制圓

circle(img, center, radius, color, thickness=None, lineType=None, shift=None):

Img:輸入的原始圖像;
Center:圓的中心點;
Radius:半徑
Color:使用的顏色;
Thickness:線的寬度(-1表示為填充);
lineType:線的類型;
Shift:按坐標比例縮放;

def drawCircle(img_path='images/bg.png'):
img = cv2.imread(img_path)
img = cv2.resize(src=img, dsize=(450, 450))
img = cv2.circle(img=img,center=(200,200),radius=50,color=(0,255,0),thickness=3)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


4.畫矩形

rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)

Img:輸入的原始圖像;
Pt1:起始點坐標;
Pt2:結束點坐標;
Color:使用的顏色;
Thickness:線的寬度;
lineType:線的類型;
Shift:按坐標比例縮放;

def drawRectangle(img_path='images/bg.png'):
img = cv2.imread(img_path)
img = cv2.resize(src=img, dsize=(450, 450))
img = cv2.rectangle(img=img,pt1=(20,20),pt2=(70,70),color=(0,255,0),thickness=3)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


5.繪制多邊形

polylines(img, pts, isClosed, color, thickness=None, lineType=None, shift=None):

img:要在上面畫多邊形的圖像;
pts:包含多邊形上點的數組;
isClosed:所繪制的多邊形是否閉合。若為 True ,則畫若干個閉合多邊形;否則,則畫一條連接所有點的折線
color:多邊形顏色;
thickness:多邊形線的粗細;
lineType:多邊形線的類型;
shift:坐標精確到小數點後第幾位;

def drawPolylines(img_path='images/bg.png'):
img = cv2.imread(img_path)
img = cv2.resize(src=img, dsize=(450, 450))
#繪制多邊形的點坐標
pts=np.array([(25,36),(89,69),(67,78),(90,128)],dtype=np.int32)
img = cv2.polylines(img=img,pts=[pts],isClosed=True,color=(0,255,0),thickness=3)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

填充多邊形

fillPoly(img, pts, color, lineType=None, shift=None, offset=None):

img:要在上面畫多邊形的圖像;
pts:包含多邊形上點的數組;
color:多邊形顏色;
thickness:多邊形線的粗細;
lineType:多邊形線的類型;
shift:坐標精確到小數點後第幾位;
Offset:偏移等高線所有點的可選偏移;

def drawPolylineFulls(img_path='images/bg.png'):
img = cv2.imread(img_path)
img = cv2.resize(src=img, dsize=(450, 450))
#繪制多邊形的點坐標
pts=np.array([(25,36),(89,69),(67,78),(90,128)],dtype=np.int32)
img=cv2.fillPoly(img=img,pts=[pts],color=(0,255,0))
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


6.繪制文本

putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None)

Img:輸入的原始圖像;
Text:繪制的文本;
Org:繪制的起始點;
color:多邊形顏色;
thickness:多邊形線的粗細;
lineType:多邊形線的類型;
bottomLeftOrigin:如果為true,則圖像數據原點位於左下角。否則,它將位於左上角。

def drawPutText(img_path='images/bg.png'):
img = cv2.imread(img_path)
img = cv2.resize(src=img, dsize=(450, 450))
cv2.putText(img=img,text='Scientist',org=(20,50),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=2,color=(0,255,0),thickness=2)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


7.綜合練習

#按下鼠標左鍵開始畫圖形,左鍵放開之後畫出圖形。
#按下q鍵退出,按下L鍵畫線,按下R鍵畫矩形,按下C鍵話圓,按下E鍵的話表示畫橢圓

import os
import cv2
import numpy as np
curshape=0
startpos=(0,0)
endspos=(0,0)
#創建窗口
cv2.namedWindow(winname='drawindow',flags=cv2.WINDOW_AUTOSIZE)
#創建一個白色的背景
def whiteimage(img):
B,G,R=cv2.split(img)
B[:]=255
G[:]=255
R[:]=255
mergeImage=cv2.merge((B,G,R))
return mergeImage
img=np.zeros(shape=(300,450,3),dtype=np.uint8)
img=whiteimage(img)
#獲取TrackBar的值
def TrackBarValue():
#獲取窗口“window”下子窗口“R”的值
value_R= cv2.getTrackbarPos(trackbarname='R', winname='drawindow')
value_G = cv2.getTrackbarPos(trackbarname='G', winname='drawindow')
value_B = cv2.getTrackbarPos(trackbarname='B', winname='drawindow')
return (value_R,value_G,value_B)
#定義回調函數
def callback():
pass
#定義TrackBar函數
def TrackBarBGR():
#value-trackbar的值 count-設置的最大值count(最小值為0) OnChange-回調函數
cv2.createTrackbar('R','drawindow', 0, 255, callback)
cv2.createTrackbar('G','drawindow', 0, 255, callback)
cv2.createTrackbar('B','drawindow', 0, 255, callback)
TrackBarBGR()
#回調函數定義
def mouse_callback(event,x,y,flags,userdata):
global startpos
global endspos
global curshape
if (event&cv2.EVENT_LBUTTONDOWN==cv2.EVENT_LBUTTONDOWN):
startpos=(x,y)
elif (event&cv2.EVENT_LBUTTONUP==cv2.EVENT_LBUTTONUP):
endspos=(x,y)
if curshape==0:
cv2.line(img=img,pt1=startpos,pt2=endspos,color=TrackBarValue(),thickness=3)
elif curshape==1:
cv2.rectangle(img=img,pt1=startpos,pt2=endspos,color=TrackBarValue(),thickness=3)
elif curshape==2:
a=(endspos[0]-startpos[0])
b=(endspos[1]-startpos[1])
radis=np.int32((a**2+b**2)**0.5)
cv2.circle(img=img,center=startpos,radius=radis,color=TrackBarValue(),thickness=3)
elif curshape==3:
a = (endspos[0] - startpos[0])
b = (endspos[1] - startpos[1])
cv2.ellipse(img=img,center=startpos,axes=(a,b),angle=0,startAngle=0,endAngle=360,color=TrackBarValue(),thickness=3)
if (event&cv2.EVENT_LBUTTONDBLCLK==cv2.EVENT_LBUTTONDBLCLK):
text=input('請輸入文字: ')
cv2.putText(img=img,text=text,org=startpos,fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=2,color=TrackBarValue(),thickness=2)
#設置鼠標回調函數
cv2.setMouseCallback('drawindow',mouse_callback)
while True:
cv2.imshow('drawindow',img)
#按下q鍵退出,按下L鍵畫線,按下R鍵畫矩形,按下C鍵話圓,按下E鍵的話表示畫橢圓,按下W鍵進行文本繪制
key=cv2.waitKey(1)
if key==ord('q'):
break
elif key==ord('l'):
curshape=0
elif key==ord('r'):
curshape=1
elif key==ord('c'):
curshape=2
elif key==ord('e'):
curshape=3
elif key==ord('w'):
curshape=4
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')

鼠標事件可以看這位博主:
https://blog.csdn.net/qq_55025358/article/details/124067576


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved