Search code examples
matlabimage-processingsizeblock

how to get Image of same size after processing


I am estimating ridge orientation of an fingerprint image by dividing it into blocks of 41*41..image is of size 240*320..here is my code and the problem is that I am getting output image size different than input image.

 % matalb code for orientation
    im =imread('D:\project\116_2_5.jpg');
    im = double(im);
    [m,n] = size(im);

    % to normalise image
    nor = im - mean(im(:));
    im = nor/std(nor(:));
    w = 41;

    % To calculate x and y gradient component using 3*3 sobel mask
    [delx,dely] = gradient(im);

    % Ridge orientation
     for i=21:w:240-41        
        for j=21:w:320-41     

            A = delx(i-20:i+20,j-20:j+20);
            B = dely(i-20:i+20,j-20:j+20); 

            Gxy = sum(sum(A.*B));
            Gxx = sum(sum(A.*A));
            Gyy = sum(sum(B.*B));
            diff = Gxx-Gyy;

            theta(i-20:i+20,j-20:j+20) = (pi/2) + 0.5*atan2(2*Gxy,diff);

        end;
    end;

but in this process i am loosing the pixels at the boundries so as to avoid the "index exceed" error i.e size of theta is m = 240-41 = 199 and n = 320-41=279..Thus my input image size is 240*320 and output image size is size 199*279..How can i get output image same as size of input image. one more thing that i dnt have to use "blockproc" function...Thanks in advance


Solution

  • You can use padarray to add zeros onto your matrix:

    A1 = padarray(A,[7 8],'post'); % 240+7=41*7, 320+8=41*8
    B1 = padarray(B,[7 8],'post');
    

    then generate Gxx, Gyy, and Gxy with A1 and B1.

    Method 2:

    Besides, I tried to simplify your code a little bit by removing the loops, for your reference:

    % Ridge orientation
    Gxy = delx .* dely;
    Gxx = delx .* delx;
    Gyy = dely .* dely;
    
    fun = @(x) sum(x(:))*ones(size(x));
    theta_Gxy = blockproc(Gxy,[41 41],fun, 'PadPartialBlocks', true);
    theta_diff = blockproc(Gxx-Gyy,[41 41],fun, 'PadPartialBlocks', true);
    
    theta0 = pi/2 + 0.5 * atan2(2 * theta_Gxy, theta_diff);
    theta = theta0(1:240, 1:320);
    

    You may check blockproc for more details.