我很确定我犯了一个愚蠢的错误,但我似乎无法找到它的相当一段时间。为什么不按照它应该的方式在这里工作?
我(成功)加载一些图像,将它们转换为灰度和阈值为& w。然后,我想制作一种方法,通过测量其平均颜色值(0 ... 255)来评估每行像素,并给出第一个低于某个阈值的行号。它有效,但扩展方法从底部做同样的事情不会。下面是它的外观:
t_thresh_hor = 210.0
def core_text_loc(image):
height, width = image.shape
height = height - 1 # to adjust for starting with 0
width = width - 1 # to adjust for starting with 0
top_trim = 0
bot_trim = height
i = 0
while i < height and top_trim == 0:
row = image[i, 0:-1]
i = i + 1
if numpy.mean(row) < t_thresh_hor:
top_trim = i
# here it stops working
i = height
while i > 0 and bot_trim == height:
row = image[height, 0:-1]
if numpy.mean(row) < t_thresh_hor:
bot_trim = i
i = i - 1
return(top_trim, bot_trim)
我知道了阈值是正确的(当我手动访问特定线工程),但bot_trim总是返回图片的高度,这意味着它停在第一次迭代(?)。 我做错了什么?
编辑: 样本案例:我上两张图片测试它: 第一个是4724x3177,输出是: top_trim:1216(正确) bot_trim:4723(应该是≈4400) 第二one4705 3177 top_trim:315(正确) bot_trim:4704(应该是≈4400)
预期产出是多少?请提供一个示例。 –