Mahotas 简明教程

Mahotas - Overlaying Image

叠加图像指的是将一张图像放置在另一张图像之上。它涉及合并两张图像的像素值来创建一张合成图像。

Overlaying an image refers to placing one image on top of another image. It involves combining the pixel values of the two images to create a composite image.

在叠加图像时,将顶部图像放置在背景图像上,允许顶部图像的像素部分或完全覆盖背景图像的像素。

When overlaying an image, the top image is placed on the background image, allowing the pixels of the top image to partially or completely cover the pixels of the background image.

这可以通过不同程度的透明度来完成,允许背景图像在一定程度上显示。

This can be done with varying degrees of transparency, allowing the background image to show through to some extent.

Overlaying Image in Mahotas

我们可以使用 overlay() 函数在 mahotas 中叠加图像。此函数确保叠加图像与基本图像对齐,同时考虑其尺寸和像素值。

We can overlay an image in mahotas using the overlay() function. This function ensures that the overlay image is aligned with the base image, taking into account their dimensions and pixel values.

如果需要,它会自动处理调整叠加图像的大小或裁剪叠加图像以匹配基本图像的大小。

It automatically handles resizing or cropping the overlay image to match the size of the base image if necessary.

要在 mahotas 中叠加图像,我们需要为叠加图像定义透明度或 alpha 值。此值确定叠加图像将覆盖背景图像的程度。

To overlay an image in mahotas, we need to define the transparency or alpha value for the overlay image. This value determines how much the overlay image will cover the background image.

The mahotas.overlay() function

在 mahotas 中,我们使用 mahotas.overlay() 函数来叠加图像。此函数接受单通道图像作为输入并返回生成的叠加图像。

In mahotas we use mahotas.overlay() function to overlay an image. This function accepts a single channel image as an input and returns a resulting overlayed image.

mahotas.overlay() 函数选择每个像素位置的最大像素值,有效地合并两幅图像的视觉内容。

The mahotas.overlay() function selects the maximum pixel value for each pixel location, effectively combining the visual content of both images.

当叠加图像具有透明信息(例如,alpha 通道)时,此操作特别有用,因为它允许叠加图像的透明部分显示基本图像的内容。

This operation is particularly useful when the overlay image has transparency information (e.g., an alpha channel), as it allows the transparent parts of the overlay image to reveal the content of the base image.

Syntax

以下是 mahotas 中 overlay() 函数的基本语法 −

Following is the basic syntax of overlay() function in mahotas −

mahotas.overlay(gray, red=None, green=None, blue=None, if_gray_dtype_not_uint8= 'stretch')

Parameters

以下是 mahotas 中 overlay() 函数接受的参数 −

Following are the parameters accepted by the overlay() function in mahotas −

  1. gray − It is the grayscale image onto which the color channels will be superimposed. It acts as the canvas for overlaying.

  2. red, blue , green (optional) − These represents the individual color channels that will be overlayed on the grayscale image. They can be provided as separate arrays representing the intensity of each color channel. If any of these color channels are not specified (set to None), the resulting overlay image will only contain the grayscale information.

  3. if_gray_dtype_not_uint8 (optional) − It defines what to do if the input image is not of data type 'np.uint8'. Default is stretch.

Example

在以下示例中,我们尝试将图像与二进制掩码叠加(像素值要么是 0(背景)要么是 1(前景) −

In the following example, we are trying to overlay an image with a binary mask (pixel values are either 0 (background) or 1 (foreground) −

import numpy as np
import mahotas as mh
import matplotlib.pyplot as plt
# Load the images
image1 = mh.imread('sea.bmp')
image2 = mh.imread('tree.tiff')
image = mh.imread('sea.bmp', as_grey=True)
# foreground image
mask = mh.imread('tree.tiff', as_grey=True) > 0
overlay = mh.overlay(image, mask)
# Display all three images in one plot
plt.figure(figsize=(10, 5))
# Display image1
plt.subplot(1, 3, 1)
plt.imshow(image1)
plt.title('Image 1')
plt.axis('off')
# Display image2
plt.subplot(1, 3, 2)
plt.imshow(image2)
plt.title('Image 2')
plt.axis('off')
# Display the overlayed image
plt.subplot(1, 3, 3)
plt.imshow(overlay, cmap='gray')
plt.title('Overlayed Image')
plt.axis('off')
plt.tight_layout()
plt.show()

获得的输出如下所示 −

The output obtained is as shown below −

overlaying image

Overlaying a Transparent Image on another Image

要将透明图像叠加到另一张图像上,我们通过创建 alpha 通道来创建透明叠加,该通道决定每个像素的透明度。我们初始化一个与背景图像形状相同的零数组,表示 alpha 通道。

To overlay a transparent image on another image, we create a transparent overlay by creating an alpha channel, which determines the transparency of each pixel. We initialize an array of zeros with the same shape as the background image, representing the alpha channel.

我们为叠加图像中非零的像素设置透明度值大于 0。

We set the transparency value as greater than 0 for pixels in the overlay image that are non−zero.

现在,我们可以通过合并背景图像、叠加图像和 alpha 通道来叠加图像。

Now, we can overlay the images by combining the background image, overlay image, and alpha channel.

我们用(1 - α)乘以背景图像来降低它在存在叠加图像处的光照度,并用图像通道乘以叠加图像来控制其透明度。最后,我们将两个组件相加。

We multiply the background image by (1 − alpha) to reduce its intensity where the overlay image is present, and multiply the overlay image by the alpha channel to control its transparency. Finally, we add the two components together.

Example

在这里,我们正在尝试在另一张图像上叠加一张透明图像 -

Here, we are trying to overlay a transparent image on another image −

import numpy as np
import mahotas as mh
from pylab import imshow, show
# Load the images
image = mh.imread('tree.tiff', as_grey=True)
overlay = mh.imread('sea.bmp', as_grey=True)
# Create a transparent overlay
alpha = np.zeros(image.shape)
alpha[overlay > 0] = 0.5 # Set transparency value for non-zero pixels in
overlay
# Overlay the images
result = mh.stretch(mh.stretch(image) * (1 - alpha) + overlay * alpha)
# Display the result
imshow(result)
show()

上述代码的输出如下:

Output of the above code is as follows −

overlay transparent image

Overlaying an Image with a specified Transparency Level

要叠加有指定透明级别图像,我们需要先定义所需的叠加图像的透明级别。

To overlay an image with a specified transparency level we need to first define the transparency level for the overlay image we desire.

透明度级别介于 0.0(完全透明)和 1.0(完全不透明)之间。这个值决定了背景图像和叠加图像之间的混合比率。

The transparency level is a value between 0.0 (fully transparent) and 1.0 (fully opaque). This value determines the blending ratio between the background image and the overlay image.

然后,使用 alpha 混合,我们可以基于定义的透明度级别将图像合并在一起。然后,我们需要通过降低叠加图像存在的区域的背景图像的光照度来调整背景图像的光照度。

Then using alpha blending, we can merge the images together based on the defined transparency level. Then we need to adjust the background image intensity by reducing the intensity of the background image in regions where the overlay image is present.

为了实现此目的,我们将背景图像乘以(1 - α)。然后,我们通过将叠加图像乘以 alpha 值来控制其透明度。

To achieve this, we multiply the background image by (1 − alpha). We then control the transparency of the overlay image by multiplying it with the alpha value.

然后将调整后的背景图像和具有受控透明度的叠加图像相加,以创建最终的叠加图像。

The adjusted background image and the overlay image with controlled transparency are then added together to create the final overlayed image.

Example

现在,我们正在尝试叠加一个具有指定透明度的图像 -

Now, we are trying to overlay an image with a specified transparency level −

import numpy as np
import mahotas as mh
from pylab import imshow, show
# Load the images
image = mh.imread('tree.tiff')
overlay = mh.imread('sea.bmp')
# Define the transparency level
alpha = 0.5
# Blend the images using alpha blending and transparency level
result = mh.stretch(mh.stretch(image) * (1 - alpha) + overlay * alpha)
# Display the result
imshow(result)
show()

以下是上面代码的输出: -

Following is the output of the above code −

overlay transparent image1