在Numpy Array中应用函数foreach Pixel

时间:2018-01-23 14:52:20

标签: python numpy

我的功能如下:

def calcChromaFromPixel(red, green, blue):
    r = int(red)
    g = int(green)
    b = int(blue)
    return math.sqrt(math.pow(r - g, 2) + 
                     math.pow(r - b, 2) + 
                     math.pow(g - b, 2))

我有一个RGB图像,它已经被转换为一个像[宽度,高度,3]这样的形状的numpy数组,其中3是颜色通道。

我想要做的是将方法应用于每个像素并从结果中构建均值。我已经完成了显而易见的事情,并使用两个循环遍历数组,但这似乎是一件非常缓慢的事情......是否有更快更漂亮的方法呢?!

谢谢:)

1 个答案:

答案 0 :(得分:1)

代码:

import math
import numpy as np
np.random.seed(1)

# FAKE-DATA
img = np.random.randint(0,255,size=(4,4,3))
print(img)

# LOOP APPROACH
def calcChromaFromPixel(red, green, blue):
    r = int(red)
    g = int(green)
    b = int(blue)
    return math.sqrt(math.pow(r - g, 2) +
                     math.pow(r - b, 2) +
                     math.pow(g - b, 2))

bla = np.zeros(img.shape[:2])
for a in range(img.shape[0]):
    for b in range(img.shape[1]):
        bla[a,b] = calcChromaFromPixel(*img[a,b])
print('loop')
print(bla)

# VECTORIZED APPROACH
print('vectorized')
res = np.linalg.norm(np.stack(
        (img[:,:,0] - img[:,:,1],
         img[:,:,0] - img[:,:,2],
         img[:,:,1] - img[:,:,2])), axis=0)
print(res)

输出:

[[[ 37 235 140]
  [ 72 137 203]
  [133  79 192]
  [144 129 204]]

 [[ 71 237 252]
  [134  25 178]
  [ 20 254 101]
  [146 212 139]]

 [[252 234 156]
  [157 142  50]
  [ 68 215 215]
  [233 241 247]]

 [[222  96  86]
  [141 233 137]
  [  7  63  61]
  [ 22  57   1]]]
loop
[[ 242.56545508  160.44313634  138.44132331   97.21111048]
 [ 246.05283985  192.94040531  291.07730932   98.66103588]
 [ 124.99599994  141.90842117  207.88939367   17.20465053]
 [ 185.66636744  133.02631319   77.82030583   69.29646456]]
vectorized
[[ 242.56545508  160.44313634  138.44132331   97.21111048]
 [ 246.05283985  192.94040531  291.07730932   98.66103588]
 [ 124.99599994  141.90842117  207.88939367   17.20465053]
 [ 185.66636744  133.02631319   77.82030583   69.29646456]]