《數(shù)字圖像處理-OpenCV/Python》連載(22)繪制直線與線段
本書京東優(yōu)惠購(gòu)書鏈接:https://item.jd.com/14098452.html
本書CSDN獨(dú)家連載專欄:https://blog.csdn.net/youcans/category_12418787.html
第 4 章 繪圖與鼠標(biāo)交互
本章介紹OpenCV的繪圖功能和簡(jiǎn)單的鼠標(biāo)交互處理方法。與Excel或Matplotlib中的可視化數(shù)據(jù)圖不同,OpenCV中的繪圖功能主要用于在圖像的指定位置繪制幾何圖形。
本章內(nèi)容概要
- 學(xué)習(xí)OpenCV繪圖的基本方法和參數(shù)。
- 通過學(xué)習(xí)OpenCV繪圖函數(shù),能在圖像上繪制直線、矩形、圓形和多邊形等,以及在圖像上添加文字和符號(hào)。
- 介紹鼠標(biāo)交互操作方法,通過鼠標(biāo)、鍵盤與顯示圖像的實(shí)時(shí)交互獲取數(shù)據(jù)。
4.2 繪制直線與線段
函數(shù)cv.line用于在圖像上繪制直線,函數(shù)cv.arrowedLine用于繪制帶箭頭的直線。
函數(shù)原型
cv.line(img, pt1, pt2, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.arrowedLine(img, pt1, pt2, color[, thickness=1, line_type=8, shift=0, tipLength=0.1]) → img
函數(shù)cv.line用于繪制圖像中點(diǎn)pt1與點(diǎn)pt2之間的直線線段,函數(shù)cv.arrowedLine用于繪制圖像中從點(diǎn)pt1指向點(diǎn)pt2的帶箭頭線段。
參數(shù)說明
- img:輸入/輸出圖像,允許為單通道灰度圖像或多通道彩色圖像。
- pt1:線段第一個(gè)點(diǎn)的坐標(biāo),格式為元組(x1, y1)。
- pt2:線段第二個(gè)點(diǎn)的坐標(biāo),格式為元組(x2, y2)。
- tipLength:箭頭部分長(zhǎng)度與線段長(zhǎng)度的比例,默認(rèn)為0.1。
注意問題
(1) 繪圖操作能直接對(duì)輸入圖像進(jìn)行修改,繪制的線段能疊加到輸入圖像上。函數(shù)語(yǔ)法無須接受函數(shù)的返回值。
(2) 繪制起點(diǎn)pt1和終點(diǎn)pt2之間的線段,注意坐標(biāo)格式為(x, y),而不是(y, x)。
(3) 如果起點(diǎn)或終點(diǎn)坐標(biāo)超出了圖像邊界,則要以圖像邊界對(duì)繪制的線段進(jìn)行裁剪。此時(shí),線段的端點(diǎn)為線段與圖像邊界的交點(diǎn),不顯示越界的線段或箭頭。
(4) 箭頭從起點(diǎn)pt1指向終點(diǎn)pt2,通過交換起點(diǎn)與終點(diǎn)并重復(fù)繪制,可以繪制帶雙向箭頭的線段。
(5) 箭頭與直線的夾角是45度,tipLength表示箭頭部分長(zhǎng)度與線段長(zhǎng)度的比例。
【例程0401】繪制直線與線段
本例程用于在圖像上繪制直線與線段,注意對(duì)照程序注釋與顯示結(jié)果比較參數(shù)的影響。
# 【0401】繪制直線與線段
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
if __name__ == '__main__':
height, width, channels = 180, 200, 3
img = np.ones((height, width, channels), np.uint8) * 160 # 創(chuàng)建灰色圖像
# (1) 線條參數(shù) color 的設(shè)置
# 注意 pt1、pt2 坐標(biāo)格式是 (x,y),而不是 (y,x)
img1 = img.copy() # 繪圖函數(shù)就地操作,修改輸入圖像
cv.line(img1, (0,0), (200,180), (0,0,255), 1) # 紅色 R=255
cv.line(img1, (0,0), (100,180), (0,255,0), 1) # 綠色 G=255
cv.line(img1, (0,40), (200,40), (128,0,0), 2) # 深藍(lán) B=128
cv.line(img1, (0,80), (200,80), 128, 2) # color=128 等效于 (128,0,0)
cv.line(img1, (0,120), (200,120), 255, 2) # color=255 等效于 (255,0,0)
# (2) 線寬的設(shè)置
# 如果設(shè)置了 thickness,則關(guān)鍵詞 "lineType" 可以省略
img2 = img.copy()
cv.line(img2, (20,50), (180,10), (255,0,0), 1, cv.LINE_8) # 綠色
cv.line(img2, (20,90), (180,50), (255,0,0), 1, cv.LINE_AA) # 綠色
# 如果沒有設(shè)置 thickness,則關(guān)鍵詞 "lineType" 不能省略
cv.line(img2, (20,130), (180,90), (255,0,0), cv.LINE_8) # 藍(lán)色,函數(shù)cv.line 被識(shí)別為線寬
cv.line(img2, (20,170), (180,130), (255,0,0), cv.LINE_AA) # 藍(lán)色,函數(shù)cv.line 被識(shí)別為線寬
# (3) tipLength 指箭頭部分長(zhǎng)度與整個(gè)線段長(zhǎng)度的比例
img3 = img.copy()
img3 = cv.arrowedLine(img3, (20,20), (180,20), (0,0,255), tipLength=0.05) # 從 pt1 指向 pt2
img3 = cv.arrowedLine(img3, (20,60), (180,60), (0,0,255), tipLength=0.1)
img3 = cv.arrowedLine(img3, (20,100), (180,100), (0,0,255), tipLength=0.15) # 雙向箭頭
img3 = cv.arrowedLine(img3, (180,100), (20,100), (0,0,255), tipLength=0.15) # 交換 pt1、pt2
img3 = cv.arrowedLine(img3, (20,140), (210,140), (0,0,255), tipLength=0.2) # 終點(diǎn)越界,箭頭未顯示
# (4) 沒有復(fù)制原圖像,直接改變輸入圖像的 img,可能導(dǎo)致它們相互影響
img4 = cv.line(img, (0,100), (150,100), (0,255,0), 1) # 水平線,y=100
img5 = cv.line(img, (75,0), (75,200), (0,0,255), 1) # 垂直線,x=75
# (5) 灰度圖像上只能繪制灰度線條,參數(shù) color 只有第一通道值有效
img6 = np.zeros((height, width), np.uint8) # 創(chuàng)建灰度圖像
cv.line(img6, (0,10), (200,10), (0,255,255), 2) # Gray=0
cv.line(img6, (0,30), (200,30), (64,128,255), 2) # Gray=64
cv.line(img6, (0,60), (200,60), (128,64,255), 2) # Gray=128
cv.line(img6, (0,100), (200,100), (255,0,255), 2) # Gray=255
cv.line(img6, (20,0), (20,200), 128, 2) # Gray=128
cv.line(img6, (60,0), (60,200), (255,0,0), 2) # Gray=255
cv.line(img6, (100,0), (100,200), (255,255,255), 2) # Gray=255
print(img6.shape, img6.shape)
plt.figure(figsize=(9, 6))
plt.subplot(231), plt.title("1. img1"), plt.axis('off')
plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB))
plt.subplot(232), plt.title("2. img2"), plt.axis('off')
plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB))
plt.subplot(233), plt.title("3. img3"), plt.axis('off')
plt.imshow(cv.cvtColor(img3, cv.COLOR_BGR2RGB))
plt.subplot(234), plt.title("4. img4"), plt.axis('off')
plt.imshow(cv.cvtColor(img4, cv.COLOR_BGR2RGB))
plt.subplot(235), plt.title("5. img5"), plt.axis('off')
plt.imshow(cv.cvtColor(img5, cv.COLOR_BGR2RGB))
plt.subplot(236), plt.title("6. img6"), plt.axis('off')
plt.imshow(img6, cmap="gray")
plt.tight_layout()
plt.show()
程序說明:
(1) 運(yùn)行結(jié)果,繪制直線與線段如圖4-1所示,注意要對(duì)照程序注釋與顯示的圖像比較不同參數(shù)的影響。
(2) 在沒有設(shè)置 thickness時(shí)關(guān)鍵詞lineType不能省略,否則函數(shù)cv.line 將被識(shí)別為線寬(見圖4-1(2))。
(3) 由于繪圖函數(shù)就地操作,修改了輸入圖像,導(dǎo)致圖4-1(4)和圖4-1(5)所示的繪制結(jié)果相互影響。
圖4-1 繪制直線與線段
版權(quán)聲明:
youcans@xupt 原創(chuàng)作品,轉(zhuǎn)載必須標(biāo)注原文鏈接:(https://blog.csdn.net/youcans/article/details/133693135)
Copyright 2023 youcans, XUPT
Crated:2023-10-16文章來源:http://www.zghlxwxcb.cn/news/detail-786366.html
歡迎關(guān)注本書CSDN獨(dú)家連載專欄
《數(shù)字圖像處理-OpenCV/Python》連載: https://blog.csdn.net/youcans/category_12418787.html文章來源地址http://www.zghlxwxcb.cn/news/detail-786366.html
到了這里,關(guān)于《數(shù)字圖像處理-OpenCV/Python》連載(22)繪制直線與線段的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!