如何检测并移除图像数组中由旋转、平移产生的非规整零填充(支持NumPy/TensorFlow实现)
边缘冗余零填充自动裁剪实现方案
核心规则
需要移除的冗余零填充,是从图像数组外边缘出发、通过邻接关系连通的全通道零值像素。裁剪目标是找到完全落在非冗余区域内的最大轴对齐矩形,移除所有矩形外的连通零填充。支持CHW、HWC两种排布格式的输入,无需提前获知图像旋转角度。
NumPy 通用实现
- 维度适配
- 若输入为CHW格式(形状形如
(C, H, W)),通过np.transpose(arr, (1, 2, 0))转换为HWC格式,将通道维度调整到最后一维。 - 若输入为HWC格式(形状形如
(H, W, C)),直接使用原数组处理。
- 若输入为CHW格式(形状形如
- 生成全零掩码
沿着最后一维(通道维)判断每个空间位置的所有通道值是否全为0,得到形状为(H, W)的二值掩码:zero_mask = np.all(arr_hwc == 0, axis=-1)
掩码中值为True代表该位置是纯零像素,False代表该位置存在非零有效内容。 - 标记边缘连通零域
通过4邻接扩散规则,将所有和数组外边缘连通的零像素标记为需要移除的冗余块:- 以数组四周边缘上的零像素作为初始扩散种子。
- 从种子出发,将相邻位置的零像素全部纳入冗余集合,直到无新的可扩散零像素。
- 得到有效区域掩码
valid_mask = ~(zero_mask & connected_to_edge),值为True的位置是需要保留的内容。
- 定位裁剪边界
基于有效掩码,通过经典柱状图法找到有效区域内的最大轴对齐内接矩形,得到四个边界值:上边界top、下边界bottom、左边界left、右边界right。 - 执行裁剪
按原输入格式切片:- CHW格式输入:裁剪结果为
arr[:, top:bottom, left:right] - HWC格式输入:裁剪结果为
arr[top:bottom, left:right, :]
- CHW格式输入:裁剪结果为
TensorFlow 张量实现
完全基于TensorFlow原生张量操作,支持动态图、静态图运行,无需转换为NumPy数组:
import tensorflow as tf def crop_edge_zero_padding(arr, data_format='CHW'): # 维度适配为HWC格式 if data_format == 'CHW': arr_hwc = tf.transpose(arr, perm=[1, 2, 0]) else: arr_hwc = arr H, W = tf.shape(arr_hwc)[0], tf.shape(arr_hwc)[1] # 生成全通道零值掩码 zero_mask = tf.reduce_all(tf.equal(arr_hwc, 0), axis=-1) # 初始化边缘连通零的扩散种子 connected_edge = tf.Variable(tf.zeros([H, W], dtype=tf.bool)) connected_edge[0, :].assign(zero_mask[0, :]) connected_edge[H-1, :].assign(zero_mask[H-1, :]) connected_edge[:, 0].assign(zero_mask[:, 0]) connected_edge[:, W-1].assign(zero_mask[:, W-1]) # 迭代扩散直到连通域收敛 while True: shifted = tf.zeros_like(connected_edge) # 上方向邻接扩散 shifted = shifted | tf.pad(connected_edge[:-1, :], paddings=[[1,0],[0,0]]) # 下方向邻接扩散 shifted = shifted | tf.pad(connected_edge[1:, :], paddings=[[0,1],[0,0]]) # 左方向邻接扩散 shifted = shifted | tf.pad(connected_edge[:, :-1], paddings=[[0,0],[1,0]]) # 右方向邻接扩散 shifted = shifted | tf.pad(connected_edge[:, 1:], paddings=[[0,0],[0,1]]) new_connected = connected_edge | (shifted & zero_mask) if tf.reduce_all(tf.equal(new_connected, connected_edge)): break connected_edge = new_connected valid_mask = tf.logical_not(connected_edge) # 基于柱状图找最大内接矩形边界 hist = tf.zeros([W], dtype=tf.int32) max_area = 0 best_top, best_bottom, best_left, best_right = 0, H, 0, W for h in range(H): row_valid = tf.cast(valid_mask[h, :], tf.int32) hist = tf.where(row_valid == 1, hist + 1, tf.zeros_like(hist)) stack = [] for w in range(W + 1): cur_h = hist[w] if w < W else 0 while stack and hist[stack[-1]] > cur_h: height = hist[stack.pop()] width = w if not stack else w - stack[-1] - 1 area = height * width if area > max_area: max_area = area left = stack[-1] + 1 if stack else 0 right = w top = h - height + 1 bottom = h + 1 best_top, best_bottom, best_left, best_right = top, bottom, left, right stack.append(w) # 按原格式裁剪输出 if data_format == 'CHW': return arr[:, best_top:best_bottom, best_left:best_right] else: return arr[best_top:best_bottom, best_left:best_right, :]
示例匹配验证
- 对示例1的垂直右零填充三通道数组,算法识别到右边缘连通的整列零值域,裁剪结果和
arr[:, :, :-1]完全一致。 - 对示例2的右上角三角零填充数组,算法识别到右上角和边缘连通的零块,定位的最大内接矩形边界对应切片
arr2[:, 1:, :-1],和预期效果完全匹配。
内容的提问来源于stack exchange,提问作者skbrhmn
相关产品推荐
相关产品推荐

