为什么不按照应有的方式在这里进行减量?(Why doesn't decrementation work here the way it should?)

编程入门 行业动态 更新时间:2024-10-28 10:35:23
为什么不按照应有的方式在这里进行减量?(Why doesn't decrementation work here the way it should?)

我很确定我犯了一个愚蠢的错误,但我似乎找不到它已经有一段时间了。

我(成功)加载一些图像,将它们转换为灰度,然后打印到黑白。 然后我想制作一个方法,通过测量每个像素的平均颜色值(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.

更多推荐

本文发布于:2023-04-29 12:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这里   不按   减量   方式   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!