如何使用MATLAB自动填充仲裁图像? [英] How do I automate the padding for arbitary images using MATLAB?

查看:185
本文介绍了如何使用MATLAB自动填充仲裁图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a further question based on this answer:

如何在MATLAB中实现鱼眼镜头效果(桶形变换)?

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

The general solution should work for all background colors and length/width ratios.

推荐答案

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

As is often the case, there are a number of different ways to do this in MATLAB. I'll list a few examples for padding RGB images...

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

This solution takes a given color padColor and replicates it using the function REPMAT to create padding of the right size, shape, and color. The padding is then added to the sides of the image using the function 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

您可以修改上述解决方案以适用于已编入索引灰度,或二进制图片,替换定义<$ c $的两行c> padColor ,其中包含以下内容之一:

You can modify the above solution to work for indexed, grayscale, or binary images by replacing the two lines defining the padColor with one of the following:

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)


此解决方案采用给定颜色 padColor 并使用 REPMAT 创建该颜色的空白方形图像。然后将原始图像插入到居中位置的空白图像中:

This solution takes a given color padColor and replicates it using the function REPMAT to create a blank square image of that color. The original image is then inserted into this blank image in a centered position:

[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

您可以修改上述解决方案以适用于已编入索引灰度,或二进制图片,替换定义<$ c的两行$ c> padColor ,其中包含以下内容之一:

You can modify the above solution to work for indexed, grayscale, or binary images by replacing the two lines defining the padColor with one of the following:

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)


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

This solution uses the function PADARRAY to create the padding to make the image square. Unfortunately, there's no easy way to specify the padding color you want for RGB images when using this solution (see below). However, you can use the 'replicate' argument to have PADARRAY simply replicate the color at the edges of the image where it is adding the padding:

[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

此解决方案适用于已编入索引灰度,或二进制图像。对于这三种图像类型,您可以选择将'replicate'参数替换为要用于填充的标量值(即 uint8(255)用于灰度图像中的白色填充)。对于 RGB图像,请替换带有单个值的'replicate'参数只允许您创建灰色阴影的填充颜色,范围从白色到黑色(即 1 创建白色填充。)

This solution will work for indexed, grayscale, or binary images. For these three image types, you have the option to replace the 'replicate' argument with a scalar value that you want to use for the padding (i.e. uint8(255) for white padding in a grayscale image). For RGB images, replacing the 'replicate' argument with a single value will only allow you to create padding colors that are gray shades ranging from white to black (i.e. 1 creates white padding).

这篇关于如何使用MATLAB自动填充仲裁图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆