【发布时间】:2018-06-26 06:26:57
【问题描述】:
你好呀, 我正在尝试通过将 OpenCV 与 Python 结合使用来构建模拟量规阅读器。我使用霍夫圆来减少编码。代码转载如下:
import cv2
import numpy as np
img = cv2.imread('gauge.jpg', 0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
height,width = img.shape
mask = np.zeros((height,width), np.uint8)
counter = 0
circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,
param1=200,param2=100,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
# Draw on mask
cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),-1)
masked_data = cv2.bitwise_and(cimg, cimg, mask=mask)
# Apply Threshold
_,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)
# Find Contour
cnt = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
#print len(contours)
x,y,w,h = cv2.boundingRect(cnt[0])
# Crop masked_data
crop = masked_data[y:y+h,x:x+w]
# Write Files
cv2.imwrite("output/crop"+str(counter)+".jpg", crop)
counter +=1
print counter
cv2.imshow('detected circles',cimg)
cv2.imwrite("output/circled_img.jpg", cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
我的问题如下:
我没有得到单独的 Dials,但在每个图像中,“crop0.jpg”有 1 个,但“crop1.jpg”有 2 个,“crop3.jpg”有 4 个。我需要的是单个文件中的单个圈子,其中然后我可以运行批量模板匹配算法。
计数器的总结果为 5,请注意。
【问题讨论】:
-
您可能希望先平滑图像,然后再使用 HoughCircles 找到圆圈。此外,由于圆的大小或多或少相同,您还可以尝试为 minRadius 和 maxRadius 设置不同的值
标签: python opencv computer-vision opencv-contour