简体   繁体   English

使用 OpenCV 从图像中删除不均匀的白色边框

[英]Remove uneven white border from images using OpenCV

I'm trying to remove uneven white borders from different set of pictures.我正在尝试从不同的图片集中删除不均匀的白色边框。 They all look like these:它们看起来都像这样:

在此处输入图像描述 在此处输入图像描述

What I'm doing right now is just drawing a rectangle around the picture in hope that it covers the white area:我现在正在做的只是在图片周围画一个矩形,希望它覆盖白色区域:

h, w = img.shape
cv2.rectangle(img, (0,0), (w,h), (0,0,0), 2)

Depending on the picture it might work or not.根据图片,它可能有效或无效。 As there are a variety number of pictures which are in similar situation I'm looking for a more logical solution which is applicable to all pictures with this kind of issue.由于有多种图片处于类似情况,我正在寻找一种更合乎逻辑的解决方案,适用于所有具有此类问题的图片。

I think your way is right, but it's unaware whether it overlays figures (you may increase the thickness if you know there won't be figures with that margin) and the desired thickness is unknown.我认为您的方式是正确的,但它不知道它是否覆盖数字(如果您知道不会有具有该边距的数字,您可以增加厚度)并且所需的厚度是未知的。

You may use findContour.您可以使用 findContour。 Find the "thick" figures (if you expect particular metrics as in the picture).找到“厚”的数字(如果您期望图片中的特定指标)。 Sort their extreme coordinates, add some margin and that would set the max depth of the border.对它们的极端坐标进行排序,添加一些边距,这将设置边框的最大深度。

However then not a rectangle, but a line would be better drawn per each side, in case there are figures very close to the border.但是,不是矩形,而是每边最好画一条线,以防有图形非常靠近边界。

Another scenario: first draw concentrating black rectangles (or lines per side) in order to clear the unevenness, then draw the white lines/rectangle with the desired thickness.另一种情况:首先绘制集中的黑色矩形(或每边的线条)以清除不均匀性,然后绘制具有所需厚度的白线/矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM