opencv python 對指針儀表讀數(shù)識別的兩種方式
我嘗試了兩種方式
用opencv 對指針儀表進(jìn)行讀數(shù)識別,
1. 先模板匹配,然后邊緣檢測 + 霍夫直線

2. 按輪廓大小過濾,然后邊緣檢測 + 霍夫直線

兩種方式對光線都非常敏感
其中第一種的應(yīng)用范圍更廣,背景復(fù)雜一點(diǎn)也能識別到
個人比較喜歡這種方式
第二種的限制多一點(diǎn),對背景、光線條件要求比較高
對于固定位置,且明暗變化不大的情況下,這種方式還是很有效的
先說第一個方案,第二個方式就不說了
第一種方式:模板匹配,然后邊緣檢測 + 霍夫直線
if __name__ == "__main__":
# 加載模板
template = cv2.imread('./data/001.jpg',1)
# 初始化
am = C_ammerter(template)
# 運(yùn)行
am.am_run()
# 結(jié)束
am.close()
模板圖 001.jpg

下面給出def am_run(self)函數(shù)的處理流程 (整體比較亂~~~)
其中邊緣檢測之前需要對圖像做一些處理:
def am_run(self):
while True:
ret, frame = self.cap.read()
if frame is None:
print('video picture is none --continue ')
continue
gray = frame.copy()
# cv2.imshow('origin', gray)
# 匹配模板 框出匹配區(qū)域
image = gray.copy()
maxval,t_left, b_right = self.get_match(gray)
if maxval < 16000000000: # 對匹配程度做判斷
print("---------------------------------------")
print('matchTemplate is not enough --continue')
print("---------------------------------------")
result =frame
image=frame
else:
cv2.rectangle(image, t_left, b_right, 255, 2)
# 高斯除噪
kernel = np.ones((6,6), np.float32) / 36
gray_cut_filter2D = cv2.filter2D(image[t_left[1]:t_left[1] + self.h, t_left[0]:t_left[0] + self.w], -1, kernel)
# 灰度圖 二值化
gray_img = cv2.cvtColor(gray_cut_filter2D, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray_img, 180, 255, cv2.THRESH_BINARY)
# 二值化后 分割主要區(qū)域 減小干擾 模板圖尺寸371*369
tm = thresh1.copy()
test_main = tm[50:319, 50:321]
# 邊緣化檢測
edges = cv2.Canny(test_main, 50, 150, apertureSize=3)
# 霍夫直線
lines = cv2.HoughLines(edges, 1, np.pi / 180, 60)
if lines is None:
continue
result = edges.copy()
for line in lines[0]:
rho = line[0] # 第一個元素是距離rho
theta = line[1] # 第二個元素是角度theta
print('distance:' + str(rho), 'theta:' + str(((theta / np.pi) * 180)))
lbael_text = 'distance:' + str(round(rho))+ 'theta:' + str(round((theta / np.pi) * 180-90,2))
cv2.putText(image, lbael_text,(t_left[0],t_left[1]-12),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
if (theta > 3 * (np.pi / 3)) or (theta < (np.pi / 2)): # 從圖像邊界畫出延長直線
# 該直線與第一行的交點(diǎn)
pt1 = (int(rho / np.cos(theta)), 0)
# 該直線與最后一行的焦點(diǎn)
pt2 = (int((rho - result.shape[0] * np.sin(theta)) / np.cos(theta)), result.shape[0])
# 繪制一條白線
cv2.line(result, pt1, pt2,255, 1)
# print('theat >180 theta<90')
else: # 水平直線
# 該直線與第一列的交點(diǎn)
pt1 = (0, int(rho / np.sin(theta)))
# 該直線與最后一列的交點(diǎn)
pt2 = (result.shape[1], int((rho - result.shape[1] * np.cos(theta)) / np.sin(theta)))
# 繪制一條直線
cv2.line(result, pt1, pt2, 255, 1)
cv2.imshow('result', result)
cv2.imshow('rectangle', image)
if cv2.waitKey(1) & 0XFF == ord('q'):
break
到此這篇關(guān)于opencv python 對指針儀表讀數(shù)識別的兩種方式的文章就介紹到這了,更多相關(guān)opencv python指針儀表讀數(shù)識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python關(guān)于os.walk函數(shù)查找windows文件方式
這篇文章主要介紹了python關(guān)于os.walk函數(shù)查找windows文件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
python3.x編碼解碼unicode字符串的實(shí)現(xiàn)示例
ASCII文本編碼是一種Unicode,存儲為表示字符的字節(jié)值的一個序列,本文主要介紹了python3.x編碼解碼unicode字符串的實(shí)現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-01-01
Python轉(zhuǎn)換HTML到Text純文本的方法
這篇文章主要介紹了Python轉(zhuǎn)換HTML到Text純文本的方法,分析了常用的兩種方法,非常具有實(shí)用價值,需要的朋友可以參考下2015-01-01
Python使用面向?qū)ο蠓绞絼?chuàng)建線程實(shí)現(xiàn)12306售票系統(tǒng)
目前python 提供了幾種多線程實(shí)現(xiàn)方式 thread,threading,multithreading ,其中thread模塊比較底層,而threading模塊是對thread做了一些包裝,可以更加方便的被使用2015-12-12

