Mahotas 简明教程
Mahotas - Color-Space Conversion
颜色空间是指在图像处理和信号以及系统中用于各种目的的不同类型的颜色模式。一些常见的颜色空间有 −
Color spaces refers to different types of color modes, used in image processing and signals and system for various purposes. Some of the common color spaces are −
-
CMY’K− It is a subtractive color model used in printing and is represented by four color channels: Cyan ©, Magenta (M), Yellow (Y), and Key (K) for black.
-
Y’UV− It is a color space used in video encoding and image processing. It separates the image information into luminance (Y') and chrominance (UV) components. The Y' channel represents the brightness or grayscale information, while the U and V channels represent the color information.
-
YIQ− It is a color space used in analog television systems, where the Y channel represents brightness and the I and Q channels represent color information.
-
Y’CbCr− It is a color space commonly used in digital image and video encoding, where the Y' channel represents brightness and the Cb and Cr channels represent color information. The Y' channel contains the grayscale information, while the Cb and Cr channels represent the blue−difference and red−difference chroma components, respectively.
-
HSV− HSV (Hue, Saturation, Value) is a color space used to represent colors in a way that is more perceptually meaningful to humans.
在我们的教程中,我们将广泛讨论关于 RGB color space 。
In our tutorial, we will widely discuss about RGB color space.
RGB Color Space
RGB 代表红色、绿色和蓝色。这是最广泛使用的颜色空间,我们将在后续章节中详细讨论它。
RGB stands for red green and blue. It is the most widely used color space, and we will discuss about it in detail in the further chapters.

RGB 模型指出,每个彩色图像实际上是由三张不同的图像组成的。
The RGB model states that each color image is actually formed of three different images.
红色图像、蓝色图像和黑色图像。普通的灰度图像仅由一个矩阵定义,但彩色图像实际上由三个不同的矩阵组成。
Red image, Blue image, and black image. A normal grayscale image is defined by only one matrix, but a color image is actually composed of three different matrices.
One color image matrix = red matrix + blue matrix + green matrix
这在以下数字中可以最好地看出 −
This can be best seen in this figure below −

Available Functions
以下是在 mahotas 中用于颜色空间转换的不同函数 −
Following are the different functions available in mahotas for color space conversion −
S.No |
Function & Description |
1 |
*rgb2gray()*This function converts an RGB image to a grayscale image. |
2 |
*rgb2grey()*This function converts an RGB image to a grayscale image. |
3 |
*rgb2lab()*This function converts an RGB image to L*a*b coordinates. |
4 |
*rgb2sepia()*This function converts an RGB image to sepia i.e. a reddish−brown color. |
5 |
*rgb2xyz()*This function converts an RGB image to XYZ color space i.e. brightness, color and intensity |
6 |
*xyz2lab()*This functions converts XYZ to L*a*b color space. |
7 |
*xyz2rgb()*This function converts XYZ to RGB color space. |
现在,让我们看看其中一些函数的示例。
Now, lets us see examples of some of these functions.
The rgb2grey() Function
rgb2grey() 函数用于将 RGB 图像转换为灰度图像。
The rgb2grey() function is used to convert an RGB image to a grayscale image.
此函数假设输入图像是一个表示 RGB 图像的二维 NumPy 数组,其中维度分别为 (高度、宽度、3),分别代表高度、宽度和 RGB 通道。
This function assumes that the input image is a 2D NumPy array representing an RGB image, where the dimensions are (height, width, 3) for the height, width, and RGB channels, respectively.
如果图像已经是灰度(即仅有一个通道),该函数只需返回该图像而不进行任何修改。
If the image is already grayscale (i.e., only has one channel), the function simply returns the image without any modifications.
Example
以下是使用 rgbtogrey() 函数将 RGB 图像转换为灰度图像的基本示例 −
Following is the basic example of converting an RGB image to a grayscale image using the rgbtogrey() function −
import mahotas as mh
import numpy as np
from pylab import imshow, show
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
grey_image = mh.colors.rgb2grey(image)
imshow(grey_image)
show()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −

The rgb2sepia() Function
rgb2grey() 函数用于将 RGB 图像转换为棕褐色图像,即红棕色的图像。
The rgb2grey() function is used to convert an RGB image to a sepia toned image i.e. a reddish−brown color image.
为了将 RGB 图像转换为棕褐色调,此函数会对每个像素的 RGB 值应用一个变换。此变换涉及调整红色、绿色和蓝色通道以实现所需的棕褐色调效果。
To convert an RGB image to sepia, the function applies a transformation to the RGB values of each pixel. This transformation involves adjusting the red, green, and blue channels to achieve the desired sepia effect.
Example
以下是将 RGB 色彩空间转换为棕褐色调图像的基本实现:
Here is the basic implementation of RGB color space to sepia toned image −
import mahotas as mh
import numpy as np
from pylab import imshow, show
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
sepia_image = mh.colors.rgb2sepia(image)
imshow(sepia_image)
show()
以下是上面代码的输出: -
Following is the output of the above code −

我们已经在本节的其余章节详细讨论了这些函数。
We have discussed these functions in detail in the remaining chapters of this section.