【问题标题】:How do I automate the padding for arbitary images using MATLAB?如何使用 MATLAB 自动填充任意图像?
【发布时间】:2010-04-09 05:12:40
【问题描述】:

这是基于此答案的进一步问题:

How can I implement a fisheye lens effect (barrel transformation) in MATLAB?

通用解决方案应该适用于所有背景颜色和长/宽比。

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    通常情况下,在 MATLAB 中有许多不同的方法可以做到这一点。我将列出一些填充RGB images的示例...

    解决方案 #1:使用 CAT 添加填充以制作方形图像

    此解决方案采用给定颜色 padColor 并使用函数 REPMAT 复制它以创建正确大小、形状和颜色的填充。然后使用函数CAT 将填充添加到图像的两侧:

    [r,c,d] = size(rgbImage);  %# Get the image dimensions
    nPad = abs(c-r)/2;         %# The padding size
    padColor = [1 1 1];        %# RGB triple for pad color (white)
    padColor = reshape(padColor,1,1,3);  %# Reshape pad color to 1-by-1-by-3
    if c > r                   %# Pad rows
      newImage = cat(1,repmat(padColor,floor(nPad),c),...  %# Top padding
                       rgbImage,...                        %# Image
                       repmat(padColor,ceil(nPad),c));     %# Bottom padding
    elseif r > c               %# Pad columns
      newImage = cat(2,repmat(padColor,r,floor(nPad)),...  %# Left padding
                       rgbImage,...                        %# Image
                       repmat(padColor,r,ceil(nPad)));     %# Right padding
    end
    

    您可以修改上述解决方案以适用于indexedgrayscalebinary images,方法是将定义padColor 的两行替换为以下内容之一:

    padColor = uint8(1);    %# For an indexed image (index of color to use)
    padColor = uint8(255);  %# For a grayscale image (white)
    padColor = true;        %# For a binary image (white)
    


    解决方案#2:制作一个空白方形图像并插入原始图像

    此解决方案采用给定颜色padColor 并使用函数REPMAT 复制它以创建该颜色的空白方形图像。然后将原始图像插入到这个空白图像的中心位置:

    [r,c,d] = size(rgbImage);  %# Get the image dimensions
    padColor = [1 1 1];        %# RGB triple for pad color (white)
    padColor = reshape(padColor,1,1,3);  %# Reshape pad color to 1-by-1-by-3
    if c > r                   %# Pad rows
      newImage = repmat(padColor,c);  %# Make c-by-c-by-3 matrix of given color
      rowIndex = floor((c-r)/2);      %# Row index for inserting image
      newImage(rowIndex+(1:r),:,:) = rgbImage;     %# Insert the image
    elseif r > c               %# Pad columns
      newImage = repmat(padColor,r);  %# Make r-by-r-by-3 matrix of given color
      columnIndex = floor((r-c)/2);   %# Column index for inserting image
      newImage(:,columnIndex+(1:c),:) = rgbImage;  %# Insert the image
    end
    

    您可以修改上述解决方案以适用于 indexedgrayscalebinary images,方法是将定义 padColor 的两行替换为以下内容之一:

    padColor = uint8(1);    %# For an indexed image (index of color to use)
    padColor = uint8(255);  %# For a grayscale image (white)
    padColor = true;        %# For a binary image (white)
    


    解决方案#3:使用PADARRAY

    此解决方案使用函数PADARRAY 创建填充以使图像呈正方形。不幸的是,在使用此解决方案时,没有简单的方法可以为 RGB images 指定填充颜色(见下文)。但是,您可以使用 'replicate' 参数让 PADARRAY 简单地复制添加填充的图像边缘的颜色:

    [r,c,d] = size(rgbImage);  %# Get the image dimensions
    nPad = abs(c-r)/2;         %# The padding size
    if c > r                   %# Pad rows
      newImage = padarray(rgbImage,[floor(nPad) 0],...  %# Pad top
                          'replicate','pre');
      newImage = padarray(newImage,[ceil(nPad) 0],...   %# Pad bottom
                          'replicate','post');
    elseif r > c               %# Pad columns
      newImage = padarray(rgbImage,[0 floor(nPad)],...  %# Pad left
                          'replicate','pre');
      newImage = padarray(newImage,[0 ceil(nPad)],...   %# Pad right
                          'replicate','post');
    end
    

    此解决方案适用于 indexedgrayscalebinary images。对于这三种图像类型,您可以选择将'replicate' 参数替换为要用于填充的标量值(即uint8(255) 用于灰度图像中的白色填充)。对于RGB images,将'replicate' 参数替换为单个值将只允许您创建从白色到黑色的灰色阴影填充颜色(即1 创建白色填充)。

    【讨论】:

    • @user198729:函数 RESHAPE 改变矩阵或向量的大小。我将padColor 从一个 1×3 矩阵更改为一个 1×1×3 矩阵,因为这使得以后使用 REPMAT 将其扩展为 N×M×3 填充矩阵变得更加容易。
    • 似乎没有逻辑来检索填充的背景颜色,所以我认为它是硬编码的,这不适用于所有背景颜色?
    • @user198729:我添加了更多选项供您选择。您可以自己轻松地添加逻辑来选择您认为的“背景颜色”。例如,您可以通过说padColor = rgbImage(1,1,:); 来选择图像的左上角颜色作为背景颜色。或者,我上面列出的第三种解决方案将简单地复制图像的边缘颜色来为填充区域着色。
    • 先谢谢了,我似乎消化了很多,我会尽快接受这个:)
    • ...cat(2,repmat(padColor,r,floor(nPad)),... 中是什么意思?
    猜你喜欢
    • 2016-12-01
    • 2011-09-29
    • 2019-04-10
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多