Mahotas 简明教程
Mahotas - Image Stretch RGB
图像拉伸是一种通过将原始像素值映射到新的值范围来增强图像对比度的技术。这通常是为了提高细节的可见性和增强图像的整体外观。
Image stretching is a technique of enhancing the contrast of an image by mapping the original pixel values to a new range of values. This is generally done to improve the visibility of details and enhance the overall appearance of the image.
对于 RGB 图像,每个像素有三个颜色通道——红色、绿色和蓝色。拉伸 RGB 图像涉及独立拉伸每个颜色通道的值以扩展强度范围。
In the case of RGB images, each pixel has three color channels− red, green, and blue. Stretching the RGB image involves independently stretching the values of each color channel to expand the range of intensities.
Image Stretch RGB in Mahotas
在 Mahotas 中,通过拉伸图像的单独颜色通道(红、绿和蓝)来执行 RGB 图像拉伸。拉伸过程涉及将原始像素强度映射到新的范围以增加对比度。
In Mahotas, RGB image stretching is performed by stretching the individual color channels (Red, Green, and Blue) of the image. The stretching process involves mapping the original pixel intensities to a new range to increase the contrast.
拉伸各个 RGB 通道后,需要将它们重新组合以创建最终的拉伸图像。
After stretching the individual RGB channels, they need to be combined back to create the final stretched image.
我们可以使用 stretch() 函数和 stretch_rgb() 函数在 mahotas 中执行图像拉伸 RGB。
We can perform image stretching RGB in mahotas using the stretch() function and the stretch_rgb() function.
Using the stretch() and the stretch_rgb() Functions
mahotas.stretch() 和 mahotas.stretch_rgb() 函数将 RGB 图像中每个通道中的像素值拉伸到指定的 min_value 和 max_value 之间。
The mahotas.stretch() and mahotas.stretch_rgb() functions stretches the pixel values in each channel of the RGB image between the specified min_value and max_value.
值低于 min_value 的像素将设置为 min_value,值高于 max_value 的像素将设置为 max_value。拉伸在每个通道上独立执行。
Pixels with values below min_value will be set to min_value, and pixels with values above max_value will be set to max_value. The stretching is performed independently on each channel.
以下是 mahotas.stretch() 函数的基本语法 −
Following is the basic syntax for mahotas.stretch() function −
mahotas.stretch(img,arg0=None, arg1=None, dtype=<type 'numpy.uint8'>)
以下是 mahotas.stretch_rgb() 函数的基本语法 −
Following is the basic syntax of mahotas.stretch_rgb() function −
mahotas.stretch(img,arg0=None, arg1=None, dtype=<type 'numpy.uint8'>)
以下参数传递给 mahotas.stretch() 函数 −
Following are the parameters passed to the mahotas.stretch() function −
img − 它是输入图像。
img − It is the input image.
arg0 and arg1 (optional) − 这些参数指定像素值应该拉伸的范围。这些参数的解释取决于它们的值−
arg0 and arg1 (optional) − These parameters specify the range within which the pixel values should be stretched. The interpretation of these arguments depends on their values −
-
If both arg0 and arg1 are provided and are not None, they represent the minimum and maximum values that the pixel values should be stretched to. The pixel values below arg0 will be set to arg0, and pixel values above arg1 will be set to arg1.
-
If only arg0 is provided and arg1 is None, arg0 should be a tuple or list of length 2 representing the percentile values. The pixel values below the value at the first element of arg0 will be set to that value, and pixel values above the value at the second element of arg0 will be set to that value.
-
If both arg0 and arg1 are None, the pixel values will be stretched to the full range of the data type specified by the dtype parameter (default is numpy.uint8).
dtype − 它指定输出图像的数据类型。默认值是 numpy.uint8,它表示无符号 8 位整数。
dtype − It specifies the data type of the output image. The default value is numpy.uint8, which represents an unsigned 8-bit integer.
在以下示例中,我们正在使用 mahotas.stretch() 函数来拉伸图像−
In the following example, we are using the mahotas.stretch() function to stretch the image−
import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image = mh.imread('sun.png')
# Stretch image
stretched = mh.stretch(image, arg0=100, arg1=280)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the stretched grayscale image
axes[1].imshow(stretched, cmap='gray')
axes[1].set_title('Stretched Grayscale Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()
以下是上面代码的输出: -
Following is the output of the above code −
在此示例中,我们将看到如何使用 mahotas.stretch_rgb 函数拉伸图像−
In this example, we will see how to use mahotas.stretch_rgb function to stretch an image −
import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image = mh.imread('nature.jpeg')
# Stretch image
stretched = mh.stretch_rgb(image, arg0=100, arg1=280)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the stretched grayscale image
axes[1].imshow(stretched, cmap='gray')
axes[1].set_title('Stretched Grayscale Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()
以下是上面代码的输出: -
Following is the output of the above code −
Stretching Image with Percentile Value
要在 Mahotas 中用百分比值拉伸 RGB 图像,我们可以手动计算百分比值并在每个颜色通道上单独使用拉伸函数−
To stretch an RGB image with percentile values in Mahotas, we can manually calculate the percentiles and use the stretch function individually on each color channel −
-
Firstly, split the RGB image into individual color channels (red, green, and blue).
-
This can be achieved by using the np.split() function from the NumPy library.
-
Once the image is split, calculate the desired percentiles for each color channel using NumPy’s np.percentile() function.
-
After obtaining the percentiles, use the stretch() function from Mahotas to each channel independently, using the calculated minimum and maximum values.
-
This will stretch the pixel values within the desired percentile range for each channel.
最后,通过沿着颜色通道轴连接拉伸通道,将拉伸通道合并回 RGB 图像中。结果图像将是基于指定百分比值的拉伸 RGB 图像。
Finally, merge the stretched channels back into an RGB image by concatenating them along the color channel axis. The resulting image will be the stretched RGB image based on the specified percentiles.
Example
在这里,我们尝试用百分比值拉伸图像−
In here, we are trying to stretch an image with percentile value −
import mahotas as mh
import numpy as np
from pylab import imshow, show
image = mh.imread('nature.jpeg')
# Splitting the RGB image into individual channels
r, g, b = np.split(image, 3, axis=2)
# Calculating percentiles for each channel
r_min, r_max = np.percentile(r, [17, 90])
g_min, g_max = np.percentile(g, [25, 75])
b_min, b_max = np.percentile(b, [50, 99])
# Stretching each channel independently
stretched_r = mh.stretch(r, r_min, r_max)
stretched_g = mh.stretch(g, g_min, g_max)
stretched_b = mh.stretch(b, b_min, b_max)
# Merging the stretched channels back into an RGB image
stretched_image = np.concatenate((stretched_r, stretched_g, stretched_b),
axis=2)
imshow(stretched_image)
show()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −