--------------------------------------------------------------------------------------------- author: Kyle Hounslow --------------------------------------------------------------------------------------------- Converts a list of single images into a list of 'montage' images of speci
(image_list, image_shape, montage_shape)
| 236 | return lib.__version__.startswith(major) |
| 237 | |
| 238 | def build_montages(image_list, image_shape, montage_shape): |
| 239 | """ |
| 240 | --------------------------------------------------------------------------------------------- |
| 241 | author: Kyle Hounslow |
| 242 | --------------------------------------------------------------------------------------------- |
| 243 | Converts a list of single images into a list of 'montage' images of specified rows and columns. |
| 244 | A new montage image is started once rows and columns of montage image is filled. |
| 245 | Empty space of incomplete montage images are filled with black pixels |
| 246 | --------------------------------------------------------------------------------------------- |
| 247 | :param image_list: python list of input images |
| 248 | :param image_shape: tuple, size each image will be resized to for display (width, height) |
| 249 | :param montage_shape: tuple, shape of image montage (width, height) |
| 250 | :return: list of montage images in numpy array format |
| 251 | --------------------------------------------------------------------------------------------- |
| 252 | |
| 253 | example usage: |
| 254 | |
| 255 | # load single image |
| 256 | img = cv2.imread('lena.jpg') |
| 257 | # duplicate image 25 times |
| 258 | num_imgs = 25 |
| 259 | img_list = [] |
| 260 | for i in xrange(num_imgs): |
| 261 | img_list.append(img) |
| 262 | # convert image list into a montage of 256x256 images tiled in a 5x5 montage |
| 263 | montages = make_montages_of_images(img_list, (256, 256), (5, 5)) |
| 264 | # iterate through montages and display |
| 265 | for montage in montages: |
| 266 | cv2.imshow('montage image', montage) |
| 267 | cv2.waitKey(0) |
| 268 | |
| 269 | ---------------------------------------------------------------------------------------------- |
| 270 | """ |
| 271 | if len(image_shape) != 2: |
| 272 | raise Exception('image shape must be list or tuple of length 2 (rows, cols)') |
| 273 | if len(montage_shape) != 2: |
| 274 | raise Exception('montage shape must be list or tuple of length 2 (rows, cols)') |
| 275 | image_montages = [] |
| 276 | # start with black canvas to draw images onto |
| 277 | montage_image = np.zeros(shape=(image_shape[1] * (montage_shape[1]), image_shape[0] * montage_shape[0], 3), |
| 278 | dtype=np.uint8) |
| 279 | cursor_pos = [0, 0] |
| 280 | start_new_img = False |
| 281 | for img in image_list: |
| 282 | if type(img).__module__ != np.__name__: |
| 283 | raise Exception('input of type {} is not a valid numpy array'.format(type(img))) |
| 284 | start_new_img = False |
| 285 | img = cv2.resize(img, image_shape) |
| 286 | # draw image to black canvas |
| 287 | montage_image[cursor_pos[1]:cursor_pos[1] + image_shape[1], cursor_pos[0]:cursor_pos[0] + image_shape[0]] = img |
| 288 | cursor_pos[0] += image_shape[0] # increment cursor x position |
| 289 | if cursor_pos[0] >= montage_shape[0] * image_shape[0]: |
| 290 | cursor_pos[1] += image_shape[1] # increment cursor y position |
| 291 | cursor_pos[0] = 0 |
| 292 | if cursor_pos[1] >= montage_shape[1] * image_shape[1]: |
| 293 | cursor_pos = [0, 0] |
| 294 | image_montages.append(montage_image) |
| 295 | # reset black canvas |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…