有时可视化会要同时放多幅图,如分割的原图、label、pseudo-label 和 prediction。当图很多,简单地排成一行可能会太长,不便观看。考虑将图排成二维网格(grid)展示,且为方便看,考虑让网格尽可能「紧致」是不是「发散」。
可用网格的周长衡量其紧致程度。由于积定和最小,所有图的总面积一定,存在一种排法使得网格周长最短,从而最紧致。一般地,有 n 幅图要一齐展示,排成 H × W H\times W H×W 的二维网格,则编排的目的是使网格的(半)周长 H + W 尽量小。
有时为了令此周长最短,可能需要多一些冗余坑位。如 17 幅周长为 a 的正方形图,如果排成 1x17 或 17x1,则网格的半周长是 a + 17a = 18a;而若放在 2x9 网格内,虽然共 18 个格,冗余一格,但此时半周长为 2a + 9a = 11a < 18a;若是 3x6 则更短:3a + 6a = 9a;而 4x5 也是 4a + 5a = 9a,但会冗余两格。即越接近正方形越紧致。
Code
exact参数指定是否限制网格恰有 n 个格
# import math, numpy as np
def compact_image_grid(image_list, exact=False):
"""adaptively arrange images in a compactest 2D grid (for better visualisation)
Input:
image_list: list of images in format of [h, w] or [h, w, c] numpy.ndarray
exact: bool, subjest to #grids = #images or not.
If False, #grids > #images may happen for a more compact view.
Output:
grid: [H, W] or [H, W, c], compiled images
"""
n = len(image_list)
if 1 == n:
return image_list[0]
# max image resolution
max_h, max_w = 0, 0
for im in image_list:
h, w = im.shape[:2]
max_h = max(max_h, h)
max_w = max(max_w, w)
# find compactest layout
nr, nc = 1, n
min_peri = nr * max_h + nc * max_w # 1 row
for r in range(n, 1, -1):
if exact and n % r != 0:
continue
c = math.ceil(n / r)
assert r * c >= n and r * (c - 1) <= n
peri = r * max_h + c * max_w
if peri < min_peri:
nr, nc, min_peri = r, c, peri
assert nr * nc >= n
grid_shape = (nr * max_h, nc * max_w) + image_list[0].shape[2:]
grid = np.zeros(grid_shape, dtype=image_list[0].dtype)
for i, img in enumerate(image_list):
r, c = i // nc, i % nc
h, w = img.shape[:2]
grid[r*max_h: r*max_h+h, c*max_w: c*max_w+w] = img
return grid
Test
import os, math
import numpy as np
from PIL import Image
def compact_image_grid(image_list, exact=False):
"""见前文"""
pass
# 读 17 幅图
p = os.path.expanduser(r"~\Pictures\wallpaper")
img_list = []
for i, f in enumerate(os.listdir(p)):
img = np.asarray(Image.open(os.path.join(p, f)).resize((224, 224)))
if img.ndim < 3: continue
img_list.append(img[:, :, :3])
if len(img_list) >= 17: break
# exact=False,必须恰有 n 格
Image.fromarray(compact_image_grid(img_list, exact=False)).save("grid.png")
# exact=True,允许冗余格
Image.fromarray(compact_image_grid(img_list, exact=True)).save("grid-exact.png")
效果:
允许冗余:

准确格数:

1892

被折叠的 条评论
为什么被折叠?



