输入手绘逻辑门
我想将门与电路分开,以便我可以在隔离的 SVM 图像上运行并可以检测门的类型,但我的问题是如何检测或分割电路中的门?
最佳答案
这是一种隔离逻辑门的方法。想法是使用 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)
关于image - 图像中手绘逻辑门的分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58491136/