【发布时间】:2019-01-17 09:36:15
【问题描述】:
我正在使用 OpenCV 在图像中查找表格数据,以便可以在其上使用 OCR。到目前为止,我已经能够在图像中找到表格,找到表格的列,然后找到每列中的每个单元格。它工作得很好,但是我遇到了细胞壁卡在我的图像中的问题,我无法可靠地移除它们。This is one example that I'm having difficulty with.This would be another example.
我尝试了几种方法来使这些图像更好。我一直很幸运能找到轮廓。
img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY)
image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask)
ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 3))
# dilate , more the iteration more the dilation
dilated = cv2.dilate(new_img, kernel, iterations=3)
cv2.imwrite('../test_images/find_contours_dilated.png', dilated)
我一直在玩弄内核大小和膨胀迭代,发现这是最好的配置。
我使用的另一种方法是使用 PIL,但只有在整个图像周围的边框是统一的情况下才真正好,在我的情况下并非如此。
copy = Image.fromarray(img)
try:
bg = Image.new(copy.mode, copy.size, copy.getpixel((0, 0)))
except:
return None
diff = ImageChops.difference(copy, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
return np.array(copy.crop(bbox))
我尝试了其他一些想法,但没有一个能让我走得很远。任何帮助将不胜感激。
【问题讨论】:
标签: python opencv image-processing python-imaging-library