Mark Setchell 的想法是开箱即用的。这是一种更传统的方法。
方法:
该图像包含其强度在较低行中逐渐消失的框。由于考虑了整个图像的强度变化,因此在这里使用全局均衡会失败。我在 OpenCV 中选择了一种局部均衡方法,可通过 CLAHE (Contrast Limited Adaptive Histogram Equalization)) 获得
使用 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)
(最右上角的框未正确覆盖。您需要调整各种参数以获得准确的结果)
您可以尝试的其他预处理方法:
- 全局均衡
- 对比拉伸
- 规范化