【问题标题】:How to calculate the average of the same pixel on 10 pictures?如何计算10张图片上相同像素的平均值?
【发布时间】:2018-01-19 14:44:16
【问题描述】:

我想为这 10 张图片中的每张图片获取相同像素 (150,220) 的平均值。

怎么做?

每张图片大小相同。

例如:

img1[220,150]=[1,2,3]

img2[220,150]=[2,3,4]

……

img9[220,150]=[9,10,11]

img10[220,150]=[10,11,12]

avg_value = [(1+2+……+9+10)/10,(2+3+……+10+11)/10,(3+4+……+11+12)/10 ]

我的代码:

import cv2

img1 = cv2.imread( r'E:/0001.jpg' )
img2 = cv2.imread( r'E:/0002.jpg' )
img3 = cv2.imread( r'E:/0003.jpg' )
img4 = cv2.imread( r'E:/0004.jpg' )
img5 = cv2.imread( r'E:/0005.jpg' )
img6 = cv2.imread( r'E:/0006.jpg' )
img7 = cv2.imread( r'E:/0007.jpg' )
img8 = cv2.imread( r'E:/0008.jpg' )
img9 = cv2.imread( r'E:/0009.jpg' )
img10 = cv2.imread( r'E:/0010.jpg' )

img_array = [img1,img2,img3,img4,img5,img6,img7,img8,img9,img10]

【问题讨论】:

  • 我不明白你的问题到底是什么。你知道如何读取这些图像中任何像素的值吗?因为如果是这样,您为什么不能以相同的方式读取您需要的所有像素值并从中计算平均值?
  • (img1+img2+...+img10)/10 如果数据类型是双精度。

标签: python python-3.x numpy opencv


【解决方案1】:

以下是我们如何使用numpy 计算一组图像中特定像素的平均像素值。

import numpy as np
import cv2

names = [r'E:/0001.jpg', 
         r'E:/0002.jpg',
         r'E:/0003.jpg',
         r'E:/0004.jpg',
         r'E:/0005.jpg',
         r'E:/0006.jpg',
         r'E:/0007.jpg',
         r'E:/0008.jpg',
         r'E:/0009.jpg',
         r'E:/0010.jpg'];


#Read all the images
images = [cv2.imread(i) for i in names];

#Get that specific pixel of all the images (making a 2D array). Exclude non-existing images
vals = np.array([im[220,150,:] for im in images if im is not None]);

#Compute mean along the first axis of 2D array
m = np.mean(vals, axis=0);

print(m)

在 Ubuntu 14.04 上使用 Python 3.4OpenCV 3.4 进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2016-08-03
    相关资源
    最近更新 更多