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