首页 > 解决方案 > 如何删除电路板周围的部分图像 - opencv

问题描述

我正在与 ROI 合作,而我正在寻找这个中心元素。但是我需要删除上面图像的这一部分。

在此处输入图像描述

smooth= cv2.GaussianBlur(gray, (3, 3), cv2.BORDER_DEFAULT)
T, thresh = cv2.threshold(smooth, 32, 255, cv2.THRESH_BINARY)

#roi = cv2.selectROI(thresh)
#print(roi)#=(2, 176, 36, 31)
roi_cropped = thresh[176:(176+31),2:(2+36)]

#kernel = np.ones((3, 3), np.uint8)
#erode = cv2.erode(roi_cropped, kernel) 
#show(erode)

contours, hierarchy = cv2.findContours(image=roi_cropped, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)

image_copy = img.copy()
cv2.drawContours(image=image_copy[176:(176+31),2:(2+36)], contours=contours, contourIdx=-1, color=(0, 255, 0), thickness=1, lineType=cv2.LINE_AA)
show(image_copy)

标签: python-3.xopencvroi

解决方案


这很简单。检查轮廓的边界框是否接触到图片的边缘。

# have `contours` from `roi_cropped`

(height, width) = roi_cropped.shape[:2]

for c in contours:
    (x, y, w, h) = bbox = cv.boundingRect(c)
    touches_edges = (x == 0) or (y == 0) or (x+w == width) or (y+h == height)
    print(touches_edges, ":", bbox)

您可以弄清楚如何擦除轮廓。有多种选择:

您甚至可能不需要擦除这些斑点。只是忽略他们。


推荐阅读