How to automate padding for harsh images using MATLAB?

This is another question based on this answer:

How can I realize the fisheye lens effect (barrel transformation) in MATLAB?

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

+2
source share
1 answer

As often happens, in MATLAB there are several ways to do this. I will give some examples for filling RGB images ...

Solution # 1: add padding with CAT to make a square image

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

indexed, , , 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

indexed, , , 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- (. ). , '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

indexed, , . 'replicate' , (.. uint8(255) ). RGB-, 'replicate' , , (.. 1 ).

+5

Source: https://habr.com/ru/post/1740552/


All Articles