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怎么学好python?
Oct 07 Python
python类型强制转换long to int的代码
Feb 10 Python
Python获取网页上图片下载地址的方法
Mar 11 Python
Python实现求最大公约数及判断素数的方法
May 26 Python
Python实现按中文排序的方法示例
Apr 25 Python
手把手教你如何安装Pycharm(详细图文教程)
Nov 28 Python
Python pandas用法最全整理
Aug 04 Python
python进程池实现的多进程文件夹copy器完整示例
Nov 27 Python
python Opencv计算图像相似度过程解析
Dec 03 Python
python中取绝对值简单方法总结
Jul 24 Python
python调用百度API实现人脸识别
Nov 17 Python
想学画画?python满足你!
Dec 24 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
Apache中php.ini的设置方法
2013/02/28 PHP
Yii2使用自带的UploadedFile实现的文件上传
2016/06/20 PHP
Ajax和PHP正则表达式验证表单及验证码
2016/09/24 PHP
php通过pecl方式安装扩展的实例讲解
2018/02/02 PHP
深入理解javascript中return的作用
2013/12/30 Javascript
再次谈论React.js实现原生js拖拽效果引起的一系列问题
2016/04/03 Javascript
详解AngularJs中$resource和restfu服务端数据交互
2016/09/21 Javascript
详解Vue用axios发送post请求自动set cookie
2017/05/10 Javascript
Vue.js 动态为img的src赋值方法
2018/03/14 Javascript
微信小程序登录session的使用
2019/03/17 Javascript
解决vue elementUI中table里数字、字母、中文混合排序问题
2020/01/07 Javascript
JS面向对象编程实现的拖拽功能案例详解
2020/03/03 Javascript
JavaScript字符和ASCII实现互相转换
2020/06/03 Javascript
js实现简单扫雷
2020/11/27 Javascript
解决antd Form 表单校验方法无响应的问题
2020/10/27 Javascript
[55:03]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第二场 11.20
2020/11/20 DOTA
[45:06]完美世界DOTA2联赛PWL S2 Magma vs InkIce 第二场 11.28
2020/12/02 DOTA
python基础教程之基本内置数据类型介绍
2014/02/20 Python
Python使用Paramiko模块编写脚本进行远程服务器操作
2016/05/05 Python
Python处理命令行参数模块optpars用法实例分析
2018/05/31 Python
pandas读取CSV文件时查看修改各列的数据类型格式
2019/07/07 Python
python实现猜数字游戏
2020/03/25 Python
通过celery异步处理一个查询任务的完整代码
2019/11/19 Python
python飞机大战pygame游戏框架搭建操作详解
2019/12/17 Python
AmazeUI 导航条的实现示例
2020/08/14 HTML / CSS
JOSEPH官网:英国奢侈时尚品牌
2018/01/31 全球购物
Java工程师面试集锦之Spring框架
2013/06/16 面试题
50道外企软件测试面试题
2014/08/18 面试题
初始化了一个没有run()方法的线程类,是否会出错?
2014/03/27 面试题
关于保护环境的建议书
2014/05/13 职场文书
计算机专业自荐信范文
2014/05/28 职场文书
领导班子对照检查剖析材料
2014/10/13 职场文书
护士先进个人总结
2015/02/13 职场文书
不同意离婚答辩状
2015/05/22 职场文书
2016年大学生暑期社会实践活动总结
2016/04/06 职场文书
十大最强妖精系宝可梦,哲尔尼亚斯实力最强,第五被称为大力士
2022/03/18 日漫