How can I put margins in an image?

前端 未结 4 1016
余生分开走
余生分开走 2020-12-11 22:39

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.

The image is binary and

4条回答
  •  不知归路
    2020-12-11 23:07

    A=ones(18,18);%// your actual image
    [M,N] = size(A);
    B = zeros(M+2,N+2);%// create matrix
    B(2:end-1,2:end-1) = A; %// matrix with zero edge around.
    

    This first gets the size of your image matrix, and creates a zero matrix with two additional columns and rows, after which you can set everything except the outer edges to the image matrix.

    Example with a non-square matrix of size [4x6]:

    B =
    
         0     0     0     0     0     0     0     0
         0     1     1     1     1     1     1     0
         0     1     1     1     1     1     1     0
         0     1     1     1     1     1     1     0
         0     1     1     1     1     1     1     0
         0     0     0     0     0     0     0     0
    

提交回复
热议问题