python-3.x - 如何删除电路板周围的部分图像 - 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)
解决方案
这很简单。检查轮廓的边界框是否接触到图片的边缘。
# 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)
您可以弄清楚如何擦除轮廓。有多种选择:
您甚至可能不需要擦除这些斑点。只是忽略他们。
推荐阅读
- java - 使用 jdbc 模板批量更新插入或更新记录后如何获取自动生成的主键?
- sql-server - 将 SQL 数据导出到具有多种记录类型的固定宽度文本文件
- angular - SweetAlert 删除 angular5 中的错误
- typescript - 打字稿:接口字段上的元
- neo4j - 如何在 NEO4J 密码中进行“或”搜索
- c# - 静态 HttpClient 和 CookieContainer
- c - 如何在c中将指针分配给数组[]?
- c# - cefsharp 子进程退出或崩溃时的事件或通知
- c# - 通过MvvmCross的IoC给类的构造函数传参
- ssl - TLS 超时仅适用于 HTTP/2 时缺少 shutdown() 回溯