我很确定我犯了一个愚蠢的错误,但我似乎找不到它已经有一段时间了。
我(成功)加载一些图像,将它们转换为灰度,然后打印到黑白。 然后我想制作一个方法,通过测量每个像素的平均颜色值(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)
I am pretty sure I've made a silly mistake, but I cannot seem to find it for quite some time.
I (successfully) load some images, convert them to grayscale and thresh to b&w. Then I wanted to make a method that evaluates each row of pixels by measuring its avg color value (0…255) and give me the row number of the first one that is below a certain threshold. It works, but expanding the method to do the same thing from the bottom does not. Here's how it looks:
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)I know the threshold is correct (works when I manually access a specific line), but bot_trim always returns the height of the picture, meaning it stopped at the first iteration (?). What have I done wrong?
EDIT: Sample case: I am testing it on two pictures: first one is 4724x3177, and the output is: top_trim: 1216 (correct) bot_trim: 4723 (should be ≈ 4400) second one4705 3177 top_trim: 315 (correct) bot_trim: 4704 (should be ≈ 4400)
最满意答案
row = image[height, 0:-1]你总是从最后一行读取,而不是你想要的第i行。 尝试将height改为i 。
row = image[height, 0:-1]You always read from the last row, not the ith row as you probably intended. Try changing height to i.
更多推荐
发布评论