引言
Python OpenCV 是一個(gè)功能強(qiáng)大的計(jì)算機(jī)視覺庫,除了圖像處理和計(jì)算機(jī)視覺任務(wù)外,它還提供了豐富的功能來繪制各種圖形。無論是在計(jì)算機(jī)視覺應(yīng)用中標(biāo)記感興趣區(qū)域,還是在圖像上繪制幾何形狀或文本,OpenCV 都為我們提供了簡(jiǎn)單易用的方法。本文將介紹如何利用 Python OpenCV 進(jìn)行圖形繪制。
1. 創(chuàng)建畫布
在開始圖形繪制之前,我們首先需要?jiǎng)?chuàng)建一個(gè)空白的畫布。在 OpenCV 中,我們可以使用 cv2.imread()
函數(shù)加載圖像,或使用 np.zeros()
創(chuàng)建一個(gè)空白的圖像作為畫布。
示例代碼:
import cv2
import numpy as np
# 創(chuàng)建一張空白的畫布
canvas = np.zeros((500, 500, 3), dtype=np.uint8)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述代碼中,我們使用 np.zeros()
函數(shù)創(chuàng)建了一個(gè)形狀為 (500, 500, 3) 的零數(shù)組,表示畫布的寬度、高度和通道數(shù)。然后,我們使用 cv2.imshow()
函數(shù)顯示畫布。
2. 繪制線段
繪制線段是圖形繪制中的基本操作之一。在 OpenCV 中,我們可以使用 cv2.line()
函數(shù)繪制線段。
示例代碼:
import cv2
# 在畫布上繪制一條線段
start_point = (100, 100)
end_point = (400, 400)
color = (0, 0, 255) # 紅色線段
thickness = 3
cv2.line(canvas, start_point, end_point, color, thickness)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述代碼中,我們使用 cv2.line()
函數(shù)在畫布上繪制了一條從 (100, 100) 到 (400, 400) 的紅色線段。我們還可以通過調(diào)整 thickness
參數(shù)來設(shè)置線段的粗細(xì)。
3. 繪制矩形
繪制矩形是常見的圖形繪制操作之一。在 OpenCV 中,我們可以使用 cv2.rectangle()
函數(shù)繪制矩形。
示例代碼:
import cv2
# 在畫布上繪制一個(gè)矩形
top_left = (200, 200)
bottom_right = (400, 400)
color = (0, 255, 0) # 綠色矩形
thickness = 2
cv2.rectangle(canvas, top_left, bottom_right, color, thickness)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述代碼中,我們使用 cv2.rectangle()
函數(shù)在畫布上繪制了一個(gè)從 (200, 200) 到 (400, 400) 的綠色矩形。我們可以通過調(diào)整 thickness
參數(shù)來設(shè)置矩形的邊框粗細(xì)。
4. 繪制圓
繪制圓形也是常見的圖形繪制操作之一。在 OpenCV 中,我們可以使用 cv2.circle()
函數(shù)繪制圓形。
示例代碼:
import cv2
# 在畫布上繪制一個(gè)圓形
center = (300, 300)
radius = 100
color = (255, 0, 0) # 藍(lán)色圓形
thickness = -1 # 填充圓形
cv2.circle(canvas, center, radius, color, thickness)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述代碼中,我們使用 cv2.circle()
函數(shù)在畫布上繪制了一個(gè)以 (300, 300) 為圓心、半徑為 100 的藍(lán)色填充圓形。我們可以通過調(diào)整 thickness
參數(shù)來設(shè)置圓形的邊框粗細(xì),負(fù)值表示填充圓形。
5. 繪制橢圓
繪制橢圓也是常見的圖形繪制操作之一。在 OpenCV 中,我們可以使用 cv2.ellipse()
函數(shù)繪制橢圓。
示例代碼:
import cv2
# 在畫布上繪制一個(gè)橢圓
center = (250, 250)
axes = (150, 100)
angle = 0
start_angle = 0
end_angle = 360
color = (0, 255, 255) # 黃色橢圓
thickness = 2
cv2.ellipse(canvas, center, axes, angle, start_angle, end_angle, color, thickness)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述代碼中,我們使用 cv2.ellipse()
函數(shù)在畫布上繪制了一個(gè)以 (250, 250) 為中心、長(zhǎng)軸為 150、短軸為 100 的黃色橢圓。我們可以通過調(diào)整 thickness
參數(shù)來設(shè)置橢圓的邊框粗細(xì)。
6. 繪制多邊形
繪制多邊形是繪制復(fù)雜形狀的常見操作。在 OpenCV 中,我們可以使用 cv2.polylines()
函數(shù)繪制多邊形。
示例代碼:
import cv2
# 在畫布上繪制一個(gè)多邊形
points = np.array([[100, 100], [200, 50], [300, 150], [250, 200]], np.int32)
points = points.reshape((-1, 1, 2))
color = (255, 255, 0) # 青色多邊形
thickness = 2
cv2.polylines(canvas, [points], True, color, thickness)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述代碼中,我們使用 cv2.polylines()
函數(shù)在畫布上
繪制了一個(gè)由多個(gè)頂點(diǎn)構(gòu)成的青色多邊形。points
是一個(gè)包含多個(gè)頂點(diǎn)坐標(biāo)的數(shù)組,我們可以根據(jù)需要添加更多的頂點(diǎn)。我們可以通過調(diào)整 thickness
參數(shù)來設(shè)置多邊形的邊框粗細(xì)。
7. 繪制字體
在圖形繪制中,有時(shí)需要在圖像上添加文本標(biāo)簽。在 OpenCV 中,我們可以使用 cv2.putText()
函數(shù)在圖像上繪制文本。
示例代碼:
import cv2
# 在畫布上繪制文本
text = 'OpenCV'
position = (200, 250)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1.5
color = (255, 255, 255) # 白色文本
thickness = 2
cv2.putText(canvas, text, position, font, font_scale, color, thickness)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述代碼中,我們使用 cv2.putText()
函數(shù)在畫布上繪制了一個(gè)白色文本 “OpenCV”,位于 (200, 250) 的位置。我們可以通過調(diào)整 font_scale
參數(shù)來設(shè)置文本的大小,通過調(diào)整 thickness
參數(shù)來設(shè)置文本的粗細(xì)。
文章來源:http://www.zghlxwxcb.cn/news/detail-500389.html
結(jié)論
通過本文的介紹,我們了解了如何使用 Python OpenCV 進(jìn)行圖形繪制。我們可以創(chuàng)建一個(gè)畫布,并利用 cv2.line()
、cv2.rectangle()
、cv2.circle()
、cv2.ellipse()
、cv2.polylines()
和 cv2.putText()
函數(shù)繪制線段、矩形、圓形、橢圓、多邊形和文本。這些圖形繪制操作在計(jì)算機(jī)視覺任務(wù)和圖像處理中非常有用。希望本文能夠幫助您掌握 Python OpenCV 的圖形繪制功能,并在實(shí)際項(xiàng)目中應(yīng)用它們。文章來源地址http://www.zghlxwxcb.cn/news/detail-500389.html
到了這里,關(guān)于【CV 向】OpenCV 圖形繪制指南的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!