from skimage import io
from skimage import color
from matplotlib import pyplot as plt
import numpy as np
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)
Cy55 = io.imread('https://github.com/jeanpat/MFISH/blob/master/MFISH_TIFF/A01__XY/A0102XY5.tif?raw=true')
Cy5 = io.imread('https://github.com/jeanpat/MFISH/blob/master/MFISH_TIFF/A01__XY/A0102XYC.tif?raw=true')
SpGreen = io.imread('https://github.com/jeanpat/MFISH/blob/master/MFISH_TIFF/A01__XY/A0102XYG.tif?raw=true')
SpOrange = io.imread('https://github.com/jeanpat/MFISH/blob/master/MFISH_TIFF/A01__XY/A0102XYO.tif?raw=true')
TexRed = io.imread('https://github.com/jeanpat/MFISH/blob/master/MFISH_TIFF/A01__XY/A0102XYT.tif?raw=true')
Let's set the color of the different images: Cy55 to red (hue=0), Cy5 to yellow (hue=0.2) , SpGreen (hue=0.4), SpOrange to blue! (hue=0.6) and TexasRed to purple (hue=0.8) ! The aim is not to have realistic colors but to see well.
hue_rotations = [0, 0.2, 0.4, 0.6, 0.8]#np.linspace(0, 1, 6)
print hue_rotations
monochromes =[Cy55, Cy5, SpGreen, SpOrange, TexRed]
colors = [colorize(mono, hue) for mono, hue in zip(monochromes, hue_rotations)]
[0, 0.2, 0.4, 0.6, 0.8]
i = 1
plt.figure(num=None, figsize=(15, 5), facecolor='w', edgecolor='k')
for im in monochromes:
plt.subplot(1,len(monochromes),i,xticks=[],yticks=[])
plt.imshow(im)
i = i +1
plt.show()
i = 1
plt.figure(num=None, figsize=(15, 5), facecolor='w', edgecolor='k')
for im, hue in zip(colors, hue_rotations):
plt.subplot(1,len(colors),i,xticks=[],yticks=[])
plt.title('hue='+str(hue))
plt.imshow(im)
i = i +1
plt.show()
Since the max value in a channel of a rgb image is 255. At first sight, we should have to divide the pixel value by the number of spectral component that is five; but dividing but three is sufficient not avoid pixel saturation.
zero = np.zeros(Cy55.shape, dtype=np.uint8)
mfish = np.dstack([zero,zero,zero])
for component in colors:
mfish = mfish + component/3.0
#mfish = np.uint8(mfish)
print mfish.dtype, mfish.shape
float64 (517, 645, 3)
plt.figure(figsize=(10,10))
plt.subplot(111,xticks=[],yticks=[])
plt.imshow(mfish)
plt.title('dtype='+str(mfish.dtype)+' '+'min='+str(mfish.min())+' max='+str(mfish.max()))
plt.show()
Let's see the histogram of HSV
mHSV = color.rgb2hsv(mfish)
hue0 = mHSV[:,:,0].flatten()
sat0 = mHSV[:,:,1].flatten()
val0 = mHSV[:,:,2].flatten()
plt.figure(figsize=(10,3))
subplot(131)
plt.title('Hue')
plt.hist(hue0, bins=30,range=(0.0,1.0),histtype='stepfilled', color='r', label='Hue', log=True)
subplot(132)
plt.title('Saturation')
plt.hist(sat0, bins=30,range=(0.0,1.0),histtype='stepfilled', color='g', label='Saturation', log=True)
subplot(133)
plt.title('value')
plt.hist(val0, bins=30,range=(0.0,1.0),histtype='stepfilled', color='b', label='Value', log=True)
plt.show()
Since the image is quite dull, let's stretch the contrast
from skimage import exposure
display = exposure.rescale_intensity(mfish, in_range=(mfish.min()+0.05,mfish.max()-0.25), out_range=(0,255))
display = np.uint8(display)
plt.figure(figsize=(10,10))
subplot(111,xticks=[],yticks=[])
plt.imshow(display)
plt.title('dtype='+str(display.dtype)+' '+'min='+str(display.min())+' max='+str(display.max()))
plt.show()
postHSV = color.rgb2hsv(display/255.0)
hue1 = postHSV[:,:,0].flatten()
sat1 = postHSV[:,:,1].flatten()
val1 = postHSV[:,:,2].flatten()
plt.figure(figsize=(10,3))
subplot(131)
plt.title('Hue')
plt.hist(hue1, bins=30,range=(0.0,1.0),histtype='stepfilled', color='r', label='Hue', log=True)
subplot(132)
plt.title('Saturation')
plt.hist(sat1, bins=30,range=(0.0,1.0),histtype='stepfilled', color='g', label='Saturation', log=True)
subplot(133)
plt.title('value')
plt.hist(val1, bins=30,range=(0.0,1.0),histtype='stepfilled', color='b', label='Value', log=True)
plt.show()
import Image
import colorsys
rgb_to_hsv = np.vectorize(colorsys.rgb_to_hsv)
hsv_to_rgb = np.vectorize(colorsys.hsv_to_rgb)
def shift_hue(arr, hout):
r, g, b, a = np.rollaxis(arr, axis=-1)
h, s, v = rgb_to_hsv(r, g, b)
h = hout
r, g, b = hsv_to_rgb(h, s, v)
arr = np.dstack((r, g, b, a))
return arr
def colorize_2(image, hue):
"""
Colorize PIL image `original` with the given
`hue` (hue within 0-360); returns another PIL image.
"""
img = image.convert('RGBA')
arr = np.array(np.asarray(img).astype('float'))
new_img = Image.fromarray(shift_hue(arr, hue/360.).astype('uint8'), 'RGBA')
return new_img
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()