Search code examples
matlabmatrixbinary-image

How can I put margins in an image?


I have a binary image of 18x18 pixels and I want to put margins around this image with the purpose of obtaining an image 20x20 pixels.

Description of the problem

The image is binary and it can be represented by a matrix of 1s and 0s. The 0 pixels are in black colour and the 1 pixels are in white colour. I need to put margins of 1 pixel of zeros around the image that I have.

How can I do it?


Solution

  • Let's get hackish:

    %// Data:
    A = magic(3);                 %// example original image (matrix)
    N = 1;                        %// margin size
    
    %// Add margins:
    A(end+N, end+N) = 0;          %// "missing" values are implicitly filled with 0
    A = A(end:-1:1, end:-1:1);    %// now flip the image up-down and left-right ...
    A(end+N, end+N) = 0;          %// ... do the same for the other half ...
    A = A(end:-1:1, end:-1:1);    %// ... and flip back