使用Python,OpenCV进行涂鸦(绘制文字、线、圆、矩形、椭圆、多边形轮廓、多边形填充、箭头~)
生活随笔
收集整理的這篇文章主要介紹了
使用Python,OpenCV进行涂鸦(绘制文字、线、圆、矩形、椭圆、多边形轮廓、多边形填充、箭头~)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用Python,OpenCV進(jìn)行涂鴉(繪制文字、線、圓、矩形、橢圓、多邊形輪廓、多邊形填充、箭頭)
- 1. 效果圖
- 2. 原理
- 2.1 繪制線:cv2.line(canvas, (300, 0), (0, 300), (255, 0, 255), 3)
- 2.2 繪制矩形:cv2.rectangle(canvas, (200, 50), (225, 125), (0, 0, 255), -1)
- 2.3 繪制圓:cv2.circle(canvas, (200, 50), 5, (255, 0, 0), -1)
- 2.4 繪制橢圓:cv2.ellipse(canvas, (200, 200), (150, 100), -60, 0, 300, (255, 0, 0), -1)
- 2.5 繪制文字:cv2.putText(canvas, "Beautiful", (30, 100), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 1)
- 2.6 NumPy的隨機(jī)數(shù)功能:np.random.randint
- 3. 源碼
- 參考
這篇博客將介紹使用Python,OpenCV繪制文字、線、圓、矩形、橢圓、多邊形輪廓、多邊形填充、箭頭。
1. 效果圖
線效果圖如下:
矩形效果圖如下:
圓效果圖靶子如下:
隨機(jī)圓心、顏色、半徑效果圖如下:
橢圓效果圖如下:
繪制文字效果圖如下:
繪制多邊形輪廓,多邊形填充效果圖如下:
隨機(jī)顏色,起終點(diǎn)箭頭效果圖如下:
2. 原理
2.1 繪制線:cv2.line(canvas, (300, 0), (0, 300), (255, 0, 255), 3)
- image 畫布
- pt1 起點(diǎn)
- pt2 終點(diǎn)
- color 顏色
- thickness 線的寬度
2.2 繪制矩形:cv2.rectangle(canvas, (200, 50), (225, 125), (0, 0, 255), -1)
- canvas 要繪制的畫布
- pt1 起始點(diǎn)坐標(biāo)
- pt2 終止點(diǎn)坐標(biāo)
- color 外輪廓線的顏色
- thickness 畫筆的厚度,負(fù)數(shù):填充,整數(shù),輪廓線的寬度
2.3 繪制圓:cv2.circle(canvas, (200, 50), 5, (255, 0, 0), -1)
- image 畫布
- center 圓心坐標(biāo)
- radius 半徑
- color 顏色
- thickness 正數(shù):線條寬度,負(fù)數(shù): 填充區(qū)域
2.4 繪制橢圓:cv2.ellipse(canvas, (200, 200), (150, 100), -60, 0, 300, (255, 0, 0), -1)
- img 圖像
- (int(cX), int(cY)) 中心
- (150,100) 長(zhǎng)軸、短軸
- angle 旋轉(zhuǎn)角度,負(fù)的逆時(shí)針旋轉(zhuǎn),正的順時(shí)針旋轉(zhuǎn);
- start_angle 開始角度
- end_angle 結(jié)束角度
- color 顏色
- thickness 負(fù)數(shù)填充,正數(shù)線條粗細(xì)
2.5 繪制文字:cv2.putText(canvas, “Beautiful”, (30, 100), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 1)
- img 圖片
- “Good job” 文字內(nèi)容
- (int(cX - 10), int(cY)) 繪制起始的像素
- cv2.FONT_HERSHEY_SIMPLEX
- 0.45 基于字體特定基大小的比例因子
- (0, 255, 0) 顏色
- 2 繪制文本的線條粗細(xì)
2.6 NumPy的隨機(jī)數(shù)功能:np.random.randint
隨機(jī)生成1個(gè)數(shù)(5,200之間):radius = np.random.randint(5, high=200)
隨機(jī)生成3個(gè)數(shù)(0,256之間)color = np.random.randint(0, high=256, size=(3,)).tolist()
3. 源碼
# 繪制文字、線、圓、矩形、橢圓、多邊形輪廓、多邊形填充、箭頭
# USAGE
# python basic_drawing.py# 導(dǎo)入必要的包
import numpy as np
import cv2# 初始化空數(shù)組圖像并繪制
# 初始化畫布(300*300)的3通道
canvas = np.zeros((300, 300, 3), dtype="uint8")
# 繪制文字
# - img 圖片
# - "Good job" 文字內(nèi)容
# - (int(cX - 10), int(cY)) 繪制起始的像素
# - cv2.FONT_HERSHEY_SIMPLEX
# - 0.45 基于字體特定基大小的比例因子
# - (0, 255, 0) 顏色
# - 2 繪制文本的線條粗細(xì)
cv2.putText(canvas, "Beautiful", (30, 100), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 1)
cv2.putText(canvas, "Excellent", (50, 150), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 0, 0), 2)
cv2.putText(canvas, "Good job", (150, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 5)
cv2.putText(canvas, "Marvelously", (20, 250), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 255), 3)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 初始化空數(shù)組圖像并繪制
# 初始化畫布(300*300)的3通道
canvas = np.zeros((300, 300, 3), dtype="uint8")# 從左上到右下繪制一條綠色的線
green = (0, 255, 0)
cv2.line(canvas, (0, 0), (300, 300), green)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 從右上到左下繪制一條寬度為3像素的紅色線
red = (0, 0, 255)
# 繪制線
# --image 畫布
# --pt1 起點(diǎn)
# --pt2 終點(diǎn)
# --color 顏色
# --thickness 線的寬度
cv2.line(canvas, (300, 0), (0, 300), red, 3)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)cv2.line(canvas, (50, 100), (300, 250), (0, 255, 255), 2, cv2.LINE_8, 3)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 繪制一個(gè)50*50的綠色矩形(從(10,10)到(60*60)
canvas = np.zeros((300, 300, 3), dtype="uint8")
cv2.rectangle(canvas, (10, 10), (60, 60), green)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 繪制另一個(gè)寬度為像素的紅色矩形,從(50,200)到(200,225)
cv2.rectangle(canvas, (50, 200), (200, 225), red, 5)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 繪制一個(gè)藍(lán)色填充的矩形
blue = (255, 0, 0)
# --canvas 要繪制的畫布
# --pt1 起始點(diǎn)坐標(biāo)
# --pt2 終止點(diǎn)坐標(biāo)
# --color 外輪廓線的顏色
# --thickness 畫筆的厚度,負(fù)數(shù):填充,整數(shù),輪廓線的寬度
cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 重新初始化一塊畫布,計(jì)算畫布的中心
canvas = np.zeros((300, 300, 3), dtype="uint8")
print(canvas.shape[1] / 2, canvas.shape[0] / 2)
print(canvas.shape[1] // 2, canvas.shape[0] // 2)
print((int)(canvas.shape[1] / 2), (int)(canvas.shape[0] / 2))
(centerX, centerY) = (canvas.shape[1] // 2, canvas.shape[0] // 2)
white = (255, 255, 255)# 遍歷不同的圓半徑,繪制多個(gè)圓
for r in range(0, 175, 25):# 繪制白色圓cv2.circle(canvas, (centerX, centerY), r, white)
# 展示畫布最終效果
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 重新初始化一塊畫布
canvas = np.zeros((300, 300, 3), dtype="uint8")
# 隨機(jī)繪制25個(gè)圓
for i in range(0, 25):# 隨機(jī)生成5~200的半徑# 隨機(jī)生成顏色# 隨機(jī)取一個(gè)點(diǎn)作為圓心radius = np.random.randint(5, high=200)color = np.random.randint(0, high=256, size=(3,)).tolist()pt = np.random.randint(0, high=300, size=(2,))# 繪制圓在畫布上,用填充色# --image 畫布# --center 圓心坐標(biāo)# --radius 半徑# --color 顏色# --thickness 正數(shù):線條寬度,負(fù)數(shù): 填充區(qū)域cv2.circle(canvas, tuple(pt), radius, color, -1)# 展示杰作到屏幕
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 重新初始化一塊畫布
canvas = np.zeros((400, 400, 3), dtype="uint8")
# 繪制橢圓
# - img 圖像
# - (int(cX), int(cY)) 中心
# - (150,100) 長(zhǎng)軸,短軸
# - angle 旋轉(zhuǎn)角度,負(fù)的逆時(shí)針旋轉(zhuǎn),正的順時(shí)針旋轉(zhuǎn);
# - start_angle 開始角度
# - end_angle 結(jié)束角度
# - (90,90,90) 顏色
# - -1 負(fù)數(shù):填充,正數(shù):線條粗細(xì)
cv2.ellipse(canvas, (250, 200), (150, 100), -60, 0, 300, (233, 0, 233), -1)
cv2.ellipse(canvas, (100, 100), (80, 40), 0, 0, 280, (255, 255, 0), 2)
cv2.ellipse(canvas, (80, 300), (60, 30), 90, 30, 360, (0, 255, 255), 4)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# 重新初始化一塊畫布
canvas = np.zeros((400, 400, 3), dtype="uint8")
pts = np.random.randint(50, 300, size=((8, 2)))
print(pts.shape)
print(pts)
# 繪制多邊形的輪廓
cv2.polylines(canvas, [pts], isClosed=True, color=(255, 0, 255), thickness=1)
cv2.imshow("polylines", canvas)
cv2.waitKey(0)# 繪制多邊形填充
cv2.fillPoly(canvas, [pts], color=(255, 255, 0))
cv2.imshow("fillPoly", canvas)
cv2.waitKey(0)# 重新初始化一塊畫布
canvas = np.zeros((400, 400, 3), dtype="uint8")
# 繪制箭頭
cv2.arrowedLine(canvas, (300, 100), (250, 300), (0, 233, 220), 3)
for i in range(6):color = np.random.randint(0, high=256, size=(3,)).tolist()pt = np.random.randint(0, high=300, size=(2,))pt2 = np.random.randint(0, high=300, size=(2,))cv2.arrowedLine(canvas, tuple(pt), tuple(pt2), color, 3)cv2.imshow("arrowedLine", canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
參考
- https://www.pyimagesearch.com/2021/01/27/drawing-with-opencv/
總結(jié)
以上是生活随笔為你收集整理的使用Python,OpenCV进行涂鸦(绘制文字、线、圆、矩形、椭圆、多边形轮廓、多边形填充、箭头~)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mapbox gl tile瓦片渲染点以
- 下一篇: Python必备基本技能——命令行参数a