如何使用Python和OpenCV读取此模拟量表?
我想使用python和openCV读取此模拟量表,这是我的起点图像:
实际上,有了一些模糊和轮廓检测,我能够将其清除一点并绘制一些重要部分:
这是我的实际代码:
import os
import glob
import cv2 # to run code even if version changes
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
img = cv2.imread('/home/administrator/Downloads/read_analogue_gauge_v1_1/images/gaugemio.jpg')
def rescaleFrame(frame, scale=0.5):
width = int(frame.shape[1]*scale)
height = int(frame.shape[0]*scale)
dimensions = (width, height)
return cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)
resized_image = rescaleFrame(img)
img = resized_image
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
img_hsv = cv2.bitwise_not(img_hsv) # Convert to HSV space
lower_black, upper_black = np.array([0, 0, 0]), np.array([180, 255, 65])
mask = cv2.inRange(img_hsv, lower_black, upper_black)
blank = np.zeros((747, 714), dtype='uint8')
blank2 = np.zeros((747, 714), dtype='uint8')
blurred = cv2.blur(~mask, (9, 9))
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT,1, 400, param1=50, param2=100, minRadius=200, maxRadius=0)
detected_circles = np.uint16(np.around(circles))
dimension = img.shape
for(x, y, r) in detected_circles[0, :]:
circle = cv2.circle(blank, (x,y),r, 255, -1 )
cv2.circle(img, (x,y),r, (0,255,0),3)
cv2.circle(img, (x,y),2, (0,0,255),3)
cv2.circle(img, (x,y),30, (0,0,255),3)
circ_col = x
circ_row = y
bitwise_and = cv2.bitwise_and(blank, mask)
canny2 = cv2.Canny(bitwise_and, 50, 150, apertureSize=3)
blurred2 = cv2.blur(bitwise_and, (15, 15))
contours, hierarchy = cv2.findContours(blurred2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow('hi',resized_image)
cv2.waitKey(0)
有技巧吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论