问题描述
目标是将纯红色图像转换为色轮的任何色调。
The aim is to convert a pure red image into any hue of the color wheel.
- 首先将单色图像转换为彩色图像。 RGB红色图像,例如:
- 然后转换为HSV
- 通过添加应该与车轮颜色匹配的
角度值来修改色相分量 - 然后绘制hsv图像返回转换为rgb颜色空间。
- A monochrome image is first converted into a RGB red image, ex:
- then converted into HSV
- The hue component is modified by adding an angle value supposed tomatch the wheel color
- then the hsv image is back converted into the rgb color space.
问题是只能获得绿色或蓝色图像(n ):
The problem is that only green or blue image can be obtained (no yellow for an angle~30° for example):
在某些ipython单元中执行的代码依赖于scikit- image 0.10dev:
The code performed in some ipython cells relies on 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()
推荐答案
我在邮件列表中回答了这个问题,但是我将解决方案复制到此处,以便更轻松地查找(格式为pre
I answered this on the mailing list, but I'll copy the solution here so that it's easier to find (and the formatting is prettier).
基本上,色调的表示方式(0--1而不是0--180),uint8与float数据类型以及可能是如何将灰度图像转换为RGB的问题。用法的快速示例如下:
Basically, there are some differences in how hue is represented (0--1 instead of 0--180), uint8 vs float data-types, and maybe some issues with how the grayscale image is converted to RGB. A quick example of usage might look like:
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()
给出:
这篇关于使用scikit-image在HSV中进行颜色旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!