00001
00002
00003
00004 from __future__ import division
00005 import math
00006
00007 import cv2
00008 import numpy as np
00009 import PIL
00010
00011
00012 def centerize(src, dst_shape):
00013 """Centerize image for specified image size
00014
00015 @param src: image to centerize
00016 @param dst_shape: image shape (height, width) or (height, width, channel)
00017 """
00018 if src.shape[:2] == dst_shape[:2]:
00019 return src
00020 centerized = np.zeros(dst_shape, dtype=src.dtype)
00021 pad_vertical, pad_horizontal = 0, 0
00022 h, w = src.shape[:2]
00023 dst_h, dst_w = dst_shape[:2]
00024 if h < dst_h:
00025 pad_vertical = (dst_h - h) // 2
00026 if w < dst_w:
00027 pad_horizontal = (dst_w - w) // 2
00028 centerized[pad_vertical:pad_vertical+h,
00029 pad_horizontal:pad_horizontal+w] = src
00030 return centerized
00031
00032 def _tile_images(imgs, tile_shape, concatenated_image):
00033 """Concatenate images whose sizes are same.
00034
00035 @param imgs: image list which should be concatenated
00036 @param tile_shape: shape for which images should be concatenated
00037 @param concatenated_image: returned image. if it is None, new image will be created.
00038 """
00039 x_num, y_num = tile_shape
00040 one_width = imgs[0].shape[1]
00041 one_height = imgs[0].shape[0]
00042 if concatenated_image is None:
00043 concatenated_image = np.zeros((one_height * y_num, one_width * x_num, 3),
00044 dtype=np.uint8)
00045 for y in range(y_num):
00046 for x in range(x_num):
00047 i = x + y * x_num
00048 if i >= len(imgs):
00049 pass
00050 else:
00051 concatenated_image[y*one_height:(y+1)*one_height,x*one_width:(x+1)*one_width,] = imgs[i]
00052 return concatenated_image
00053
00054
00055 def get_tile_image(imgs, tile_shape=None, result_img=None):
00056 """Concatenate images whose sizes are different.
00057
00058 @param imgs: image list which should be concatenated
00059 @param tile_shape: shape for which images should be concatenated
00060 @param result_img: numpy array to put result image
00061 """
00062 def get_tile_shape(img_num):
00063 x_num = 0
00064 y_num = int(math.sqrt(img_num))
00065 while x_num * y_num < img_num:
00066 x_num += 1
00067 return x_num, y_num
00068
00069 if tile_shape is None:
00070 tile_shape = get_tile_shape(len(imgs))
00071
00072
00073 max_height, max_width = np.inf, np.inf
00074 for img in imgs:
00075 max_height = min([max_height, img.shape[0]])
00076 max_width = min([max_width, img.shape[1]])
00077
00078
00079 for i, img in enumerate(imgs):
00080 h, w = img.shape[:2]
00081 h_scale, w_scale = max_height / h, max_width / w
00082 scale = min([h_scale, w_scale])
00083 h, w = int(scale * h), int(scale * w)
00084 img = cv2.resize(img, (w, h))
00085 img = centerize(img, (max_height, max_width, 3))
00086 imgs[i] = img
00087 return _tile_images(imgs, tile_shape, result_img)
00088
00089
00090 def colorize_cluster_indices(image, cluster_indices, alpha=0.3, image_alpha=1):
00091 from skimage.color import rgb2gray
00092 from skimage.color import gray2rgb
00093 from skimage.util import img_as_float
00094 from skimage.color.colorlabel import DEFAULT_COLORS
00095 from skimage.color.colorlabel import color_dict
00096 image = img_as_float(rgb2gray(image))
00097 image = gray2rgb(image) * image_alpha + (1 - image_alpha)
00098 height, width = image.shape[:2]
00099
00100 n_colors = len(DEFAULT_COLORS)
00101 indices_to_color = np.zeros((height * width, 3))
00102 for i, indices in enumerate(cluster_indices):
00103 color = color_dict[DEFAULT_COLORS[i % n_colors]]
00104 indices_to_color[indices] = color
00105 indices_to_color = indices_to_color.reshape((height, width, 3))
00106 result = indices_to_color * alpha + image * (1 - alpha)
00107 result = (result * 255).astype(np.uint8)
00108 return result