聘我网

新概念招聘3.0

MATLAB如何将图形填充为方形?

vote up0vote downstar

将任意长宽比例的图片用背景颜色填充为方形?

 

1 个答复

vote up0vote downcheck

直接拼图:

[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

插入法:

[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
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.