Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 712685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:52:30+00:00 2026-05-14T04:52:30+00:00

This is a further question based on this answer: How can I implement a

  • 0

This is a further question based on this answer:

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:52:31+00:00Added an answer on May 14, 2026 at 4:52 am

    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…

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

    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
    

    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)
    

    Solution #2: Make a blank square image and insert the original image

    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
    

    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)
    

    Solution #3: Use 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
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i have created a question answer based app, the question's used to be
This is a bit of a long shot, but if anyone can figure it
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
This most be the second most simple rollover effect, still I don't find any
This is a self-explanatory question: Why does this thing bubble into my try catch's
inHi, this question is fast, but from my point of view its pretty hard.
I asked a question similar to this one a couple of weeks ago, but
[EDIT: further digging revealed a different root issue. I'm rephrasing the question, but leaving
This is starting to vex me. I recently decided to clear out my FTP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.