image - 图像中手绘逻辑门的分割

标签 image opencv image-processing computer-vision object-detection

输入手绘逻辑门

我想将门与电路分开,以便我可以在隔离的 SVM 图像上运行并可以检测门的类型,但我的问题是如何检测或分割电路中的门?

最佳答案

这是一种隔离逻辑门的方法。想法是使用 cv2.HoughLinesP() 进行线检测。一旦我们有了检测到的线条,我们就可以有效地移除掩模上的线条以隔离门。从这里开始,我们执行形态学操作来清理图像并从每个门获得单个轮廓。最后,我们进行轮廓过滤和ROI提取


要删除的检测线

结果

提取的 ROI

enter image description here

根据输入图像,您可能必须更改 cv2.HoughLinesP() 中的参数和最小阈值区域。使用 maxLineGap=50area > 1000,这是另一个输入图像的结果

enter image description here

我在 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/

相关文章:

c++ - cvQueryFrame 是否预先有帧缓冲区?

c++ - 如何在 OpenCV 中锐化图像

javascript - 如何将 HTML 文件转换为 GIF

python - 从客户端向服务器发送大量图像的有效方法

python - 如何在 django 模板中显示保存的动态创建的图像?

python - 直方图均衡:未获得所需的输出图像

python - 如何从光学形式中选择?

python - 平均后图像全灰

image-processing - 用于图像过滤的 3d CUDA 内核索引?

jQuery 和图像 slider /缩放效果