Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python计算3d RGB阵列的平均值和标准偏差?_Python_Arrays_Numpy_Opencv - Fatal编程技术网

如何使用python计算3d RGB阵列的平均值和标准偏差?

如何使用python计算3d RGB阵列的平均值和标准偏差?,python,arrays,numpy,opencv,Python,Arrays,Numpy,Opencv,我现在需要得到10张图片(400px,400px)的RGB值的平均值和标准偏差。我的意思是红色的(x,y),红色的标准(x,y),等等 使用cv2.imread,我得到了10(400400,3)个形状数组。因此,我首先尝试使用numpy.dstack来堆叠每个RGB值,以获得(400400,3,10)形状数组。但是,由于数组的形状会随着迭代而改变,所以它不起作用 因此,我最终在下面编写了代码 def average_and_std_of_RGB(pic_database,start,num_pa

我现在需要得到10张图片(400px,400px)的RGB值的平均值和标准偏差。我的意思是红色的(x,y),红色的标准(x,y),等等

使用cv2.imread,我得到了10(400400,3)个形状数组。因此,我首先尝试使用numpy.dstack来堆叠每个RGB值,以获得(400400,3,10)形状数组。但是,由于数组的形状会随着迭代而改变,所以它不起作用

因此,我最终在下面编写了代码

def average_and_std_of_RGB(pic_database,start,num_past_frame):
    background = pic_database[0] #initialize background
    past_frame = pic_database[1:num_past_frame+1]
    width,height,depth = background.shape
    sumB = np.zeros(width*height)
    sumG = np.zeros(width*height)
    sumR = np.zeros(width*height)
    sumB_sq = np.zeros(width*height)
    sumG_sq = np.zeros(width*height)
    sumR_sq = np.zeros(width*height)
    for item in (past_frame):
        re_item = np.reshape(item,3*width*height) #reshape (400,400,3) to (480000,)
        itemB =[re_item[i] for i in range(3*width*height) if i%3==0] #Those divisible by 3 is Blue
        itemG =[re_item[i] for i in range(3*width*height) if i%1==0] #Those divisible by 1 is Green
        itemR =[re_item[i] for i in range(3*width*height) if i%2==0] #Those divisible by 2 is Red
        itemB_sq = [item**2 for item in itemB]
        itemG_sq = [item**2 for item in itemG]
        itemR_sq = [item**2 for item in itemR]
        sumB = [x+y for (x,y) in zip(sumB,itemB)]
        sumG = [x+y for (x,y) in zip(sumG,itemG)]
        sumR = [x+y for (x,y) in zip(sumR,itemR)]
        sumB_sq = [x+y for (x,y) in zip(sumB_sq,itemB_sq)]
        sumG_sq = [x+y for (x,y) in zip(sumG_sq,itemG_sq)]
        sumR_sq = [x+y for (x,y) in zip(sumR_sq,itemR_sq)]
    aveB = [x/num_past_frame for x in sumB]
    aveG = [x/num_past_frame for x in sumG]
    aveR = [x/num_past_frame for x in sumR]
    aveB_sq = [x/num_past_frame for x in sumB]
    aveG_sq = [x/num_past_frame for x in sumR]
    aveR_sq = [x/num_past_frame for x in sumR]
    stdB = [np.sqrt(abs(x-y**2)) for (x,y) in zip(aveB_sq,aveB)]
    stdG = [np.sqrt(abs(x-y**2)) for (x,y) in zip(aveG_sq,aveG)]
    stdR = [np.sqrt(abs(x-y**2)) for (x,y) in zip(aveR_sq,aveR)]
    return sumB,sumG,sumR,stdB,stdG,stdR
它实际上是有效的,但看起来很残忍,需要一些时间。 我想知道是否有更有效的方法得到同样的结果。 请帮我一把,谢谢

>>> img = cv2.imread("/home/auss/Pictures/test.png")
>>> means, stddevs  = cv2.meanStdDev(img)
>>> means
array([[ 95.84747396],
       [ 91.55859375],
       [ 96.96260851]])
>>> stddevs
array([[ 48.26534676],
       [ 48.24555701],
       [ 55.92261374]])