如何在python中提取黑白图像中的相对颜色强度?

时间:2021-04-14 00:23:37

Suppose I have got a black an white image, how do I convert the colour intensity at each point into a numerical value that represents its relativity intensity?

假设我有一个黑色的白色图像,如何将每个点的颜色强度转换为表示其相对强度的数值?

I checked somewhere on the web and found the following: Intensity = np.asarray(PIL.Image.open('test.jpg'))

我检查了网络上的某个地方,发现了以下内容:Intensity = np.asarray(PIL.Image.open('test.jpg'))

What's the difference between asarray and array? Besides, the shape of the array Intensity is '181L, 187L, 3L'. The size of the image test.jpg is 181x187, so what does the extra '3' represent?

asarray和array之间有什么区别?此外,阵列强度的形状为'181L,187L,3L'。图像test.jpg的大小是181x187,那么额外的'3'代表什么呢?

And are there any other better ways of extracting the colour intensity of an image? thank you.

还有其他更好的方法来提取图像的颜色强度吗?谢谢。

1 个解决方案

#1


The image is being opened as a color image, not as a black and white one. The shape is 181x187x3 because of that: the 3 is there because each pixel is an RGB value. Quite often images in black and white are actually stored in an RGB format. For an image image, if np.all(image[:,:,0]==image[:,:,1]) and so on, then you can just choose to use any of them (eg, image[:,:,0]). Alternatively, you could take the mean with np.mean(image,axis=2).

图像作为彩色图像打开,而不是黑白图像。形状是181x187x3,因为:3是因为每个像素都是RGB值。通常,黑白图像实际上以RGB格式存储。对于图像图像,如果np.all(图像[:,:0] ==图像[:,:,1])等等,那么你可以选择使用它们中的任何一个(例如,图像[:, :0])。或者,您可以使用np.mean(图像,轴= 2)取平均值。

Note too that the range of values will depend on the format, and so depending upon what you mean by color intensity, you may need to normalize them. In the case of a jpeg, they are probably uint8s, so you may want image[:,:,0].astype('float')/255 or something similar.

另请注意,值的范围取决于格式,因此根据颜色强度的含义,您可能需要对它们进行标准化。在jpeg的情况下,它们可能是uint8s,所以你可能想要图像[:,:0] .astype('float')/ 255或类似的东西。

#1


The image is being opened as a color image, not as a black and white one. The shape is 181x187x3 because of that: the 3 is there because each pixel is an RGB value. Quite often images in black and white are actually stored in an RGB format. For an image image, if np.all(image[:,:,0]==image[:,:,1]) and so on, then you can just choose to use any of them (eg, image[:,:,0]). Alternatively, you could take the mean with np.mean(image,axis=2).

图像作为彩色图像打开,而不是黑白图像。形状是181x187x3,因为:3是因为每个像素都是RGB值。通常,黑白图像实际上以RGB格式存储。对于图像图像,如果np.all(图像[:,:0] ==图像[:,:,1])等等,那么你可以选择使用它们中的任何一个(例如,图像[:, :0])。或者,您可以使用np.mean(图像,轴= 2)取平均值。

Note too that the range of values will depend on the format, and so depending upon what you mean by color intensity, you may need to normalize them. In the case of a jpeg, they are probably uint8s, so you may want image[:,:,0].astype('float')/255 or something similar.

另请注意,值的范围取决于格式,因此根据颜色强度的含义,您可能需要对它们进行标准化。在jpeg的情况下,它们可能是uint8s,所以你可能想要图像[:,:0] .astype('float')/ 255或类似的东西。