【问题标题】:Calculating Dual-Energy Gradient using Numpy使用 Numpy 计算双能梯度
【发布时间】:2018-02-25 23:47:22
【问题描述】:

我有像素(x, y)的“双能量梯度”(Reference)的定义是Δ^2(x, y) + Δy^2(x, y),其中x梯度的平方Δx^2(x, y) = Rx(x, y)^2 + Gx(x, y)^2 + Bx(x, y)^2和中心差异Rx(x,y ), Gx(x, y) 和 Bx(x, y) 是像素 (x + 1, y) 和像素 (x − 1, y) 和类似像素之间红、绿、蓝分量差的绝对值对于 y 梯度。

如何使用 numpy 有效地计算?

我不确定numpy.gradient 是否可以帮助我开箱即用。

【问题讨论】:

  • 什么是 Rx(x,y) Gx(x,y) Bx(x,y) ?我猜它与图像的 RGB 分量有关,但“x”是什么意思?
  • @ma3oun - 更新了我的定义。

标签: python numpy image-processing


【解决方案1】:

我不知道,是否有内置函数供你计算(也许你想探索scipy.ndimage)。
如果没有,这里是只使用numpy 函数的版本:

import numpy as np

#convert from uint8 to int64 to prevent overflow problems
arr = np.array(loaded_pic, dtype = int)
#calculate squared difference ((x-1, y) - (x+1, y))^2 for each R, G and B pixel
deltaX2 = np.square(np.roll(arr, -1, axis = 0) - np.roll(arr, 1, axis = 0))
#same for y axis
deltaY2 = np.square(np.roll(arr, -1, axis = 1) - np.roll(arr, 1, axis = 1))
#add R, G and B values for each pixel, then add x- and y-shifted values
de_gradient = np.sum(deltaX2, axis = 2) + np.sum(deltaY2, axis = 2)

来自linked reference的示例输入

loaded_pic = np.asarray([[(255, 101, 51), (255, 101, 153), (255, 101, 255)],
                         [(255, 153, 51), (255, 153, 153), (255, 153, 255)],
                         [(255, 203, 51), (255, 204, 153), (255, 205, 255)],
                         [(255, 255, 51), (255, 255, 153), (255, 255, 255)]],
                         dtype = "uint8")

示例输出:

[[20808 52020 20808]
 [20808 52225 21220]
 [20809 52024 20809]
 [20808 52225 21220]]

【讨论】:

    【解决方案2】:

    假设我正确理解了您的公式,以下是我的建议:

    from scipy.misc import face
    import matplotlib.pyplot as plt
    import numpy as np
    
    img = face() # this is just a sample racoon RGB image for testing
    
    #uncomment to show the racoon image
    #plt.imshow(img)
    #plt.show(img) # this displays the original image
    

    计算 x 梯度:

    gradX = np.gradient(img,axis=0)
    

    计算 x 梯度沿 RGB 分量的平方和:

    squareX = np.sum(np.square(gradX),axis=2)
    

    y 梯度也一样:

    gradY = np.gradient(img,axis=1)
    squareY = np.sum(np.square(gradY),axis=2)
    

    “双能梯度”:

    dualEnergy = squareX + squareY
    
    #uncomment to display the image
    #plt.imshow(dualEnergy)
    #plt.show()
    

    原图

    双能

    【讨论】:

    • 谢谢,@ma3oun 我怀疑我们是否认识到从理论到实现的映射是正确的。我也用我的图像进行了尝试,它们与参考论文中显示的不同 - cs.princeton.edu/courses/archive/spring13/cos226/assignments/…
    • np.gradient 不简单地计算相邻数组元素的差异。如果是这样,您在gradX 中将只有整数。但它也包含浮点值。
    猜你喜欢
    • 2013-04-11
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多