python库skimage给灰度图像染色的方法示例


Posted in Python onApril 27, 2020

灰度图像染成红色和黄色

# 1.将灰度图像转换为RGB图像
image = color.gray2rgb(grayscale_image)
# 2.保留红色分量和黄色分量
red_multiplier = [1, 0, 0]
yellow_multiplier = [1, 1, 0]
# 3.显示图像
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给灰度图像染色的方法示例

HSV图像,H从0到1表示的颜色

hue_gradient = np.linspace(0, 1)
# print(hue_gradient.shape) # output:(50,)
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给灰度图像染色的方法示例

将灰度图像染成不同的颜色

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给灰度图像染色的方法示例

完整代码

"""
=========================
Tinting gray-scale images
=========================

It can be useful to artificially tint an image with some color, either to
highlight particular regions of an image or maybe just to liven up a grayscale
image. This example demonstrates image-tinting by scaling RGB values and by
adjusting colors in the HSV color-space.

In 2D, color images are often represented in RGB---3 layers of 2D arrays, where
the 3 layers represent (R)ed, (G)reen and (B)lue channels of the image. The
simplest way of getting a tinted image is to set each RGB channel to the
grayscale image scaled by a different multiplier for each channel. For example,
multiplying the green and blue channels by 0 leaves only the red channel and
produces a bright red image. Similarly, zeroing-out the blue channel leaves
only the red and green channels, which combine to form yellow.
"""

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)

######################################################################
# In many cases, dealing with RGB values may not be ideal. Because of that,
# there are many other `color spaces`_ in which you can represent a color
# image. One popular color space is called HSV, which represents hue (~the
# color), saturation (~colorfulness), and value (~brightness). For example, a
# color (hue) might be green, but its saturation is how intense that green is
# ---where olive is on the low end and neon on the high end.
#
# In some implementations, the hue in HSV goes from 0 to 360, since hues wrap
# around in a circle. In scikit-image, however, hues are float values from 0
# to 1, so that hue, saturation, and value all share the same scale.
#
# .. _color spaces:
#   https://en.wikipedia.org/wiki/List_of_color_spaces_and_their_uses
#
# Below, we plot a linear gradient in the hue, with the saturation and value
# turned all the way up:
import numpy as np

hue_gradient = np.linspace(0, 1)
# print(hue_gradient.shape) # output:(50,)
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()

######################################################################
# Notice how the colors at the far left and far right are the same. That
# reflects the fact that the hues wrap around like the color wheel (see HSV_
# for more info).
#
# .. _HSV: https://en.wikipedia.org/wiki/HSL_and_HSV
#
# Now, let's create a little utility function to take an RGB image and:
#
# 1. Transform the RGB image to HSV 2. Set the hue and saturation 3.
# Transform the HSV image back to RGB


def colorize(image, hue, saturation=1):
  """ Add color of the given hue to an RGB image.

  By default, set the saturation to 1 so that the colors pop!
  """
  hsv = color.rgb2hsv(image)
  hsv[:, :, 1] = saturation
  hsv[:, :, 0] = hue
  return color.hsv2rgb(hsv)


######################################################################
# Notice that we need to bump up the saturation; images with zero saturation
# are grayscale, so we need to a non-zero value to actually see the color
# we've set.
#
# Using the function above, we plot six images with a linear gradient in the
# hue and a non-zero saturation:

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()

######################################################################
# You can combine this tinting effect with numpy slicing and fancy-indexing
# to selectively tint your images. In the example below, we set the hue of
# some rectangles using slicing and scale the RGB values of some pixels found
# by thresholding. In practice, you might want to define a region for tinting
# based on segmentation results or blob detection methods.

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()

######################################################################
# For coloring multiple regions, you may also be interested in
# `skimage.color.label2rgb http://scikit-
# image.org/docs/0.9.x/api/skimage.color.html#label2rgb`_.

python库skimage给灰度图像染色的方法示例

到此这篇关于python库skimage给灰度图像染色的方法示例的文章就介绍到这了,更多相关python 灰度图像染色内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python实现从url中提取域名的几种方法
Sep 26 Python
Python彩色化Linux的命令行终端界面的代码实例分享
Jul 02 Python
PyQt5实现拖放功能
Apr 25 Python
Django使用详解:ORM 的反向查找(related_name)
May 30 Python
使用python将mysql数据库的数据转换为json数据的方法
Jul 01 Python
python 并发编程 阻塞IO模型原理解析
Aug 20 Python
python中struct模块之字节型数据的处理方法
Aug 27 Python
windows10 pycharm下安装pyltp库和加载模型实现语义角色标注的示例代码
May 07 Python
python3 循环读取excel文件并写入json操作
Jul 14 Python
详解Django中 render() 函数的使用方法
Apr 22 Python
Python NumPy灰度图像的压缩原理讲解
Aug 04 Python
Python软件包安装的三种常见方法
Jul 07 Python
python实现密度聚类(模板代码+sklearn代码)
Apr 27 #Python
Django中文件上传和文件访问微项目的方法
Apr 27 #Python
详解Python中namedtuple的使用
Apr 27 #Python
Python PyQt5运行程序把输出信息展示到GUI图形界面上
Apr 27 #Python
使用python实现微信小程序自动签到功能
Apr 27 #Python
Python日志:自定义输出字段 json格式输出方式
Apr 27 #Python
如何使用PyCharm将代码上传到GitHub上(图文详解)
Apr 27 #Python
You might like
php读取csv文件并输出的方法
2015/03/14 PHP
PHP中应该避免使用同名变量(拆分临时变量)
2015/04/03 PHP
Zend Framework教程之Autoloading用法详解
2016/03/08 PHP
php 截取中英文混合字符串的方法
2018/05/31 PHP
Thinkphp5.0 框架的请求方式与响应方式分析
2019/10/14 PHP
PHP实现创建一个RPC服务操作示例
2020/02/23 PHP
PHP实现页面静态化深入讲解
2021/03/04 PHP
node.js应用后台守护进程管理器Forever安装和使用实例
2014/06/01 Javascript
jQuery常用操作方法及常用函数总结
2014/06/19 Javascript
Javascript基础教程之switch语句
2015/01/18 Javascript
javascript原始值和对象引用实例分析
2015/04/25 Javascript
浅谈toLowerCase和toLocaleLowerCase的区别
2016/08/15 Javascript
详解Vue的computed(计算属性)使用实例之TodoList
2017/08/07 Javascript
vue-swiper的使用教程
2018/08/30 Javascript
Element UI 自定义正则表达式验证方法
2018/09/04 Javascript
js使用swiper实现层叠轮播效果实例代码
2018/12/12 Javascript
vue操作dom元素的3种方法示例
2020/09/20 Javascript
Python基础入门之seed()方法的使用
2015/05/15 Python
Python实现程序的单一实例用法分析
2015/06/03 Python
怎样使用Python脚本日志功能
2016/08/14 Python
Python 实现使用dict 创建二维数据、DataFrame
2018/04/13 Python
查看Django和flask版本的方法
2018/05/14 Python
Python 保存矩阵为Excel的实现方法
2019/01/28 Python
python实现打砖块游戏
2020/02/25 Python
Python使用tkinter实现小时钟效果
2021/02/22 Python
香港优质食材和美酒专门店:FoodWise
2017/09/01 全球购物
澳大利亚运动鞋零售商:The Athlete’s Foot
2018/11/04 全球购物
构造器Constructor是否可被override?
2013/08/06 面试题
VLAN和VPN有什么区别?分别实现在OSI的第几层?
2014/12/23 面试题
介绍一下Java的事务处理
2012/12/07 面试题
自荐信包含哪些内容
2013/10/30 职场文书
舞蹈教育学专业求职信
2014/06/29 职场文书
督导岗位职责
2015/02/04 职场文书
中国合伙人观后感
2015/06/02 职场文书
大学生奶茶店创业计划书
2019/06/25 职场文书
nginx处理http请求实现过程解析
2021/03/31 Servers