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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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
padColorand 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:You can modify the above solution to work for indexed, grayscale, or binary images by replacing the two lines defining the
padColorwith one of the following:Solution #2: Make a blank square image and insert the original image
This solution takes a given color
padColorand 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:You can modify the above solution to work for indexed, grayscale, or binary images by replacing the two lines defining the
padColorwith one of the following: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: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.1creates white padding).