亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

opencv python 對指針儀表讀數(shù)識別的兩種方式

 更新時間:2021年01月14日 10:14:38   作者:老毛鴣  
這篇文章主要介紹了opencv python 對指針儀表讀數(shù)識別的兩種方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

我嘗試了兩種方式

用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)文章

最新評論