From 34de475999a22ac7280411e15339e77a0c8dbc2e Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Tue, 12 May 2015 13:15:55 +0800 Subject: initial commit --- filterNorm.m | 3 +++ gaborFilter.m | 13 +++++++++++++ gabor_example.m | 43 +++++++++++++++++++++++++++++++++++++++++++ gabor_fn.m | 27 +++++++++++++++++++++++++++ genFeatureVec.m | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ getVecs.m | 26 ++++++++++++++++++++++++++ parGetVecs.m | 7 +++++++ schmidFilter.m | 5 +++++ 8 files changed, 179 insertions(+) create mode 100644 filterNorm.m create mode 100644 gaborFilter.m create mode 100644 gabor_example.m create mode 100644 gabor_fn.m create mode 100644 genFeatureVec.m create mode 100644 getVecs.m create mode 100644 parGetVecs.m create mode 100644 schmidFilter.m diff --git a/filterNorm.m b/filterNorm.m new file mode 100644 index 0000000..2037ae0 --- /dev/null +++ b/filterNorm.m @@ -0,0 +1,3 @@ +function fout = filterNorm(fin) + [x,y]=size(fin); + fout = fin-sum(sum(fin))/x/y; \ No newline at end of file diff --git a/gaborFilter.m b/gaborFilter.m new file mode 100644 index 0000000..8cf9260 --- /dev/null +++ b/gaborFilter.m @@ -0,0 +1,13 @@ +function fout = gaborFilter(gamma,theta,lambda,sigma) + sigma_x = sigma; + sigma_y = sigma/gamma; + + sz=fix(4*max(sigma_y,sigma_x)); + + [x y]=meshgrid(-sz:sz,sz:-1:-sz); + + % Rotation + x_theta=x*cos(theta)+y*sin(theta); + y_theta=-x*sin(theta)+y*cos(theta); + + fout=filterNorm(exp(-0.5*(x_theta.^2/sigma_x^2+y_theta.^2/sigma_y^2)).*exp(2*i*pi/lambda*x_theta)); \ No newline at end of file diff --git a/gabor_example.m b/gabor_example.m new file mode 100644 index 0000000..88d0bc0 --- /dev/null +++ b/gabor_example.m @@ -0,0 +1,43 @@ +function gabor_example() +% an example to demonstrate the use of gabor filter. +% requires lena.jpg in the same path. +% the results mimic: +% http://matlabserver.cs.rug.nl/edgedetectionweb/web/edgedetection_examples +% .html +% using default settings (except for in radians instead of degrees) +% +% note that gabor_fn only take scalar inputs, and multiple filters need to +% be generated using (nested) loops +% +% also, apparently the scaling of the numbers is different from the example +% software at +% http://matlabserver.cs.rug.nl +% but are consistent with the formulae shown there +lambda = 8; +theta = 0; +psi = [0 pi/2]; +gamma = 0.5; +bw = 1; +N = 8; +img_in = im2double(imread('lena.jpg')); +img_in(:,:,2:3) = []; % discard redundant channels, it's gray anyway +img_out = zeros(size(img_in,1), size(img_in,2), N); +for n=1:N + gb = gabor_fn(bw,gamma,psi(1),lambda,theta)... + + 1i * gabor_fn(bw,gamma,psi(2),lambda,theta); + % gb is the n-th gabor filter + img_out(:,:,n) = imfilter(img_in, gb, 'symmetric'); + % filter output to the n-th channel + theta = theta + 2*pi/N; + % next orientation +end +figure(1); +imshow(img_in); +title('input image'); +figure(2); +img_out_disp = sum(abs(img_out).^2, 3).^0.5; +% default superposition method, L2-norm +img_out_disp = img_out_disp./max(img_out_disp(:)); +% normalize +imshow(img_out_disp); +title('gabor output, L-2 super-imposed, normalized'); \ No newline at end of file diff --git a/gabor_fn.m b/gabor_fn.m new file mode 100644 index 0000000..effc0ba --- /dev/null +++ b/gabor_fn.m @@ -0,0 +1,27 @@ +function gb=gabor_fn(bw,gamma,psi,lambda,theta) +% bw = bandwidth, (1) +% gamma = aspect ratio, (0.5) +% psi = phase shift, (0) +% lambda= wave length, (>=2) +% theta = angle in rad, [0 pi) + +sigma = lambda/pi*sqrt(log(2)/2)*(2^bw+1)/(2^bw-1); +sigma_x = sigma; +sigma_y = sigma/gamma; + +sz=fix(8*max(sigma_y,sigma_x)); +if mod(sz,2)==0, sz=sz+1;end + +% alternatively, use a fixed size +% sz = 60; + +[x y]=meshgrid(-fix(sz/2):fix(sz/2),fix(sz/2):-1:fix(-sz/2)); +% x (right +) +% y (up +) + +% Rotation +x_theta=x*cos(theta)+y*sin(theta); +y_theta=-x*sin(theta)+y*cos(theta); + +gb=exp(-0.5*(x_theta.^2/sigma_x^2+y_theta.^2/sigma_y^2)).*cos(2*pi/lambda*x_theta+psi); +imshow(gb/2+0.5); \ No newline at end of file diff --git a/genFeatureVec.m b/genFeatureVec.m new file mode 100644 index 0000000..75fa0e8 --- /dev/null +++ b/genFeatureVec.m @@ -0,0 +1,55 @@ +function vec = genFeatureVec(img) + bins = 16; + ncnls = 8; + cnls = zeros(size(img,1), size(img,2),ncnls); + cnls(:,:,1:3) = img; + cnls(:,:,4:6) = rgb2ycbcr(img); + hsv = rgb2hsv(img); + cnls(:,:,7:8) = hsv(:,:,1:2); + % Schmid + scVec=[ + 1,2; + 1,4; + 2,4; + 1,6; + 2,6; + 3,6; + 1,8; + 2,8; + 3,8; + 1,10; + 2,10; + 3,10; + 4,10]; + gbVec = [ + 0.3,0,4,2; + 0.3,0,8,2; + 0.4,0,4,1; + 0.4,0,8,2; + 0.3,pi/2,4,2; + 0.3,pi/2,8,2; + 0.4,pi/2,4,1; + 0.4,pi/2,8,2;]; + gb = length(gbVec); + sc = length(scVec); + vec=[]; + for i = 1:sc + for cnl = 1:ncnls + filt = schmidFilter(scVec(i,1),scVec(i,2)); + pos = real(sum(sum(filt.*(filt>0)))); + neg = real(sum(sum(filt.*(filt<0)))); + ss = (pos-neg)/bins; + v = histcounts(imfilter(cnls(:,:,cnl), filt, 'symmetric'),(0:bins)*ss+neg); + vec=cat(2,vec,v); + end + end + for i = 1:gb + for cnl = 1:ncnls + filt = gaborFilter(gbVec(i,1),gbVec(i,2),gbVec(i,3),gbVec(i,4)); + pos = real(sum(sum(filt.*(filt>0)))); + neg = real(sum(sum(filt.*(filt<0)))); + ss = (pos-neg)/bins; + v = histcounts(real(imfilter(cnls(:,:,cnl), filt, 'symmetric')),(0:bins)*ss+neg); + vec=cat(2,vec,v); + end + end \ No newline at end of file diff --git a/getVecs.m b/getVecs.m new file mode 100644 index 0000000..40f08cd --- /dev/null +++ b/getVecs.m @@ -0,0 +1,26 @@ +function getVecs(dirname) + indir = strcat(strcat('~/VIPeR/',dirname),'/'); + idlist=dir(indir); + + ofile = strcat(dirname,'.txt'); + fid = fopen(ofile,'wt'); + + for i=1:length(idlist) + n = idlist(i).name; + switch n + case '.' + continue; + case '..' + continue; + otherwise + n=n(1:strfind(n,'_')-1) + end + img=im2double(imread(strcat(indir,idlist(i).name))); + I=genFeatureVec(img); + fprintf(fid,'%s ',n); + fprintf(fid,'%f ',I); + fprintf(fid,'\n'); + end + fprintf(fid,'0'); + fclose(fid); +end \ No newline at end of file diff --git a/parGetVecs.m b/parGetVecs.m new file mode 100644 index 0000000..4097f5a --- /dev/null +++ b/parGetVecs.m @@ -0,0 +1,7 @@ +parfor i=1:2 + if i==1 + getVecs('test_a'); + else + getVecs('test_b'); + end +end \ No newline at end of file diff --git a/schmidFilter.m b/schmidFilter.m new file mode 100644 index 0000000..12d4b30 --- /dev/null +++ b/schmidFilter.m @@ -0,0 +1,5 @@ +function fout = schmidFilter(t,sigma) + sz=fix(4*sigma); + [x,y] = meshgrid(-sz:sz,sz:-1:-sz); + r=sqrt(x.^2+y.^2); + fout = filterNorm(cos((2*pi*t/sigma)*r).*exp(-(r.^2)/(2*sigma^2))); -- cgit v1.2.3-70-g09d2