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 调用VC++的动态链接库(DLL)
Sep 06 Python
在Docker上开始部署Python应用的教程
Apr 17 Python
用Python将IP地址在整型和字符串之间轻松转换
Mar 22 Python
python登录并爬取淘宝信息代码示例
Dec 09 Python
python3.6.3转化为win-exe文件发布的方法
Oct 31 Python
对Python使用mfcc的两种方式详解
Jan 09 Python
python 计算一个字符串中所有数字的和实例
Jun 11 Python
浅析Windows 嵌入python解释器的过程
Jul 26 Python
Django中使用极验Geetest滑动验证码过程解析
Jul 31 Python
Python实现企业微信机器人每天定时发消息实例
Feb 25 Python
Keras loss函数剖析
Jul 06 Python
python3中apply函数和lambda函数的使用详解
Feb 28 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中防止SQL注入方法详解
2014/12/25 PHP
php实现httpRequest的方法
2015/03/13 PHP
PHP常见错误提示含义解释(实用!值得收藏)
2016/04/25 PHP
fix-ie5.js扩展在IE5下不能使用的几个方法
2007/08/20 Javascript
jQuery调用WebService的实现代码
2011/06/19 Javascript
增强用户体验友好性之jquery easyui window 窗口关闭时的提示
2012/06/22 Javascript
JavaScript取得鼠标绝对位置程序代码介绍
2012/09/16 Javascript
浅析用prototype定义自己的方法
2013/11/14 Javascript
jQuery实现下拉框多选 jquery-multiselect 的实例代码
2016/07/14 Javascript
js实现非常棒的弹出div
2016/10/06 Javascript
jQuery仿写百度百科的目录树
2017/01/03 Javascript
Vue中控制v-for循环次数的实现方法
2018/09/26 Javascript
详解小程序设置缓存并且不覆盖原有数据
2019/04/15 Javascript
vue cli安装使用less的教程详解
2019/07/12 Javascript
javascript实现弹出层效果
2019/12/10 Javascript
ES6 Object.assign()的用法及其使用
2020/01/18 Javascript
Python基于有道实现英汉字典功能
2015/07/25 Python
python 编码规范整理
2018/05/05 Python
python使用opencv驱动摄像头的方法
2018/08/03 Python
详解Python3中ceil()函数用法
2019/02/19 Python
python查看文件大小和文件夹内容的方法
2019/07/08 Python
Python使用Pandas读写Excel实例解析
2019/11/19 Python
pytorch之ImageFolder使用详解
2020/01/06 Python
keras 自定义loss层+接受输入实例
2020/06/28 Python
css3 线性渐变和径向渐变示例附图
2014/04/08 HTML / CSS
HTML5-WebSocket实现聊天室示例
2016/12/15 HTML / CSS
英国男女豪华配饰和礼品网站:Black.co.uk
2020/02/28 全球购物
你们项目是如何进行变更控制的
2015/08/26 面试题
大型主题婚礼活动策划方案
2014/09/15 职场文书
客户经理岗位职责
2015/01/31 职场文书
审美与表现自我评价
2015/03/09 职场文书
安全学习心得体会范文
2016/01/18 职场文书
高中化学教学反思
2016/02/22 职场文书
python基础学习之生成器与文件系统知识总结
2021/05/25 Python
JavaScript实现贪吃蛇游戏
2021/06/16 Javascript
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技