基于HSV空间色彩的图像分割方法(代码实现)


1. 分割图片示例:

在这里插入图片描述

2. 代码实现:

from skimage.io import imread, imshow
from skimage import img_as_ubyte  # For converting the float image to uint8
from skimage import io, color
from skimage.color import rgb2hsv
from matplotlib import pyplot as plt
import numpy as np

image_path = "img.jpg"
rgb_image = io.imread(image_path)

img_hsv = rgb2hsv(rgb_image)
# -------------转换成HSV色彩空间----------------
fig, ax = plt.subplots(1, 3, figsize=(12,4))
ax[0].imshow(img_hsv[:,:,0], cmap='gray')
ax[0].set_title('Hue')
ax[1].imshow(img_hsv[:,:,1], cmap='gray')
ax[1].set_title('Saturation')
ax[2].imshow(img_hsv[:,:,2], cmap='gray')
ax[2].set_title('Value')
plt.show()
# --------------获得每个HSV通道的强度值---------------
fig2, ax2 = plt.subplots(1, 3, figsize=(15, 5))
ax2[0].imshow(img_hsv[:,:,0],cmap='hsv')
ax2[0].set_title('hue')
ax2[1].imshow(img_hsv[:,:,1],cmap='hsv')
ax2[1].set_title('transparency')
ax2[2].imshow(img_hsv[:,:,2],cmap='hsv')
ax2[2].set_title('value')
fig2.colorbar(imshow(img_hsv[:,:,0],cmap='hsv'))
fig2.tight_layout()
plt.show()
# -------------挑选阈值----------------
# refer to hue channel (in the colorbar)
lower_mask = img_hsv[:, :, 0] > 0.08
# refer to hue channel (in the colorbar)
upper_mask = img_hsv[:, :, 0] < 0.5
# refer to transparency channel (in the colorbar)
saturation_mask = img_hsv[:, :, 1] > 0.7

mask = upper_mask * lower_mask * saturation_mask
red = rgb_image[:, :, 0] * mask
green = rgb_image[:, :, 1] * mask
blue = rgb_image[:, :, 2] * mask
img_masked = np.dstack((red, green, blue))

imshow(img_masked)
plt.show()

# Save the masked image as PNG
# io.imsave("img_masked.png", img_as_ubyte(img_masked))

3. 运行结果:

(1)
在这里插入图片描述

(2)

在这里插入图片描述

(3)

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-05 16:40:06       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-05 16:40:06       97 阅读
  3. 在Django里面运行非项目文件

    2023-12-05 16:40:06       78 阅读
  4. Python语言-面向对象

    2023-12-05 16:40:06       88 阅读

热门阅读

  1. 简单介绍Docker

    2023-12-05 16:40:06       46 阅读
  2. 【C++】sizeof()、strlen()、length()\以及size()用法区别

    2023-12-05 16:40:06       55 阅读
  3. Flyway——Oracle创建前缀索引

    2023-12-05 16:40:06       62 阅读
  4. oracle 19c rac 安装手册

    2023-12-05 16:40:06       45 阅读
  5. mysql学习记录

    2023-12-05 16:40:06       42 阅读
  6. Oracle之ORA-29275: 部分多字节字符

    2023-12-05 16:40:06       62 阅读
  7. Redis下载安装教程(详细步骤)

    2023-12-05 16:40:06       64 阅读
  8. Python实现pdf文件转word文件

    2023-12-05 16:40:06       56 阅读
  9. MySQL数据库命令详解(二)

    2023-12-05 16:40:06       58 阅读
  10. 国防科技大博士招生入学考试【50+论文主观题】

    2023-12-05 16:40:06       36 阅读