gpt4 book ai didi

python - skimage 将 RGB 转换为 HSV 时如何获得正确的颜色。了解色调

转载 作者:行者123 更新时间:2023-12-01 01:01:27 26 4
gpt4 key购买 nike

我尝试使用 skimage 将 RGB 转换为 HSV,但得到了我不期望的行为。这是一些我希望只产生蓝色的示例代码。这很重要,因为(稍后)我想拍摄真实图像并通过引用色调来确定整个图像中每种颜色的存在量。

import numpy as np
import skimage as ski
import matplotlib.pyplot as plt

#define my own color in RGB, should be B
tested = np.ones(shape=(100,100,3))*200
tested[:,:,0] =0
tested[:,:,1] =0

hsv_test_img_arr=ski.color.rgb2hsv(tested)

hue_img = hsv_test_img_arr[:, :, 0]
sat_img = hsv_test_img_arr[:, :, 1]
value_img = hsv_test_img_arr[:, :, 2]

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

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')

ax3.imshow(sat_img)
ax3.set_title('sat channel')
ax3.axis('off')


output

最佳答案

您忘记正确标准化数据。所有 channel 中的值范围都在 0 到 1 之间。因此您需要将此信息提供给 imshow,

imshow(..., vmin=0, vmax=1)

完整代码:

import numpy as np
import skimage as ski
import matplotlib.pyplot as plt

#define my own color in RGB, should be B
tested = np.ones(shape=(100,100,3))*200
tested[:,:,0] =0
tested[:,:,1] =0

hsv_test_img_arr=ski.color.rgb2hsv(tested)

hue_img = hsv_test_img_arr[:, :, 0]
sat_img = hsv_test_img_arr[:, :, 1]
value_img = hsv_test_img_arr[:, :, 2]

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

im1 = ax1.imshow(hue_img, cmap='hsv', vmin=0, vmax=1)
ax1.set_title('hue channel')
ax1.axis('off')
fig.colorbar(im1, ax=ax1)

im2 = ax2.imshow(value_img, cmap="gray", vmin=0, vmax=1)
ax2.set_title('value channel')
ax2.axis('off')
fig.colorbar(im2, ax=ax2)

im3 = ax3.imshow(sat_img, cmap="gray", vmin=0, vmax=1)
ax3.set_title('sat channel')
ax3.axis('off')
fig.colorbar(im3, ax=ax3)

plt.show()

enter image description here

拍摄真实图像会使这更有用。

import skimage as ski
import matplotlib.pyplot as plt

img = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/World%2C_administrative_divisions_-_de_-_colored_%28all_countries%29.svg/640px-World%2C_administrative_divisions_-_de_-_colored_%28all_countries%29.svg.png"
tested = plt.imread(img)[:,:,:3]

hsv_test_img_arr=ski.color.rgb2hsv(tested)

hue_img = hsv_test_img_arr[:, :, 0]
sat_img = hsv_test_img_arr[:, :, 1]
value_img = hsv_test_img_arr[:, :, 2]

fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(ncols=2, nrows=2, figsize=(8, 6))

im0 = ax0.imshow(tested)
ax0.set_title('original')
ax0.axis('off')

im1 = ax1.imshow(hue_img, cmap='hsv', vmin=0, vmax=1)
ax1.set_title('hue channel')
ax1.axis('off')
fig.colorbar(im1, ax=ax1)

im2 = ax2.imshow(value_img, cmap="gray", vmin=0, vmax=1)
ax2.set_title('value channel')
ax2.axis('off')
fig.colorbar(im2, ax=ax2)

im3 = ax3.imshow(sat_img, cmap="gray", vmin=0, vmax=1)
ax3.set_title('sat channel')
ax3.axis('off')
fig.colorbar(im3, ax=ax3)

plt.show()

enter image description here

关于python - skimage 将 RGB 转换为 HSV 时如何获得正确的颜色。了解色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55767292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com