def trans(self, img1, img2, img3): rst = color.xyz2rgb(np.array((img1.T, img2.T, img3.T)).T / 200.0) * 255 return rst.astype(np.uint8)
def test_xyz2rgb_conversion(self): # only roundtrip test, we checked rgb2xyz above already assert_almost_equal(xyz2rgb(rgb2xyz(self.colbars_array)), self.colbars_array)
from sklearn.cluster import KMeans from skimage import data, io, filters, color import numpy as np import matplotlib.pyplot as plt import sys image = io.imread(str(sys.argv[1])) n_clusters = int(sys.argv[2]) image_xyz = color.rgb2xyz(image) image_xyz = np.reshape(image_xyz, (-1, 3)) kmeans = KMeans(n_clusters).fit(image_xyz) palettes = [] for cluster in kmeans.cluster_centers_: img = np.tile(cluster, (100, 100, 1)) img = color.xyz2rgb(img) palettes.append(img) palettes = np.hstack(palettes) f1 = plt.figure(figsize=(8, 6)) ax1 = f1.add_subplot(212, frameon=False) ax2 = f1.add_subplot(211, frameon=False) ax1.set_axis_off() ax2.set_axis_off() ax1.imshow(palettes) ax2.imshow(image) plt.show()
def test_xyz2rgb_dtype(self): img = rgb2xyz(self.colbars_array) img32 = img.astype('float32') assert xyz2rgb(img).dtype == img.dtype assert xyz2rgb(img32).dtype == img32.dtype
results = ms.cluster_centers_[results][:, :, :-2] imsave("images/chinese_color_rgb_meanshift_%d_%d_%d.png" % (k * 100, hs * 100, hr), results) print "XYZ" im = image.copy() desc = mem.cache(descriptors.extract_color_descriptors)(im, space="xyz") descs = desc.astype(float) ms = mem.cache(cluster.meanshift)(descs, k, hs=hs, hr=hr) labels = ms.labels_ num = np.sqrt(len(labels)) results = labels.reshape((num, num)).T results = ms.cluster_centers_[results][:, :, :-2] imsave("images/chinese_color_xyz_meanshift_%d_%d_%d.png" % (k * 100, hs * 100, hr), color.xyz2rgb(results)) print "HSV" im = image.copy() desc = mem.cache(descriptors.extract_color_descriptors)(im, space="hsv") descs = desc.astype(float) ms = mem.cache(cluster.meanshift)(descs, k, hs=hs, hr=hr) labels = ms.labels_ num = np.sqrt(len(labels)) results = labels.reshape((num, num)).T results = ms.cluster_centers_[results][:, :, :-2] imsave("images/chinese_color_hsv_meanshift_%d_%d_%d.png" % (k * 100, hs * 100, hr), color.hsv2rgb(results)) # # print 'GRAY'
def test_xyz_rgb_roundtrip(self): img_rgb = img_as_float(self.img_rgb) assert_array_almost_equal(xyz2rgb(rgb2xyz(img_rgb)), img_rgb)
def to_rgb_from_xyz(self): '''convert from xyz to rgb''' return Image(xyz2rgb(self)).convert_type(self.dtype)
desc = mem.cache(descriptors.extract_color_descriptors)( im, space='xyz') descs = desc.astype(float) ms = mem.cache(cluster.meanshift)(descs, k, hs=hs, hr=hr) labels = ms.labels_ num = np.sqrt(len(labels)) results = labels.reshape((num, num)).T results = ms.cluster_centers_[results][:, :, :-2] imsave('images/black_swan_color_xyz_meanshift_%d_%d_%d.png' % (k * 1000, hs * 100, hr), color.xyz2rgb(results)) print 'HSV' im = image.copy() desc = mem.cache(descriptors.extract_color_descriptors)( im, space='hsv') descs = desc.astype(float) ms = mem.cache(cluster.meanshift)(descs, k, hs=hs, hr=hr) labels = ms.labels_ num = np.sqrt(len(labels)) results = labels.reshape((num, num)).T results = ms.cluster_centers_[results][:, :, :-2] imsave('images/black_swan_color_hsv_meanshift_%d_%d_%d.png' % (
CIEDATA = CIEDATA[17:97] cmf = array([CIEDATA['CIE1931x'], CIEDATA['CIE1931y'], CIEDATA['CIE1931z']]) wl = CIEDATA['wavelength'] D65 = CIEDATA['D65'] D50 = CIEDATA['D50'] colhdrs = CIEDATA.dtype.names[15:] img = zeros((800, 1200, 3)) XYZn = trapz(cmf*D65.T, wl) XYZnD50 = trapz(cmf*D50.T, wl) a = 0.055 for i in range(4): for j in range(6): n = 6*i+j color = CIEDATA[colhdrs[n]] XYZ = trapz(cmf*(D65*color).T, wl)/XYZn[1] XYZ = transpose(expand_dims(matrix(XYZ), 3), (0, 2, 1)) rgb = xyz2rgb(XYZ) img[200*i:200*(i+1), 200*j:200*(j+1), :] = tile(rgb, (200, 200, 1)) imshow(img) show()
def lms2rgb(img, transform_matrix): """ Convert image from LMS to RGB """ return xyz2rgb(lms2xyz(img, transform_matrix))
def preprocess(img): choices = np.zeros(shape=[25]) #ABOUT TRAINING: slowly increase nums here nums = np.random.random_integers(0, 10) picked = np.random.random_integers(0, 24, nums) choices[picked] = 1 #print(picked) #Color reduction if (choices[0] == 1): scales = [ np.asscalar(np.random.random_integers(8, 200)) for x in range(3) ] multi_channel = np.random.choice(2) == 0 params = [multi_channel] + [s / 200.0 for s in scales] if multi_channel: img = np.round(img * scales[0]) / scales[0] else: for i in range(3): img[:, :, i] = np.round(img[:, :, i] * scales[i]) / scales[i] #JPEG Noise if (choices[1] == 1): quality = np.asscalar(np.random.random_integers(55, 95)) params = [quality / 100.0] pil_image = Image.fromarray((img * 255.0).astype(np.uint8)) f = BytesIO() pil_image.save(f, format='jpeg', quality=quality) jpeg_image = np.asarray(Image.open(f), ).astype(np.float32) / 255.0 img = jpeg_image #Swirl if (choices[2] == 1): strength = (2.0 - 0.01) * np.random.random(1)[0] + 0.01 c_x = np.random.random_integers(1, 256) c_y = np.random.random_integers(1, 256) radius = np.random.random_integers(10, 200) params = [strength / 2.0, c_x / 256.0, c_y / 256.0, radius / 200.0] img = transform.swirl(img, rotation=0, strength=strength, radius=radius, center=(c_x, c_y)) #Noise Injection if (choices[3] == 1): params = [] options = ['gaussian', 'poisson', 'salt', 'pepper', 's&p', 'speckle'] noise_type = np.random.choice(options, 1)[0] params.append(options.index(noise_type) / 6.0) per_channel = np.random.choice(2) == 0 params.append(per_channel) if per_channel: for i in range(3): img[:, :, i] = skimage.util.random_noise(img[:, :, i], mode=noise_type) else: img = skimage.util.random_noise(img, mode=noise_type) # FFT Perturbation if (choices[4] == 1): r, c, _ = img.shape point_factor = (1.02 - 0.98) * np.random.random((r, c)) + 0.98 randomized_mask = [np.random.choice(2) == 0 for x in range(3)] keep_fraction = [(0.95 - 0.0) * np.random.random(1)[0] + 0.0 for x in range(3)] params = randomized_mask + keep_fraction for i in range(3): im_fft = fftpack.fft2(img[:, :, i]) r, c = im_fft.shape if randomized_mask[i]: mask = np.ones(im_fft.shape[:2]) > 0 im_fft[int(r * keep_fraction[i]):int(r * (1 - keep_fraction[i]))] = 0 im_fft[:, int(c * keep_fraction[i]):int(c * (1 - keep_fraction[i]))] = 0 mask = ~mask mask = mask * ~(np.random.uniform(size=im_fft.shape[:2]) < keep_fraction[i]) mask = ~mask im_fft = np.multiply(im_fft, mask) else: im_fft[int(r * keep_fraction[i]):int(r * (1 - keep_fraction[i]))] = 0 im_fft[:, int(c * keep_fraction[i]):int(c * (1 - keep_fraction[i]))] = 0 im_fft = np.multiply(im_fft, point_factor) im_new = fftpack.ifft2(im_fft).real im_new = np.clip(im_new, 0, 1) img[:, :, i] = im_new #Zooms if (choices[5] == 1): h, w, _ = img.shape i_s = np.random.random_integers(10, 50) i_e = np.random.random_integers(10, 50) j_s = np.random.random_integers(10, 50) j_e = np.random.random_integers(10, 50) params = [i_s / 50, i_e / 50, j_s / 50, j_e / 50] i_e = h - i_e j_e = w - j_e # Crop the image... img = img[i_s:i_e, j_s:j_e, :] # ...now scale it back up img = skimage.transform.resize(img, (h, w, 3)) if (choices[11] == 1): #DO NOTHING HERE BECAUSE SEAM_CARVE REMOVED """ h, w, _ = img.shape both_axis = np.random.choice(2) == 0 toRemove_1 = np.random.random_integers(10, 50) toRemove_2 = np.random.random_integers(10, 50) params = [both_axis, toRemove_1 / 50, toRemove_2 / 50] if both_axis: # First remove from vertical eimg = skimage.filters.sobel(skimage.color.rgb2gray(img) ) img = transform.seam_carve(img, eimg, 'vertical', toRemove_1) # Now from horizontal eimg = skimage.filters.sobel(skimage.color.rgb2gray(img) ) img = transform.seam_carve(img, eimg,'horizontal', toRemove_2) else: eimg = skimage.filters.sobel(skimage.color.rgb2gray(img) ) direction = 'horizontal' if toRemove_2 < 30: direction = 'vertical' img = transform.seam_carve(img, eimg,direction, toRemove_1) # Now scale it back up img = skimage.transform.resize(img, (h, w, 3)) """ #Color space processes if (choices[6] == 1): img = color.rgb2hsv(img) params = [] # Hue img[:, :, 0] += randUnifC(-0.05, 0.05, params=params) # Saturation img[:, :, 1] += randUnifC(-0.25, 0.25, params=params) # Value img[:, :, 2] += randUnifC(-0.25, 0.25, params=params) img = np.clip(img, 0, 1.0) img = color.hsv2rgb(img) img = (img * 2.0) - 1.0 img = np.clip(img, -1.0, 1.0) if (choices[8] == 1): img = (img + 1.0) * 0.5 img = color.rgb2xyz(img) params = [] # X img[:, :, 0] += randUnifC(-0.05, 0.05, params=params) # Y img[:, :, 1] += randUnifC(-0.05, 0.05, params=params) # Z img[:, :, 2] += randUnifC(-0.05, 0.05, params=params) img = np.clip(img, 0, 1.0) img = color.xyz2rgb(img) img = (img * 2.0) - 1.0 img = np.clip(img, -1.0, 1.0) if (choices[9] == 1): img = (img + 1.0) * 0.5 img = color.rgb2lab(img) params = [] # L img[:, :, 0] += randUnifC(-5.0, 5.0, params=params) # a img[:, :, 1] += randUnifC(-2.0, 2.0, params=params) # b img[:, :, 2] += randUnifC(-2.0, 2.0, params=params) img[:, :, 0] = np.clip(img[:, :, 0], 0, 100.0) img = color.lab2rgb(img) img = (img * 2.0) - 1.0 img = np.clip(img, -1.0, 1.0) if (choices[10] == 1): img = (img + 1.0) * 0.5 img = color.rgb2yuv(img) params = [] # Y img[:, :, 0] += randUnifC(-0.05, 0.05, params=params) # U img[:, :, 1] += randUnifC(-0.02, 0.02, params=params) # V img[:, :, 2] += randUnifC(-0.02, 0.02, params=params) # U & V channels can have negative values; clip only Y img[:, :, 0] = np.clip(img[:, :, 0], 0, 1.0) img = color.yuv2rgb(img) img = (img * 2.0) - 1.0 img = np.clip(img, -1.0, 1.0) if (choices[7] == 1): nbins = np.random.random_integers(40, 256) params = [nbins / 256.0] for i in range(3): img[:, :, i] = skimage.exposure.equalize_hist(img[:, :, i], nbins=nbins) img = (img * 2.0) - 1.0 img = np.clip(img, -1.0, 1.0) #if(choices[12]==1): if (choices[13] == 1): per_channel = np.random.choice(2) == 0 params = [per_channel] low_precentile = [ randUnifC(0.01, 0.04, params=params) for x in range(3) ] hi_precentile = [ randUnifC(0.96, 0.99, params=params) for x in range(3) ] if per_channel: for i in range(3): p2, p98 = np.percentile( img[:, :, i], (low_precentile[i] * 100, hi_precentile[i] * 100)) img[:, :, i] = skimage.exposure.rescale_intensity(img[:, :, i], in_range=(p2, p98)) else: p2, p98 = np.percentile( img, (low_precentile[0] * 100, hi_precentile[0] * 100)) img = skimage.exposure.rescale_intensity(img, in_range=(p2, p98)) img = (img * 2.0) - 1.0 img = np.clip(img, -1.0, 1.0) if (choices[14] == 1): ratios = np.random.rand(3) ratios /= ratios.sum() params = [x for x in ratios] img_g = img[:, :, 0] * ratios[0] + img[:, :, 1] * ratios[ 1] + img[:, :, 2] * ratios[2] for i in range(3): img[:, :, i] = img_g img = np.clip(img, -1.0, 1.0) if (choices[15] == 1): ratios = np.random.rand(3) ratios /= ratios.sum() prop_ratios = np.random.rand(3) params = [x for x in ratios] + [x for x in prop_ratios] img_g = img[:, :, 0] * ratios[0] + img[:, :, 1] * ratios[ 1] + img[:, :, 2] * ratios[2] for i in range(3): p = max(prop_ratios[i], 0.2) img[:, :, i] = img[:, :, i] * p + img_g * (1.0 - p) img = np.clip(img, -1.0, 1.0) if (choices[16] == 1): params = [] channels = [0, 1, 2] remove_channel = np.random.choice(3) channels.remove(remove_channel) params.append(remove_channel) ratios = np.random.rand(2) ratios /= ratios.sum() params.append(ratios[0]) img_g = img[:, :, channels[0]] * ratios[0] + img[:, :, channels[1]] * ratios[1] for i in channels: img[:, :, i] = img_g img = np.clip(img, -1.0, 1.0) if (choices[17] == 1): params = [] channels = [0, 1, 2] to_alter = np.random.choice(3) channels.remove(to_alter) params.append(to_alter) ratios = np.random.rand(2) ratios /= ratios.sum() params.append(ratios[0]) img_g = img[:, :, channels[0]] * ratios[0] + img[:, :, channels[1]] * ratios[1] # Lets mix it back in with the original channel p = (0.9 - 0.1) * np.random.random(1)[0] + 0.1 params.append(p) img[:, :, to_alter] = img_g * p + img[:, :, to_alter] * (1.0 - p) img = np.clip(img, -1.0, 1.0) if (choices[18] == 1): if randUnifC(0, 1) > 0.5: sigma = [randUnifC(0.1, 3)] * 3 else: sigma = [randUnifC(0.1, 3), randUnifC(0.1, 3), randUnifC(0.1, 3)] img[:, :, 0] = skimage.filters.gaussian(img[:, :, 0], sigma=sigma[0]) img[:, :, 1] = skimage.filters.gaussian(img[:, :, 1], sigma=sigma[1]) img[:, :, 2] = skimage.filters.gaussian(img[:, :, 2], sigma=sigma[2]) img = np.clip(img, -1.0, 1.0) if (choices[19] == 1): if randUnifC(0, 1) > 0.5: radius = [randUnifI(2, 5)] * 3 else: radius = [randUnifI(2, 5), randUnifI(2, 5), randUnifI(2, 5)] # median blur - different sigma for each channel for i in range(3): mask = skimage.morphology.disk(radius[i]) img[:, :, i] = skimage.filters.rank.median( np.clip(img[:, :, i], -1.0, 1.0), mask) / 255.0 img = np.clip(img, -1.0, 1.0) if (choices[20] == 1): if randUnifC(0, 1) > 0.5: radius = [randUnifI(2, 3)] * 3 else: radius = [randUnifI(2, 3), randUnifI(2, 3), randUnifI(2, 3)] # mean blur w/ different sigma for each channel for i in range(3): mask = skimage.morphology.disk(radius[i]) img[:, :, i] = skimage.filters.rank.mean( np.clip(img[:, :, i], -1.0, 1.0), mask) / 255.0 img = np.clip(img, -1.0, 1.0) if (choices[21] == 1): params = [] radius = [] ss = [] for i in range(3): radius.append(randUnifI(2, 20, params=params)) ss.append(randUnifI(5, 20, params=params)) ss.append(randUnifI(5, 20, params=params)) for i in range(3): mask = skimage.morphology.disk(radius[i]) img[:, :, i] = skimage.filters.rank.mean_bilateral( np.clip(img[:, :, i], -1.0, 1.0), mask, s0=ss[i], s1=ss[3 + i]) / 255.0 img = np.clip(img, -1.0, 1.0) if (choices[22] == 1): params = [] weight = (0.25 - 0.05) * np.random.random(1)[0] + 0.05 params.append(weight) multi_channel = np.random.choice(2) == 0 params.append(multi_channel) img = skimage.restoration.denoise_tv_chambolle( img, weight=weight, multichannel=multi_channel) img = np.clip(img, -1.0, 1.0) if (choices[23] == 1): wavelets = ['db1', 'db2', 'haar', 'sym9'] convert2ycbcr = np.random.choice(2) == 0 wavelet = np.random.choice(wavelets) mode_ = np.random.choice(["soft", "hard"]) denoise_kwargs = dict(multichannel=True, convert2ycbcr=convert2ycbcr, wavelet=wavelet, mode=mode_) max_shifts = np.random.choice([0, 1]) params = [ convert2ycbcr, wavelets.index(wavelet) / float(len(wavelets)), max_shifts / 5.0, (mode_ == "soft") ] img = skimage.restoration.cycle_spin( img, func=skimage.restoration.denoise_wavelet, max_shifts=max_shifts, func_kw=denoise_kwargs) img = np.clip(img, -1.0, 1.0) if (choices[24] == 1): wavelets = ['db1', 'db2', 'haar', 'sym9'] convert2ycbcr = np.random.choice(2) == 0 wavelet = np.random.choice(wavelets) mode_ = np.random.choice(["soft", "hard"]) denoise_kwargs = dict(multichannel=True, convert2ycbcr=convert2ycbcr, wavelet=wavelet, mode=mode_) max_shifts = np.random.choice([0, 1]) params = [ convert2ycbcr, wavelets.index(wavelet) / float(len(wavelets)), max_shifts / 5.0, (mode_ == "soft") ] img = skimage.restoration.cycle_spin( img, func=skimage.restoration.denoise_wavelet, max_shifts=max_shifts, func_kw=denoise_kwargs) img = np.clip(img, -1.0, 1.0) return img
def xyz2rgb(self, imageArray): return color.xyz2rgb(imageArray)
import numpy as np import mahotas import mahotas.demos from skimage import data from skimage.color import rgb2xyz, xyz2rgb from PIL import Image xyzImage = np.load("xyzImage_new.npy") img2 = Image.fromarray(xyzImage) img2.show() img2.save("XYZ_Image.tif") img_rgb = xyz2rgb(xyzImage) img_rgb *= 255 img_rgb = img_rgb.astype('uint8') print(type(img_rgb)) img = Image.fromarray(img_rgb) img.show() img.save("RGB_Image.tif")
def read_tiff_16bit_img_into_sRGB(tiff_fn, exposure=0): xyz = read_tiff_16bit_img_into_XYZ(tiff_fn, exposure) sRGb = color.xyz2rgb(xyz) return sRGb
def xyz_to_rgb(xyz_array): """ convert xyz to rgb color space, and normalize all pixels and channels together. """ rgb_array = color.xyz2rgb(xyz_array) return rgb_array / rgb_array.max()
pred_coord[:, :, 0] = imgObj_x.reshape(x, y) pred_coord[:, :, 1] = imgObj_y.reshape(x, y) pred_coord[:, :, 2] = imgObj_z.reshape(x, y) for i in range(pred_coord.shape[2]): imgObj_xyz = pred_coord[:, :, i] imgObj_xyz = imgObj_xyz.reshape(-1, 1) # imgObj_xyz_scale = preprocessing.scale(imgObj_xyz) # imgObj_xyz_normalized = preprocessing.normalize(imgObj_xyz, norm='l2') imgObj_xyz_norm = mm.fit_transform(imgObj_xyz) predObj_norm[:, :, i] = imgObj_xyz_norm.reshape(x, y) # predObj_normalized[:, :, i] = imgObj_xyz_normalized.reshape(x, y) # origin_data = mm.inverse_transform(imgObj_xyz_norm) imgRGB = xyz2rgb(predObj_norm) # show RGB array as a picture plt.imshow(imgRGB) plt.savefig('./Figure/523_predict.jpg') plt.show() # imgObj_z_norm = imgObj_z_norm.astype(np.uint8) # imgObj_z = imgObj_z.astype(np.uint8) # imgObj_z_normalized = imgObj_z_normalized.astype(np.uint8) # img = cv2.applyColorMap(imgObj_z_normalized, colormap=4) # cv2.imshow('a', img) # cv2.waitKey(0) # pred_coordz = predObj_norm[:, :, 2] # imgRGB = cv2.cvtColor(pred_coord, cv2.COLOR_XYZ2RGB) # plt.imshow(imgRGB)
def test_xyz2rgb_conversion(self): assert_almost_equal(xyz2rgb(rgb2xyz(self.colbars_array)), self.colbars_array)
def lms2rgb(lmsimage): """""" return xyz2rgb( sp.tensordot(lmsimage, sp.linalg.inv(CIECAM02_CAT), axes=(-1, 1)))
def xyz2hsv(XYZimage): """""" return rgb2hsv(xyz2rgb(XYZimage))
def trans(self, img1, img2, img3): rst = color.xyz2rgb(np.array((img1.T, img2.T, img3.T)).T / 200.0) * 255 #print('============', rst.min(), rst.max()) return rst.astype(np.uint8)