手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python中使用skimage模块给灰度图像着色

[日期:2020-04-26] 来源:Linux公社  作者:Linux [字体: ]

人工用某种颜色对图像进行着色可能会有用,要么突出显示图像的特定区域,要么只是使灰度图像生动化。 本示例通过缩放RGB值并通过调整HSV颜色空间中的颜色来演示图像着色。

Python中使用skimage模块给灰度图像着色

在2D中,彩色图像通常以RGB-3层2D阵列表示,其中3层表示图像的(R)ed,(G)reen和(B)lue通道。 获取着色图像的最简单方法是将每个RGB通道设置为通过每个通道的不同乘数缩放的灰度图像。 例如,将绿色和蓝色通道乘以0仅留下红色通道并产生明亮的红色图像。 同样,将蓝色通道归零后,仅留下红色和绿色通道,它们合并形成黄色。

import matplotlib.pyplot as plt
from skimage import data
from skimage import color
from skimage import img_as_float

grayscale_image = img_as_float(data.camera()[::2, ::2])
image = color.gray2rgb(grayscale_image)

red_multiplier = [1, 0, 0]
yellow_multiplier = [1, 1, 0]

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4),
                              sharex=True, sharey=True)
ax1.imshow(red_multiplier * image)
ax2.imshow(yellow_multiplier * image)

Python中使用skimage模块给灰度图像着色

在许多情况下,处理RGB值可能不是理想的选择。 因此,还有许多其他颜色空间可用来表示彩色图像。 一种流行的色彩空间称为HSV,它代表色调(~the color颜色),饱和度(~colorfulness色度)和值(~brightness亮度)。 例如,颜色(hue))可能是绿色,但其饱和度是绿色的强烈程度- 橄榄色在低端,霓虹色在高端。

在某些实现中,HSV中的色相从0变为360,因为色相环绕成一个圆圈。 但是,在scikit图像中,色相是从0到1的浮点值,因此色相,饱和度和值都共享相同的比例。

下面,我们在色相中绘制线性渐变,饱和度和值一直向上调:

import numpy as np

hue_gradient = np.linspace(0, 1)
hsv = np.ones(shape=(1, len(hue_gradient), 3), dtype=float)
hsv[:, :, 0] = hue_gradient

all_hues = color.hsv2rgb(hsv)

fig, ax = plt.subplots(figsize=(5, 2))
# Set image extent so hues go from 0 to 1 and the image is a nice aspect ratio.
ax.imshow(all_hues, extent=(0, 1, 0, 0.2))
ax.set_axis_off()

Python中使用skimage模块给灰度图像着色

请注意,最左边和最右边的颜色是相同的。 这反映了色相像色轮一样缠绕的事实(有关更多信息,请参见HSV)。

现在,让我们创建一个实用工具来拍摄RGB图像,然后:

1、将RGB图像转换为HSV 2,设置色相和饱和度3,将HSV图像转换回RGB。

def colorize(image, hue, saturation=1):
    """将给定色相的颜色添加到RGB图像中。

    默认情况下,将饱和度设置为1,以便弹出颜色!
    """
    hsv = color.rgb2hsv(image)
    hsv[:, :, 1] = saturation
    hsv[:, :, 0] = hue
    return color.hsv2rgb(hsv)

注意,我们需要提高饱和度。 零饱和度的图像是灰度图像,因此我们需要一个非零值才能实际看到我们设置的颜色。

使用上面的函数,我们绘制了六个图像,这些图像的色调呈线性梯度且饱和度为非零:

hue_rotations = np.linspace(0, 1, 6)

fig, axes = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True)

for ax, hue in zip(axes.flat, hue_rotations):
    # Turn down the saturation to give it that vintage look.
    tinted_image = colorize(image, hue, saturation=0.3)
    ax.imshow(tinted_image, vmin=0, vmax=1)
    ax.set_axis_off()
fig.tight_layout()

Python中使用skimage模块给灰度图像着色

您可以将此着色效果与numpy切片和花式索引结合使用,以有选择地对图像进行着色。 在下面的示例中,我们使用切片设置某些矩形的色相,并缩放通过阈值找到的某些像素的RGB值。 在实践中,您可能想基于分割结果或斑点检测方法定义要着色的区域。

from skimage.filters import rank

# Square regions defined as slices over the first two dimensions.
top_left = (slice(100),) * 2
bottom_right = (slice(-100, None),) * 2

sliced_image = image.copy()
sliced_image[top_left] = colorize(image[top_left], 0.82, saturation=0.5)
sliced_image[bottom_right] = colorize(image[bottom_right], 0.5, saturation=0.5)

# Create a mask selecting regions with interesting texture.
noisy = rank.entropy(grayscale_image, np.ones((9, 9)))
textured_regions = noisy > 4
# Note that using `colorize` here is a bit more difficult, since `rgb2hsv`
# expects an RGB image (height x width x channel), but fancy-indexing returns
# a set of RGB pixels (# pixels x channel).
masked_image = image.copy()
masked_image[textured_regions, :] *= red_multiplier

fig, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4),
                              sharex=True, sharey=True)
ax1.imshow(sliced_image)
ax2.imshow(masked_image)

plt.show()

Python中使用skimage模块给灰度图像着色

关注Linux公社微信公众号(linuxidc_com),(见https://www.linuxidc.com/Linux/2013-12/93755.htm),在Linux公社微信公众号后台发送发送“163045”即可获得本文代码。

更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2020-04/163045.htm

linux
相关资讯       Python模块  Python skimage  灰度图像 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款