我正在通过将指纹图像划分为41 * 41的块来估计指纹图像的方向。图像大小为240 * 320 ..这是我的代码,问题是我得到的输出图像大小与输入不同图像。
% 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;
但是在这个过程中我失去了边界处的像素,以避免“索引超过”错误,即theta的大小是m = 240-41 = 199和n = 320-41 = 279.那么我的输入图像尺寸为240 * 320,输出图像尺寸为199 * 279 ..如何获得与输入图像尺寸相同的输出图像。 还有一件事我不得不使用“blockproc”功能......提前致谢
答案 0 :(得分:1)
您可以使用padarray
在矩阵中添加零:
A1 = padarray(A,[7 8],'post'); % 240+7=41*7, 320+8=41*8
B1 = padarray(B,[7 8],'post');
然后使用Gxx
和Gyy
生成Gxy
,A1
和B1
。
方法2:
此外,我尝试通过删除循环来简化您的代码,供您参考:
% 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);
您可以查看blockproc
了解详情。