【发布时间】:2020-02-17 19:53:21
【问题描述】:
输入手绘逻辑门
我想将门与电路分开,以便在隔离的 Svm 图像上运行并检测门的类型,但我的问题是如何检测或分割电路中的门?
【问题讨论】:
标签: image opencv image-processing computer-vision object-detection
输入手绘逻辑门
我想将门与电路分开,以便在隔离的 Svm 图像上运行并检测门的类型,但我的问题是如何检测或分割电路中的门?
【问题讨论】:
标签: image opencv image-processing computer-vision object-detection
这是一种隔离逻辑门的方法。这个想法是使用cv2.HoughLinesP() 进行线路检测。一旦我们检测到线,我们就可以有效地去除掩模上的线以隔离门。从这里开始,我们执行形态学操作来清理图像并从每个门中获取单个轮廓。最后,我们进行轮廓过滤和 ROI 提取
检测到要删除的行
结果
提取的 ROI
根据输入图像,您可能需要更改cv2.HoughLinesP() 中的参数和最小阈值区域。使用maxLineGap=50 和area > 1000,这是另一个输入图像的结果
我在 Python OpenCV 中实现了这种方法
import cv2
import numpy as np
# Grayscale + Otsu's threshold
image = cv2.imread('1.jpg')
original = image.copy()
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find lines
minLineLength = 10
maxLineGap = 150
lines = cv2.HoughLinesP(thresh,1,np.pi/180,100,minLineLength,maxLineGap)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(thresh,(x1,y1),(x2,y2),(0,0,0),5)
# Morphological operations to clean image
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=3)
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Contour filtering and ROI extraction
ROI_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > 3000:
x,y,w,h = cv2.boundingRect(c)
ROI = original[y:y+h,x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 8)
ROI_number += 1
cv2.imwrite('thresh.png', thresh)
cv2.imwrite('close.png', close)
cv2.imwrite('image.png', image)
【讨论】:
maxLineGap=50 和area > 1000 得到了很好的结果