直接拼图:
[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