【问题标题】:Count the pixel change in each row计算每一行的像素变化
【发布时间】:2020-12-22 00:02:28
【问题描述】:

我想计算图像每一行的像素变化(这意味着从黑色到白色或从白色到黑色的任何变化), 如下图: Example of letter A

我写了这段代码,但是第二部分是错误的:

change = [0 for k in range(test.shape[0])]
total = []
for x in range(test.shape[0]-1):
    change[x]=0
    for y in range(test.shape[1]-1):
        if test[x,y+1] != test[x,y]:
            change[x] = change[x] + 1
        elif test[x+1,y+1] != test[x+1, y]:
                change[x] = change[x] + 1

    total.append(change[x])

【问题讨论】:

  • 您想获取每行的更改计数吗?
  • @Mike67 是的

标签: python image image-processing pixel image-segmentation


【解决方案1】:

要计算一行的变化,只需将每个像素与同一行的下一个像素进行比较。

在您的代码中,只需删除 elif

change = [0 for k in range(test.shape[0])]  # changes per row
total = []
for x in range(test.shape[0]):  # each row
    change[x]=0   # not needed
    for y in range(test.shape[1]-1):  # each column
        if test[x,y+1] != test[x,y]:
            change[x] = change[x] + 1
    #    elif test[x+1,y+1] != test[x+1, y]:   # not needed
    #            change[x] = change[x] + 1     # not needed

    total.append(change[x])

【讨论】:

  • 但是如果你检查照片,我的代码不能得到第 3 个路口
【解决方案2】:

您可以尝试使用 Numpy:

out=np.sum(np.absolute(np.diff(np.double(img), axis=1))/255.0, axis=1)

【讨论】:

  • 是的,我喜欢你所做的,但它无法检测到数字 3(如图所示)
猜你喜欢
  • 2020-04-28
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多