python多幅图自适应紧致二维排放

有时可视化会要同时放多幅图,如分割的原图、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")

效果:

允许冗余:
在这里插入图片描述
准确格数:
grid-exact

### Python 实现自适应图像二值化的方法与示例 #### 自适应图像二值化的概念 自适应图像二值化是一种根据图像局部区域的特性动态调整阈值的技术,适用于光照不均匀或背景复杂的场景。它通过计算每个像素点周围的局部统计信息(如均值或加权平均值)来确定最佳阈值[^2]。 #### 常见的自适应二值化方法 1. **均值法**:使用局部区域的均值作为阈值。 2. **高斯加权法**:使用高斯权重计算局部区域的加权平均值作为阈值。 #### OpenCV实现自适应二值化 OpenCV库提供了`cv2.adaptiveThreshold()`函数,用于实现自适应二值化。以下是其实现方式和参数说明: - `src`:输入图像(灰度)。 - `maxValue`:二值化后的最大值。 - `adaptiveMethod`:自适应阈值计算方法(`cv2.ADAPTIVE_THRESH_MEAN_C`或`cv2.ADAPTIVE_THRESH_GAUSSIAN_C`)。 - `thresholdType`:二值化类型(`cv2.THRESH_BINARY`或`cv2.THRESH_BINARY_INV`)。 - `blockSize`:邻域大小,必须为奇数。 - `C`:从均值或加权均值中减去的常数。 #### 示例代码 以下是一个完整的Python代码示例,展示如何使用OpenCV实现自适应二值化: ```python import cv2 # 读取图像并转换为灰度 image = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE) # 使用均值法进行自适应二值化 binary_mean = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) # 使用高斯加权法进行自适应二值化 binary_gaussian = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) # 显示结果 cv2.imshow('Original Image', image) cv2.imshow('Adaptive Mean Thresholding', binary_mean) cv2.imshow('Adaptive Gaussian Thresholding', binary_gaussian) cv2.waitKey(0) cv2.destroyAllWindows() ``` 上述代码展示了如何使用均值法和高斯加权法对图像进行自适应二值化,并显示处理结果[^3]。 #### 参数调整的影响 - **窗口大小(blockSize)**:增大窗口大小可以考虑更大范围的像素值,从而适应不同大小的文字或目标。但过大的窗口可能导致细节丢失。 - **偏移值(C)**:减小偏移值可以使阈值更接近局部像素均值,以更好地区分目标和背景[^4]。 #### 处理复杂场景 对于复杂光照条件下的图像,可以通过结合形态学操作(如膨胀和腐蚀)进一步优化二值化结果。例如,去除噪声或填补断裂的轮廓。 ```python # 形态学操作 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) dilated = cv2.dilate(binary_gaussian, kernel, iterations=1) eroded = cv2.erode(dilated, kernel, iterations=1) # 显示形态学操作结果 cv2.imshow('Dilated Image', dilated) cv2.imshow('Eroded Image', eroded) cv2.waitKey(0) cv2.destroyAllWindows() ``` #### 局部自适应二值化递归法 对于某些特殊场景,可以采用递归法局部自适应二值化技术,通过划分窗口并逐级优化阈值来提高分割精度[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值