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转码问题的解决方法
Oct 07 Python
Python2.7基于淘宝接口获取IP地址所在地理位置的方法【测试可用】
Jun 07 Python
python flask中静态文件的管理方法
Mar 20 Python
python3实现163邮箱SMTP发送邮件
May 22 Python
TensorFlow 滑动平均的示例代码
Jun 19 Python
浅谈python 读excel数值为浮点型的问题
Dec 25 Python
python自动发送测试报告邮件功能的实现
Jan 22 Python
解决python 上传图片限制格式问题
Oct 30 Python
使用pyqt 实现重复打开多个相同界面
Dec 13 Python
Python基于Tensor FLow的图像处理操作详解
Jan 15 Python
python批量合成bilibili的m4s缓存文件为MP4格式 ver2.5
Dec 01 Python
Python命令行参数argv和argparse该如何使用
Feb 08 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
我的论坛源代码(八)
2006/10/09 PHP
56.com视频采集接口程序(PHP)
2007/09/22 PHP
对象失去焦点时自己动提交数据的实现代码
2012/11/06 PHP
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
2017/07/22 PHP
php解决约瑟夫环算法实例分析
2019/09/30 PHP
建立良好体验度的Web注册系统ajax
2007/07/09 Javascript
js 获取服务器控件值的代码
2010/03/05 Javascript
javascript下对于事件、事件流、事件触发的顺序随便说说
2010/07/17 Javascript
商城常用滚动的焦点图效果代码简单实用
2013/03/28 Javascript
JS中批量给元素绑定事件过程中的相关问题使用闭包解决
2013/04/15 Javascript
解析javascript 浏览器关闭事件
2013/07/08 Javascript
网页中表单按回车就自动提交的问题的解决方案
2014/11/03 Javascript
推荐JavaScript实现继承的最佳方式
2014/11/11 Javascript
JS中Location使用详解
2015/05/12 Javascript
两款JS脚本判断手机浏览器类型跳转WAP手机网站
2015/10/16 Javascript
微信小程序 Video API实例详解
2016/10/02 Javascript
原生JS简单实现ajax的方法示例
2016/11/29 Javascript
Vue数据驱动模拟实现5
2017/01/13 Javascript
探究react-native 源码的图片缓存问题
2017/08/24 Javascript
Vue cli构建及项目打包以及出现的问题解决
2018/08/27 Javascript
webpack4.0+vue2.0利用批处理生成前端单页或多页应用的方法
2019/06/28 Javascript
微信小程序全局变量的设置、使用、修改过程解析
2019/09/24 Javascript
在Vue项目中,防止页面被缩放和放大示例
2019/10/28 Javascript
python实现域名系统(DNS)正向查询的方法
2016/04/19 Python
python验证码识别实例代码
2018/02/03 Python
python中字符串比较使用is、==和cmp()总结
2018/03/18 Python
Python实现自定义顺序、排列写入数据到Excel的方法
2018/04/23 Python
解决pip install xxx报错SyntaxError: invalid syntax的问题
2018/11/30 Python
python安装dlib库报错问题及解决方法
2020/03/16 Python
Vs Code中8个好用的python 扩展插件
2020/10/12 Python
纯css3实现的竖形无限级导航
2014/12/10 HTML / CSS
瑞士国际航空官网:SWISS
2016/07/21 全球购物
学习十八大标语
2014/10/09 职场文书
2014年煤矿工作总结
2014/11/24 职场文书
优秀范文:《但愿人长久》教学反思3篇
2019/10/24 职场文书
MySQL数据库完全卸载的方法
2022/03/03 MySQL