繁体   English   中英

使用scikit-image在HSV中进行颜色旋转

[英]Color rotation in HSV using scikit-image

目的是将纯红色图像转换为色轮的任何色调。

  • 单色图像首先转换为RGB红色图像,例如: 原始的单色图像
  • 然后转换成HSV
  • 通过添加应该与轮颜色匹配的角度值来修改色调分量
  • 然后将hsv图像反转换为rgb颜色空间。

问题是只能获得绿色或蓝色图像( 例如黄色,角度约为30° ): 色相旋转

在一些ipython单元格中执行的代码依赖于scikit-image 0.10dev:

from skimage import io
from skimage import color
from scipy import ndimage as nd
import numpy as np
from matplotlib import pyplot as plt
import os
cy55 = io.imread('/home/jeanpat/MFISH/PSI/P07/01/Cy5/P070109C.tif')

zero = np.zeros(cy55.shape,dtype=np.uint8)
rgb0 = np.dstack([cy55, zero,zero])

hue_rotations = [18, 36,72,90,108]
images = {}
images[0] = rgb0
hsv0 = color.rgb2hsv(rgb0)
print hsv0[:,:,0].dtype
for hue in hue_rotations:
    hsv = np.copy(hsv0)
    hsv[:,:,0] = hsv[:,:,0]+ hue
    rgb = color.hsv2rgb(hsv)
    images[hue] = rgb
i = 1
plt.figure(num=None, figsize=(15, 5),  facecolor='w', edgecolor='k')
for hue in np.sort(images.keys()):
    plt.subplot(1,6,i,xticks=[],yticks=[])
    plt.title('hue='+str(hue))
    plt.imshow(images[hue])
    i = i +1
plt.show()

我在邮件列表上回答了这个问题,但我会在这里复制解决方案,以便更容易找到(并且格式更漂亮)。

基本上,在如何表示色调(0--1而不是0-180),uint8与浮点数据类型之间存在一些差异,并且可能存在灰度图像如何转换为RGB的一些问题。 使用的简单示例可能如下所示:

import numpy as np
import matplotlib.pyplot as plt
from skimage import color
from skimage import data


def colorize(image, hue):
    """Return image tinted by the given hue based on a grayscale image."""
    hsv = color.rgb2hsv(color.gray2rgb(image))
    hsv[:, :, 0] = hue
    hsv[:, :, 1] = 1  # Turn up the saturation; we want the color to pop!
    return color.hsv2rgb(hsv)


image = data.camera()[::2, ::2]

hue_rotations = np.linspace(0, 1, 6)  # 0--1 is equivalent to 0--180
colorful_images = [colorize(image, hue) for hue in hue_rotations]

fig, axes = plt.subplots(nrows=2, ncols=3)

for ax, array in zip(axes.flat, colorful_images):
    ax.imshow(array, vmin=0, vmax=1)
    ax.set_axis_off()

plt.show()

这使:

彩色灰度图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM