Scipy.misc.imread flatten 参数——转换为灰度 [英] Scipy.misc.imread flatten argument -- converting to grey scale
问题描述
我正在尝试了解此方法如何以灰度转换图像(如果它使用简单均值或加权均值)——我必须参考此方法.
I'm trying to understand how this method transform an image in grey scale (if it uses a simple mean or a weighted mean) -- I have to reference this method.
从 文档 我知道此方法调用 convert('F')
方法.
From the documentation I know that this method calls the convert(‘F’)
method.
来自 Pillow/PIL 源代码,我可以找到这个方法,但是,当mode
参数设置为'F'时,我找不到它的作用.
From Pillow/PIL source code, I can find this method, however, I couldn't find what it does when the mode
parameter is set to 'F'.
谢谢.
推荐答案
在 Image
对象的 convert
方法的文档字符串中(在你链接的代码中看到到),有这个:
In the docstring of the convert
method of the Image
object (seen in the code that you linked to), there is this:
将彩色图像转换为黑白(模式L")时,该库使用 ITU-R 601-2 亮度变换::
When translating a color image to black and white (mode "L"), the library uses the ITU-R 601-2 luma transform::
L = R * 299/1000 + G * 587/1000 + B * 114/1000
L = R * 299/1000 + G * 587/1000 + B * 114/1000
显然这也是 mode='F'
的处理方式.
Apparently this is also how mode='F'
is handled.
这是一个例子:
In [2]: from PIL import Image
为演示创建一个数组:
In [3]: x = np.random.randint(0, 9, size=(4, 4, 3)).astype(np.uint8)
使用 convert
方法中的 mode='F'
通过转换将数组发送到图像并返回到数组:
Send the array through the conversion to an Image and back to an array, using mode='F'
in the convert
method:
In [4]: np.array(Image.fromarray(x).convert('F'))
Out[4]:
array([[ 3.24499989, 6.30499983, 1.86899996, 4.54400015],
[ 3.54399991, 5.04300022, 4.63000011, 0.29899999],
[ 2.0539999 , 3.29900002, 1.85800004, 1.76100004],
[ 3.9289999 , 4.76100016, 5.76100016, 2.47799993]], dtype=float32)
将 x
乘以 convert
的文档字符串中显示的因子:
Multiply x
by the factors shown in the docstring of convert
:
In [5]: f = np.array([0.299, 0.587, 0.114])
In [6]: x.dot(f)
Out[6]:
array([[ 3.245, 6.305, 1.869, 4.544],
[ 3.544, 5.043, 4.63 , 0.299],
[ 2.054, 3.299, 1.858, 1.761],
[ 3.929, 4.761, 5.761, 2.478]])
如果我们转换为 np.float32
,我们会看到与 convert
方法创建的值完全相同:
If we convert to np.float32
, we see exactly the same values as created by the convert
method:
In [7]: x.dot(f).astype(np.float32)
Out[7]:
array([[ 3.24499989, 6.30499983, 1.86899996, 4.54400015],
[ 3.54399991, 5.04300022, 4.63000011, 0.29899999],
[ 2.0539999 , 3.29900002, 1.85800004, 1.76100004],
[ 3.9289999 , 4.76100016, 5.76100016, 2.47799993]], dtype=float32)
这篇关于Scipy.misc.imread flatten 参数——转换为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!