visualize.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 from __future__ import division
5 import math
6 
7 import cv2
8 import numpy as np
9 import PIL
10 
11 
12 def centerize(src, dst_shape, margin_color=None):
13  """Centerize image for specified image size
14 
15  @param src: image to centerize
16  @param dst_shape: image shape (height, width) or (height, width, channel)
17  """
18  if src.shape[:2] == dst_shape[:2]:
19  return src
20  centerized = np.zeros(dst_shape, dtype=src.dtype)
21  if margin_color is not None:
22  centerized[:, :] = margin_color
23  pad_vertical, pad_horizontal = 0, 0
24  h, w = src.shape[:2]
25  dst_h, dst_w = dst_shape[:2]
26  if h < dst_h:
27  pad_vertical = (dst_h - h) // 2
28  if w < dst_w:
29  pad_horizontal = (dst_w - w) // 2
30  centerized[pad_vertical:pad_vertical+h,
31  pad_horizontal:pad_horizontal+w] = src
32  return centerized
33 
34 def _tile_images(imgs, tile_shape, concatenated_image, margin_color=None):
35  """Concatenate images whose sizes are same.
36 
37  @param imgs: image list which should be concatenated
38  @param tile_shape: shape for which images should be concatenated
39  @param concatenated_image: returned image. if it is None, new image will be created.
40  """
41  x_num, y_num = tile_shape
42  one_width = imgs[0].shape[1]
43  one_height = imgs[0].shape[0]
44  if concatenated_image is None:
45  if imgs[0].ndim == 2:
46  concatenated_image = np.zeros((one_height * y_num, one_width * x_num),
47  dtype=np.uint8)
48  elif imgs[0].ndim == 3:
49  concatenated_image = np.zeros((one_height * y_num, one_width * x_num, 3),
50  dtype=np.uint8)
51  else:
52  raise NotImplementedError
53  if margin_color is not None:
54  concatenated_image[:, :] = margin_color
55  for y in range(y_num):
56  for x in range(x_num):
57  i = x + y * x_num
58  if i >= len(imgs):
59  pass
60  else:
61  concatenated_image[y*one_height:(y+1)*one_height,x*one_width:(x+1)*one_width,] = imgs[i]
62  return concatenated_image
63 
64 
65 def get_tile_image(imgs, tile_shape=None, result_img=None, margin_color=None,
66  min_size=50):
67  """Concatenate images whose sizes are different.
68 
69  @param imgs: image list which should be concatenated
70  @param tile_shape: shape for which images should be concatenated
71  @param result_img: numpy array to put result image
72  """
73  def get_tile_shape(img_num):
74  x_num = 0
75  y_num = int(math.sqrt(img_num))
76  while x_num * y_num < img_num:
77  x_num += 1
78  return x_num, y_num
79 
80  imgs = [img for img in imgs if img is not None]
81  if tile_shape is None:
82  tile_shape = get_tile_shape(len(imgs))
83 
84  # get max tile size to which each image should be resized
85  max_height, max_width = np.inf, np.inf
86  for img in imgs:
87  max_height = min([max_height, img.shape[0]])
88  max_width = min([max_width, img.shape[1]])
89  max_height = max(max_height, min_size)
90  max_width = max(max_width, min_size)
91 
92  # resize and concatenate images
93  for i, img in enumerate(imgs):
94  h, w = img.shape[:2]
95  h_scale, w_scale = max_height / h, max_width / w
96  scale = min([h_scale, w_scale])
97  h, w = int(scale * h), int(scale * w)
98  img = cv2.resize(img, (w, h))
99  if img.ndim == 2:
100  img = centerize(img, (max_height, max_width),
101  margin_color=margin_color)
102  elif img.ndim == 3:
103  img = centerize(img, (max_height, max_width, 3),
104  margin_color=margin_color)
105  else:
106  raise NotImplementedError
107  imgs[i] = img
108  return _tile_images(imgs, tile_shape, result_img,
109  margin_color=margin_color)
110 
111 
112 def colorize_cluster_indices(image, cluster_indices, alpha=0.3, image_alpha=1):
113  from skimage.color import rgb2gray
114  from skimage.color import gray2rgb
115  from skimage.util import img_as_float
116  from skimage.color.colorlabel import DEFAULT_COLORS
117  from skimage.color.colorlabel import color_dict
118  image = img_as_float(rgb2gray(image))
119  image = gray2rgb(image) * image_alpha + (1 - image_alpha)
120  height, width = image.shape[:2]
121 
122  n_colors = len(DEFAULT_COLORS)
123  indices_to_color = np.zeros((height * width, 3))
124  for i, indices in enumerate(cluster_indices):
125  color = color_dict[DEFAULT_COLORS[i % n_colors]]
126  indices_to_color[indices] = color
127  indices_to_color = indices_to_color.reshape((height, width, 3))
128  result = indices_to_color * alpha + image * (1 - alpha)
129  result = (result * 255).astype(np.uint8)
130  return result
jsk_recognition_utils.visualize.get_tile_image
def get_tile_image(imgs, tile_shape=None, result_img=None, margin_color=None, min_size=50)
Definition: visualize.py:65
jsk_recognition_utils.visualize._tile_images
def _tile_images(imgs, tile_shape, concatenated_image, margin_color=None)
Definition: visualize.py:34
jsk_recognition_utils.visualize.colorize_cluster_indices
def colorize_cluster_indices(image, cluster_indices, alpha=0.3, image_alpha=1)
Definition: visualize.py:112
jsk_recognition_utils.visualize.centerize
def centerize(src, dst_shape, margin_color=None)
Definition: visualize.py:12


jsk_recognition_utils
Author(s):
autogenerated on Fri May 16 2025 03:10:54