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线程池的实现实例
Nov 18 Python
Python中针对函数处理的特殊方法
Mar 06 Python
python实现猜数字游戏(无重复数字)示例分享
Mar 29 Python
Python实现ssh批量登录并执行命令
Oct 25 Python
Python实现Youku视频批量下载功能
Mar 14 Python
Python实现桶排序与快速排序算法结合应用示例
Nov 22 Python
Python 实现在文件中的每一行添加一个逗号
Apr 29 Python
python3 判断列表是一个空列表的方法
May 04 Python
Python列表list排列组合操作示例
Dec 18 Python
python列表使用实现名字管理系统
Jan 30 Python
Django通过json格式收集主机信息
May 29 Python
详解BeautifulSoup获取特定标签下内容的方法
Dec 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
长波有什么东西
2021/03/01 无线电
shell脚本作为保证PHP脚本不挂掉的守护进程实例分享
2013/07/15 PHP
PHP缓存集成库phpFastCache用法
2014/12/15 PHP
php获取百度收录、百度热词及百度快照的方法
2015/04/02 PHP
关于PHP通用返回值设置方法
2017/03/31 PHP
laravel 多图上传及图片的存储例子
2019/10/14 PHP
jquery的ajax和getJson跨域获取json数据的实现方法
2014/02/04 Javascript
javascript实现多级联动下拉菜单的方法
2015/02/06 Javascript
基于JavaScript实现百度搜索框效果
2020/06/28 Javascript
JS脚本加载后执行相应回调函数的操作方法
2018/02/28 Javascript
引入外部js脚本加载慢与页面白屏问题的解决
2018/12/10 Javascript
vue动态设置页面title的方法实例
2020/08/23 Javascript
Python 由字符串函数名得到对应的函数(实例讲解)
2017/08/10 Python
Python实现对字典分别按键(key)和值(value)进行排序的方法分析
2018/12/19 Python
Python3中列表list合并的四种方法
2019/04/19 Python
利用anaconda保证64位和32位的python共存
2021/03/09 Python
pytorch实现seq2seq时对loss进行mask的方式
2020/02/18 Python
python实现跨excel sheet复制代码实例
2020/03/03 Python
Selenium python时间控件输入问题解决方案
2020/07/22 Python
PyQt5结合matplotlib绘图的实现示例
2020/09/15 Python
HTML5 canvas基本绘图之文字渲染
2016/06/27 HTML / CSS
HTML5本地存储之Web Storage详解
2016/07/04 HTML / CSS
科尔士百货公司官网:Kohl’s
2016/07/11 全球购物
英国第一职业高尔夫商店:Clickgolf.co.uk
2020/11/18 全球购物
楼面部长岗位职责范本
2014/02/14 职场文书
社区健康教育实施方案
2014/03/18 职场文书
法人授权委托书范本
2014/04/04 职场文书
关于感恩的演讲稿800字
2014/08/26 职场文书
导游词300字
2015/02/13 职场文书
培训班通知
2015/04/25 职场文书
春节晚会开场白
2015/05/29 职场文书
消防安全培训工作总结
2015/10/23 职场文书
python opencv人脸识别考勤系统的完整源码
2021/04/26 Python
Python 流媒体播放器的实现(基于VLC)
2021/04/28 Python
浅谈Python中的函数(def)及参数传递操作
2021/05/25 Python
WCG2010 星际争霸决赛 Flash vs Goojila 1 星际经典比赛回顾
2022/04/01 星际争霸