[英]How to find rectangles in a full transparent object?
Mark Setchell 的想法是开箱即用的。 这是一种更传统的方法。
方法:
图像包含强度在较低行中逐渐消失的框。 由于考虑了整个图像的强度变化,因此在这里使用全局均衡将失败。 我在 OpenCV 中选择了一种局部均衡方法,可用作CLAHE(对比度有限自适应直方图均衡))
使用 CLAHE:
让我们看看如何在我们的问题中使用它:
代码:
# read image and store green channel
green_channel = img[:,:,1]
# grid-size for CLAHE
ts = 8
# initialize CLAHE function with parameters
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(ts, ts))
# apply the function
cl = clahe.apply(green_channel)
请注意上图,下部区域的框看起来像预期的那样稍暗。 这将对我们以后有所帮助。
# apply Otsu threshold
r,th_cl = cv2.threshold(cl, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# dilation performed using vertical kernels to connect disjoined boxes
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 3))
dilate = cv2.dilate(th_cl, vertical_kernel, iterations=1)
# find contours and draw bounding boxes
contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
img2 = img.copy()
for c in contours:
area = cv2.contourArea(c)
if area > 100:
x, y, w, h = cv2.boundingRect(c)
img2 = cv2.rectangle(img2, (x, y), (x + w, y + h), (0,255,255), 1)
(最右上角的框没有正确覆盖。您需要调整各种参数以获得准确的结果)
您可以尝试的其他预处理方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.