📜  Mahotas – RGB 到灰色的转换

📅  最后修改于: 2022-05-13 01:54:28.815000             🧑  作者: Mango

Mahotas – RGB 到灰色的转换

在本文中,我们将了解如何在 mahotas 中将 rgb 图像转换为灰色。 RGB 图像(有时称为真彩色图像)作为 m×n×3 数据数组存储在 MATLAB 中,该数组定义了每个像素的红色、绿色和蓝色分量。
在本教程中,我们将使用“lena”图像,下面是加载它的命令。

mahotas.demos.load('lena')

下面是莉娜的图片

为此,我们将使用 mahotas.colors.rgb2greymethod

注意:它可以使用 rgb2grey 或 rgb2gray 将图像转换为灰度,两者都有效)。此转换使用视觉逼真的方法(人眼对绿色通道更敏感,因此对绿色通道的权重更大)
下面是实现

Python3
# importing required libraries
import mahotas
import mahotas.demos
from pylab import gray, imshow, show
import numpy as np
  
# loading image
img = mahotas.demos.load('lena')
 
  
# showing image
print("Image")
imshow(img)
show()
 
# rgb to gray
new_img = mahotas.colors.rgb2gray(img)
 
# showing new image
print("New Image")
imshow(new_img)
show()


Python3
# importing required libraries
import mahotas
import numpy as np
import matplotlib.pyplot as plt
import os
  
# loading image
img = mahotas.imread('dog_image.png')
       
# filtering image
img = img[:, :, :3]
 
# showing image
print("Image")
imshow(img)
show()
 
# rgb to gray
new_img = mahotas.colors.rgb2gray(img)
 
# showing new image
print("New Image")
imshow(new_img)
show()


输出 :

Image

New Image

另一个例子

Python3

# importing required libraries
import mahotas
import numpy as np
import matplotlib.pyplot as plt
import os
  
# loading image
img = mahotas.imread('dog_image.png')
       
# filtering image
img = img[:, :, :3]
 
# showing image
print("Image")
imshow(img)
show()
 
# rgb to gray
new_img = mahotas.colors.rgb2gray(img)
 
# showing new image
print("New Image")
imshow(new_img)
show()

输出 :

Image

New Image