使用scikit-image计算值<0.5的像素的平均值

问题描述 投票:0回答:1

在Python 3中使用scikit-image包并给出RGB图像,我们如何计算值> 0.5的像素的平均像素值(范围从0到1的标准化值)?

我想我设法掩盖了我想要计算平均值的像素,那么我们应该如何进行呢?

from skimage.io import imread, imshow
from skimage.transform import resize

imgData = data.imread(dataFilepath, as_grey=True)
imgData = resize(imgData, (256,256))

imgMask = imgData < 0.5
imgData[imgMask] = 0
imshow(imgData)    # correctly shows the pixels that we want selected. the unwanted pixels are blacked out (value = 0)
python python-3.x image scikit-image scikits
1个回答
1
投票

您可以将NumPy的boolean indexingmean()方法一起用于多维数组:

imgData = imread(dataFilepath, as_gray=True)
imgMask = imgData > 0.5
imgAvg = imgData[imgMask].mean()

在上面的片段中,imgAvg是在强度值大于0.5的像素上计算的平均强度。如果您打算将值小于或等于0.5的图像像素变黑,然后计算整个图像的平均值,则可以使用以下代码:

imgAvg = imgData[imgMask].sum()/imgData.size
© www.soinside.com 2019 - 2024. All rights reserved.