【问题标题】:Resize image mask (shrink) using max value of united pixel group使用联合像素组的最大值调整图像掩码(缩小)的大小
【发布时间】:2022-11-12 07:07:39
【问题描述】:

我想调整大小,特别是缩小掩码(1 和 0 的二维数组),以便低分辨率掩码中的任何像素映射到高分辨率掩码(原始)中的一组像素,其中包含至少一个值 1 将被设置为 1 本身(底部示例)。

我尝试使用 cv2.resize() 使用 cv2.INTER_MAX 但它返回了一个错误:

错误:OpenCV(4.6.0)/io/opencv/modules/imgproc/src/resize.cpp:3927:错误:(-5:错误参数)函数“调整大小”中的未知插值方法

Pillow Image 或 scipy 似乎没有插值方法来这样做。

我正在寻找定义的 shrink_max() 的解决方案

>>> orig_mask = [[1,0,0],[0,0,0],[0,0,0]]
>>> orig_mask
[[1,0,0]
,[0,0,0]
,[0,0,0]]
>>> mini_mask = shrink_max(orig_mask, (2,2))
>>> mini_mask
[[1,0]
,[0,0]]
>>> mini_mask = shrink_max(orig_mask, (1,1))
>>> mini_mask
[[1]]

【问题讨论】:

    标签: opencv numpy-ndarray image-resizing


    【解决方案1】:

    我不知道直接方法,但尝试将蒙版缩小到一半大小,即每个低分辨率像素映射到 4 个原始像素(根据您的需要修改为任何比率):

    import numpy as np
    
    orig_mask = np.array([[1,0,0],[0,0,0],[0,0,0]])
    
    # first make the original mask divisible by 2
    pad_row = orig_mask.shape[0] % 2
    pad_col = orig_mask.shape[1] % 2
    
    # i.e. pad the right and bottom of the mask with zeros
    orig_mask_padded = np.pad(orig_mask, ((0,pad_row), (0,pad_col)))
    
    # get the new shape
    new_rows = orig_mask_padded.shape[0] // 2
    new_cols = orig_mask_padded.shape[1] // 2
    
    # group the original pixels by fours and max each group 
    shrunk_mask = orig_mask_padded.reshape(new_rows, 2, new_cols, 2).max(axis=(1,3))
    
    print(shrunk_mask)
    

    在此处检查使用子矩阵:Numpy: efficiently sum sub matrix m of M

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      相关资源
      最近更新 更多