Python-OpenCV学习--外接摄像头实时检测文本框
生活随笔
收集整理的這篇文章主要介紹了
Python-OpenCV学习--外接摄像头实时检测文本框
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
一、Windows7 系統下 Python 3.x版本? , 臺式機外接攝像頭 使用分水嶺算法 腐蝕 膨脹等,識別文本的區域。
import numpy as np import cv2 from matplotlib import pyplot as plt cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FPS, 15) while True:ret, frame = cap.read()# 轉化成灰度圖gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# 利用Sobel邊緣檢測生成二值圖sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3) # 二值化ret, binary = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)# 膨脹、腐蝕element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 9))element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (24, 6))# 膨脹一次,讓輪廓突出dilation = cv2.dilate(binary, element2, iterations=1)# 腐蝕一次,去掉細節erosion = cv2.erode(dilation, element1, iterations=1)# 再次膨脹,讓輪廓明顯一些dilation2 = cv2.dilate(erosion, element2, iterations=2)# 查找輪廓和篩選文字區域region = []contours, hierarchy = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)for i in range(len(contours)):cnt = contours[i]# 計算輪廓面積,并篩選掉面積小的area = cv2.contourArea(cnt)if (area < 1000):continue# 找到最小的矩形rect = cv2.minAreaRect(cnt)print("rect is: ")print(rect)# box是四個點的坐標box = cv2.boxPoints(rect)box = np.int0(box)# 計算高和寬height = abs(box[0][1] - box[2][1])width = abs(box[0][0] - box[2][0])# 根據文字特征,篩選那些太細的矩形,留下扁的if (height > width * 1.3):continueregion.append(box)# 繪制輪廓for box in region:cv2.drawContours(frame, [box], 0, (0, 255, 0), 2)cv2.imshow('img', frame)if cv2.waitKey(10) == ord("q"):break #隨時準備按q退出 cap.release() cv2.destroyAllWindows()?
總結
以上是生活随笔為你收集整理的Python-OpenCV学习--外接摄像头实时检测文本框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python-OpenCV学习--电脑外
- 下一篇: Python-OpenCV-- 台式机外