python的skimage库 图像色彩空间转换 RGB到灰度;RGB到HSV

Posted 我坚信阳光灿烂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python的skimage库 图像色彩空间转换 RGB到灰度;RGB到HSV相关的知识,希望对你有一定的参考价值。

RGB 到 grayscale

from skimage.color import rgb2gray
grayscale = rgb2gray(original)

"""
================
RGB to grayscale
================

This example converts an image with RGB channels into an image with a single
grayscale channel.

The value of each grayscale pixel is calculated as the weighted sum of the
corresponding red, green and blue pixels as::

        Y = 0.2125 R + 0.7154 G + 0.0721 B

"""
import matplotlib.pyplot as plt

from skimage import data
from skimage.color import rgb2gray

original = data.astronaut()
grayscale = rgb2gray(original)

fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()

ax[0].imshow(original)
ax[0].set_title("Original")
ax[1].imshow(grayscale, cmap=plt.cm.gray)
ax[1].set_title("Grayscale")

fig.tight_layout()
plt.show()

技术图片

RGB 到 HSV

from skimage.color import rgb2hsv
hsv_img = rgb2hsv(rgb_img)

实验:将杯子从背景中简单分离

"""
==========
RGB to HSV
==========

This example illustrates how RGB to HSV (Hue, Saturation, Value) conversion 
can be used to facilitate segmentation processes.

Usually, objects in images have distinct colors (hues) and luminosities, so
that these features can be used to separate different areas of the image.
In the RGB representation the hue and the luminosity are expressed as a linear
combination of the R,G,B channels, whereas they correspond to single channels
of the HSV image (the Hue and the Value channels). A simple segmentation of the
image can then be effectively performed by a mere thresholding of the HSV
channels.

"""


import matplotlib.pyplot as plt

from skimage import data
from skimage.color import rgb2hsv

##############################################################################
# We first load the RGB image and extract the Hue and Value channels:

rgb_img = data.coffee()
hsv_img = rgb2hsv(rgb_img)
hue_img = hsv_img[:, :, 0]
value_img = hsv_img[:, :, 2]

fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, figsize=(8, 2))

ax0.imshow(rgb_img)
ax0.set_title("RGB image")
ax0.axis(‘off‘)
ax1.imshow(hue_img, cmap=‘hsv‘)
ax1.set_title("Hue channel")
ax1.axis(‘off‘)
ax2.imshow(value_img)
ax2.set_title("Value channel")
ax2.axis(‘off‘)

fig.tight_layout()

##############################################################################
# We then set a threshold on the Hue channel to separate the cup from the
# background:

hue_threshold = 0.04
binary_img = hue_img > hue_threshold
# print(hue_img) # 图像数值矩阵
# print(binary_img) # True or False 的一个矩阵

fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 3))

# 参数2:bins
ax0.hist(hue_img.ravel(), 512)
ax0.set_title("Histogram of the Hue channel with threshold")
# 设置1条垂直于x轴的红色的虚线
ax0.axvline(x=hue_threshold, color=‘r‘, linestyle=‘dashed‘, linewidth=2)
# 设置x轴范围
ax0.set_xbound(0, 0.12)
ax1.imshow(binary_img)
ax1.set_title("Hue-thresholded image")
ax1.axis(‘off‘)

fig.tight_layout()

##############################################################################
# We finally perform an additional thresholding on the Value channel to partly
# remove the shadow of the cup:

fig, ax0 = plt.subplots(figsize=(4, 3))

value_threshold = 0.10
binary_img = (hue_img > hue_threshold) | (value_img < value_threshold)

ax0.imshow(binary_img)
ax0.set_title("Hue and value thresholded image")
ax0.axis(‘off‘)

fig.tight_layout()
plt.show()

技术图片
技术图片
技术图片





以上是关于python的skimage库 图像色彩空间转换 RGB到灰度;RGB到HSV的主要内容,如果未能解决你的问题,请参考以下文章

python库skimage 将针对灰度图像的滤波器用于RGB图像 逐通道滤波;转换为HSV图像滤波

OpenCV图像处理应用(面向Python)之色彩空间转换类型

python opencv:色彩空间

色彩空间类型---OpenCV-Python开发指南(7)

『Python』skimage图像处理_旋转图像

python库skimage 绘制二值图像的凸壳(convex hull)