~bertrand-nouvel/pycvf-keypoints/trunk

« back to all changes in this revision

Viewing changes to lib/gb/make_fb.py

  • Committer: tranx
  • Date: 2010-10-01 16:56:14 UTC
  • Revision ID: tranx@havane-20101001165614-u938mdd1y1fgd0o5
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
% Demo code for matching roughly based on the procedure
 
4
% described in:
 
5
%
 
6
% "Shape Matching and Object Recognition using Low Distortion Correspondence"
 
7
% A. C. Berg, T. L. Berg, J. Malik
 
8
% CVPR 2005
 
9
%
 
10
% code Copyright 2005 Alex Berg
 
11
%
 
12
% questions -> Alex Berg aberg@cs.berkeley.edu
 
13
 
 
14
"""
 
15
import numpy
 
16
import scipy
 
17
from numpy.fft import fft,ifft
 
18
pi=numpy.pi
 
19
 
 
20
from pycvfext.image.lib import rescale
 
21
 
 
22
def imresize(img,sz,meth):
 
23
  #return rescale.Rescaler2d(sz).process(img)
 
24
  return scipy.misc.pilutil.imresize(img,sz)#,meth)
 
25
 
 
26
def imrotate(img,ang,meth1,meth2):
 
27
  return scipy.misc.pilutil.imrotate(img,ang,meth1)
 
28
 
 
29
 
 
30
from pycvfext.image.nodes.hilbert import hilbert
 
31
 
 
32
 
 
33
def  make_fb( nori=4, aspect=3, scale=4, half_support=15):
 
34
  support = 2*half_support + 1;
 
35
  sigma = scale;
 
36
  FB = numpy.zeros((nori,support,support))
 
37
 
 
38
  ## compute meshgrid
 
39
  X,Y = numpy.mgrid[0:(10*support+1),0:(10*support+1)]
 
40
  ## center it
 
41
  X = X - X.mean(axis=-1)
 
42
  Y = Y - Y.mean(axis=-1)
 
43
  
 
44
  ###
 
45
  R = numpy.sqrt(X**2 + (aspect*Y)**2); ## compute distance to center adding aspect scaling in direction Y
 
46
  ### variance = 10*scale 
 
47
  ### compute gaussian PDF
 
48
  G = (1/(10*sigma*numpy.sqrt(2*pi)))*numpy.exp(-(R**2)/(2*100*sigma**2));
 
49
  G = G/G.sum()
 
50
 
 
51
  ### compute gradient
 
52
  dx,dy=scipy.gradient(G);
 
53
 
 
54
  ### focus only dy
 
55
  a_Odd = dy;
 
56
  ## introduce the imaginary part of the hilber transform s even signal
 
57
  a_Even = numpy.imag(hilbert(a_Odd))
 
58
 
 
59
 
 
60
  ## normalize
 
61
  a_Even = a_Even - a_Even.mean()
 
62
  a_Even = a_Even / abs(a_Even).sum()
 
63
  a_Odd = a_Odd - a_Odd.mean()
 
64
  a_Odd = a_Odd / abs(a_Odd).sum()
 
65
 
 
66
 
 
67
  ## rescale to support
 
68
  a_Odd = imresize(a_Odd,[support,support],'bilinear')
 
69
  a_Even = imresize(a_Even,[support,support],'bilinear')
 
70
 
 
71
  
 
72
  ## now generate in all orientations 
 
73
  #sigma = scale
 
74
  oris=numpy.zeros((nori,))
 
75
  #print FB.shape,nori
 
76
  for i in range(nori//2):
 
77
    theta = (i)*180./nori
 
78
 
 
79
    oris[2*i] = theta
 
80
    oris[2*i+1] = theta
 
81
    F = imrotate(a_Odd,theta,'bilinear','crop')
 
82
    # normalize F
 
83
    F = F - F.mean()
 
84
    F = F / abs(F).mean()
 
85
    FB[2*i+1,:,:]=  F
 
86
 
 
87
    F = imrotate(a_Even,theta,'bilinear','crop');
 
88
    # normalize F
 
89
    F = F - F.mean()
 
90
    F = F / abs(F).mean()
 
91
    FB[2*i,:,:]=  F
 
92
 
 
93
  return FB,oris
 
94
 
 
95
 
 
96
    
 
 
b'\\ No newline at end of file'