【问题标题】:Why doesn't decrementation work here the way it should?为什么递减在这里没有按应有的方式工作?
【发布时间】:2017-07-02 19:19:08
【问题描述】:

我很确定我犯了一个愚蠢的错误,但我似乎在很长一段时间内都找不到它。

我(成功)加载了一些图像,将它们转换为灰度,然后将它们转换为黑白。然后我想创建一种方法,通过测量其平均颜色值 (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) 第二个4705 3177 top_trim:315(正确) bot_trim: 4704 (应该是 ≈ 4400)

【问题讨论】:

  • 预期输出是什么?请提供一个样例。

标签: python python-3.x loops iteration


【解决方案1】:
row = image[height, 0:-1]

您总是从最后一行读取,而不是您可能想要的ith 行。尝试将height 更改为i

【讨论】:

    【解决方案2】:

    在第二个 while 循环中,你使用了

    row = image[height, 0:-1]
    

    应该是这样的

    row = image[i, 0:-1]
    

    【讨论】:

      【解决方案3】:

      改变

      row = image[height, 0:-1]
      

      row = image[i, 0:-1]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 2014-12-24
        • 1970-01-01
        相关资源
        最近更新 更多