Python 3.x 图像中空白区域的检测与消除
我收集了35000张不同纸质表格的灰度扫描。有些纸张是横向的,有些是纵向的。错误地以纵向模式扫描了一些横向形状。这将导致扫描,其下三分之二(大约)为空白。当然,在空白处有一些噪音 我需要做的是从纵向扫描中删除空白,并将其转换为横向扫描。这应该很简单,但我对图像处理显然是新手 因此,任何帮助都将不胜感激。很抱歉,无法发布示例,因为扫描包含个人医疗数据 编辑:Python 3.x 图像中空白区域的检测与消除,python-3.x,orientation,scikit-image,Python 3.x,Orientation,Scikit Image,我收集了35000张不同纸质表格的灰度扫描。有些纸张是横向的,有些是纵向的。错误地以纵向模式扫描了一些横向形状。这将导致扫描,其下三分之二(大约)为空白。当然,在空白处有一些噪音 我需要做的是从纵向扫描中删除空白,并将其转换为横向扫描。这应该很简单,但我对图像处理显然是新手 因此,任何帮助都将不胜感激。很抱歉,无法发布示例,因为扫描包含个人医疗数据 编辑: 来自skimage导入io 从skiliage.util导入作物 将numpy作为np导入 image=io.imread(转换图像路径+文
来自skimage导入io
从skiliage.util导入作物
将numpy作为np导入
image=io.imread(转换图像路径+文件名)
crop_image=crop(image,(image.shape[0]/5,image.shape[0]),(0,image.shape[1]),(0,image.shape[2]))
结果是:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-6cc7c0d534bf> in <module>
4
5 image = io.imread(convert_image_path + filename)
----> 6 crop_image = crop(image, ( image.shape[0]/5, image.shape[0]), ((0, image.shape[1])), (0,image.shape[2]))
/usr/local/lib/python3.7/site-packages/skimage/util/arraycrop.py in crop(ar, crop_width, copy, order)
58 for i, (a, b) in enumerate(crops))
59 if copy:
---> 60 cropped = np.array(ar[slices], order=order, copy=True)
61 else:
62 cropped = ar[slices]
ValueError: Non-string object detected for the array ordering. Please pass in 'C', 'F', 'A', or 'K' instead
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
4.
5 image=io.imread(转换图像路径+文件名)
---->6 crop_image=crop(image,(image.shape[0]/5,image.shape[0]),(0,image.shape[1]),(0,image.shape[2]))
/裁剪中的usr/local/lib/python3.7/site-packages/skimage/util/arraycrop.py(ar、裁剪宽度、副本、订单)
58为i,(a,b)在枚举(作物))
59如果是副本:
--->60裁剪=np.数组(ar[切片],顺序=顺序,副本=真)
61.其他:
62裁剪=ar[切片]
ValueError:为数组排序检测到非字符串对象。请输入“C”、“F”、“A”或“K”
我将尝试回答您的三个主要问题
import numpy as np
from skimage import io
portrait = np.full(shape=(250, 150), fill_value=255, dtype=np.uint8)
portrait[:200, :] = 0
io.imsave("portrait.png", portrait)
如何切片 对于切片,我们可以直接使用numpy,它有一个从左上角开始的坐标系,具有
行
,列
顺序(y
,x
)
为了对图像的底部进行切片,我们必须使用[:200]
选择顶部,这将获取200上方的所有行。(删除最后50个白色行)
如何旋转 对于旋转,在
numpy
中有一个内置方法rot90
import numpy as np
from skimage import io
portrait = io.imread("portrait.png")
rotated = np.rot90(portrait, k=-1) # Rotate clockwise, k=1, rotate counter clockwise
io.imsave("portrait rotated.png", rotated)
如何检测白色区域 要检测较大的白色区域,可以查看行和列中的平均值。基于此,您可以决定是否有足够的白色将其指示为空白区域
import numpy as np
from itertools import groupby, count
from skimage import io
portrait = io.imread("portrait.png")
# We take the average value for every row.
rows = np.average(portrait, axis=1)
# This returns the row indices that have a gray value higher than 240
white_rows = np.squeeze(np.argwhere(rows > 240), axis=-1)
# This locates the groups of consecutive rows that are largely white
groups = groupby(white_rows, key=lambda item, c=count(): item - next(c))
for index, group in groups:
white_region = list(group)
# Only slice regions that contains at least so many largely white rows.
if len(white_region) > 30:
slicing = [white_region[0], white_region[-1]]
print(slicing)
示例输出:
[200, 249]
现在我们知道图像中有一个很大的白色区域。在本例中,您可能需要验证该区域是否仅位于图像的底部。这可以通过过滤低值的白色行来实现
white_rows = white_rows[white_rows > 150]
这意味着白色行必须超过第150个像素行
额外的
要检测图像是否为纵向,可以使用图像形状
portrait = image.shape[0] > image.shape[1]
到目前为止,您尝试了什么,或者您正在努力解决什么问题?我尝试了一些我发现的代码示例,但似乎无法按我希望的方式对图像进行切片-以错误结束,或者什么都不做。必须有一个简单的方法来实现这一点,但迄今为止没有任何效果。更困难的是,我无法在我的Jupiter笔记本中显示图像。但这是另一个问题。我编辑这个问题是为了证明我的一个失败。你的问题对于你想要什么仍然很模糊,你知道风景中有哪些图片吗?是否希望图像从所有图像中删除空白,图像的其他部分有多少白色?对于底部区域有空白的图像,您想做什么?非常感谢,Thymen
portrait = image.shape[0] > image.shape[1]