Gabor过滤器代码错误

时间:2012-01-19 16:17:54

标签: image matlab image-processing

编辑:GABOR FILTER的描述

 
 % gab2d: **2D Gabor filter**

 % The Gabor filter is basically a Gaussian, modulated by a complex sinusoid
 % G = gab2d(I,Sx,Sy,f,theta,FUN)


 % Input and output arguments ([]'s are optional):
 % I (matrix) of size NxM: Input Image of size NxM. 
 gamma (scalar): The spatial aspect ratio, x to y.
 lambda(scalar): The wavelength of the sinusoidal function. 
 b (scalar): The spatial frequency band-width (in octaves)
 theta (scalar): The orientation of the gabor filter.
 % phi (scalar): The phase offset. 0 is real part of Gabor filter or
 % even-symmetric, pi/2 is imaginary part of Gabor filter or
 % odd-symmetric.

 % **Note**:

 sigma (scalar): The spread of Gabor filter or the standard
 % deviation of Gaussian is automatically computed from lambda and b.
 % [shape] (strings): Shape for conv2. See help conv2. Default is 'same'.
 % % GO (matrix) of size NxM: Output images which was applied Gabor
 % filters. This is the magnitude response.
 % [GF] (matrix) of size (2Sx+1)x(2Sy+1): Gabor filter.
function [GO, GF] = gab2d(I, gamma, lambda, b, theta, phi, shape)

I=imread('C:\Users\Vinay\Documents\MATLAB\textureflawimages\text9.png');

gamma = 1; b = 1; theta = 0:pi/6:pi-pi/6; phi = 0; shape = 'valid'; lambda=8;

if nargin < 7, shape = 'same'; end;

if isa(I, 'double') ~= 1, I = double(I); end

sigma = (1 / pi) * sqrt(log(2)/2) * (2^b+1) / (2^b-1) * lambda;

Sy = sigma * gamma;

for x = -fix(sigma):fix(sigma)

    for y = -fix(Sy):fix(Sy)

        xp = x * cos(theta) + y * sin(theta);

        yp = y * cos(theta) - x * sin(theta);

        GF(fix(Sy)+y+1,fix(sigma)+x+1) = ...

        exp(-.5*(xp^2+gamma^2*yp^2)/sigma^2) * cos(2*pi*xp/lambda+phi) ...

        ; %/ (2*pi*(sigma^2/gamma));

        % Normalize if you use different sigma (lambda or b)
    end
end
GO = conv2(I, double(GF), shape);

错误:

  

???使用==&gt;时出错mpower Matrix必须是方形的。

     

==&gt;中的错误gab2d at 36 GF(fix(Sy)+ y + 1,fix(sigma)+ x + 1)= ...

我在某种程度上无法纠正这个问题。

请帮忙

1 个答案:

答案 0 :(得分:2)

theta是一个数组。因此,例如, xp也是一个数组。如果要对xp的每个元素求平方,则需要使用元素运算符,例如.^表示幂,或.*表示乘法。

为了更快地找出错误,请在命令行输入dbstop if error时将调试器设置为在出现错误时停止。这允许您通过鼠标悬停在编辑器中检查所有变量,并评估复杂表达式的一些小部分以缩小错误范围。