使用Gabor滤波器的图像处理

时间:2011-09-14 16:03:51

标签: matlab

我正在尝试对图像执行gabor过滤器。

%% Read

clear all;
close all;
clc;
I=imread('test.png');
imshow(I);

%% Crop
I2 = imcrop(I);
figure, imshow(I2)
m=size(I2,1);
n=size(I2,2);
%% Gabor
phi = 7*pi/8;
theta = 2;
sigma = 0.65*theta;
for i=1:3
    for j=1:3
        xprime= j*cos(phi);
        yprime= i*sin(phi);
        K = exp(2*pi*theta*i(xprime+ yprime));
        G= exp(-(i.^2+j.^2)/(sigma^2)).*abs(K);
    end
end

%% Convolve

for i=1:m
    for j=1:n
       J(i,j)=conv2(I2,G);
    end
end
imshow(uint8(J))

我总是得到这个错误。

??? Subscript indices must either be real positive integers or logicals.

不确定如何解决此问题...... enter image description here

enter image description here

2 个答案:

答案 0 :(得分:4)

*与括号之间的K = exp(2*pi*theta*i(xprime+ yprime));内遗漏了i。你应该是K = exp(2*pi*theta*i*(xprime+ yprime));。正是由于这种情况,Mathworks建议使用sqrt(-1)作为虚数。

更新: 在Matlab中你不需要循环来进行卷积。你只需说J=conv2(I2,G);

更新2:

这是工作代码

%% Gabor
phi = 7*pi/8;
theta = 2;
sigma = 0.65*theta;
filterSize = 6;

G = zeros(filterSize);


for i=(0:filterSize-1)/filterSize
    for j=(0:filterSize-/filterSize
        xprime= j*cos(phi);
        yprime= i*sin(phi);
        K = exp(2*pi*theta*sqrt(-1)*(xprime+ yprime));
        G(round((i+1)*filterSize),round((j+1)*filterSize)) = exp(-(i^2+j^2)/(sigma^2))*K;
    end
end

%% Convolve

J = conv2(I2,G);
imshow(imag(J));

答案 1 :(得分:0)

根据上面的答案,最终的代码是:

clear all;
close all;
clc;
I=imread('test.png');
imshow(I);

%% Crop
I2 = imcrop(I);
figure, imshow(I2)

    phi = 7*pi/8;
    theta = 2;
    sigma = 0.65*theta;
    filterSize = 6;

    G = zeros(filterSize);

    for i=(0:filterSize-1)/filterSize
        for j=(0:filterSize-1)/filterSize
            xprime= j*cos(phi);
            yprime= i*sin(phi);
            K = exp(2*pi*theta*sqrt(-1)*(xprime+ yprime));
            G(round((i+1)*filterSize),round((j+1)*filterSize)) = exp(-(i^2+j^2)/(sigma^2))*K;
        end
    end

    J = conv2(I,G);
    figure(2);
    imagesc(imag(J))