~bertrand-nouvel/pycvf-keypoints/trunk

« back to all changes in this revision

Viewing changes to nodes/specific.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
import numpy, scipy
 
4
from pycvf.core import genericnode
 
5
 
 
6
def getpatch(I,x):
 
7
  L=16
 
8
  r=I[max(int(x[1]-L),0):int(x[1]+L),
 
9
      max(int(x[0]-L),0):int(x[0]+L),:]
 
10
  #print r.shape,x
 
11
  return r
 
12
 
 
13
class SpecificKeypoint(object):
 
14
  """
 
15
   This class allows you to combine a keypoint extractor 
 
16
   with a keypoint descriptor.
 
17
 
 
18
   Of course exploded may have been used that for, but sometimes things need to be made easier and simpler
 
19
   
 
20
   Two modes are supported : 
 
21
     The mode "c" will pass a couple made out of the image and of the coordinates of the keypoints.
 
22
     The mode "b" where a block will be cropped at the keypoint position and this block will be passed to the model
 
23
       in mode"b" the half-size of the block is defined by keypointhalfsize
 
24
       
 
25
   
 
26
   
 
27
   TODO : this maybe improved so that the element of the dataflow actually enters the dataflow
 
28
  """
 
29
  def __init__(self,keypointmodel, descriptormodel,mode="c",as_couple=True,keypointhalfsize=(10,10)):
 
30
     self.keypointmodel=keypointmodel
 
31
     self.descriptormodel=descriptormodel
 
32
     self.mode=mode
 
33
     self.as_couple=True
 
34
     self.keypointhalfsize=keypointhalfsize
 
35
  def set_model_node(self,model):
 
36
     self.model_node=model
 
37
     self.db=self.model_node.get_curdb()
 
38
     
 
39
     self.keypointmodel=(pycvf_builder(self.keypointmodel) if type(self.keypointmodel) in [str,unicode] else self.keypointmodel)
 
40
     self.keypointmodel.init("/",self.db,self)
 
41
     keypointmodelpath=self.keypointmodel.resolve_modelpath(0)
 
42
     metak= self.keypointmodel.get_features_meta().keys()
 
43
     self.keypointmodel.metainfo_curdb=self.db
 
44
     self.keypointmodelpathno=metak.index(keypointmodelpath)
 
45
     
 
46
     self.descriptormodel=(pycvf_builder(self.descriptormodel) if type(self.descriptormodel) in [str,unicode] else self.descriptormodel)
 
47
     self.descriptormodel.init("/",self.db,self)
 
48
     descriptormodelpath=self.descriptormodel.resolve_modelpath(0)
 
49
     metak= self.descriptormodel.get_features_meta().keys()
 
50
     self.descriptormodel.metainfo_curdb=self.db
 
51
     self.descriptormodelpathno=metak.index(descriptormodelpath)
 
52
 
 
53
  def on_model_destroy(self,model):
 
54
     self.model_node=None        
 
55
  def process(self,x):
 
56
     kp=self.keypointmodel.process(x,addr=self.model_node.get_curaddr())[self.keypointmodelpathno]
 
57
     #print kp
 
58
     if self.mode=='b':
 
59
       kp=filter(lambda k:(k[0]>=self.keypointhalfsize[0]) and (k[0]<(x.shape[1]-self.keypointhalfsize[0])) and (k[1]>self.keypointhalfsize[1]) and (k[1]<(x.shape[0]-self.keypointhalfsize[1])),kp)
 
60
       desc=[self.descriptormodel.process(getpatch(x,p))[self.descriptormodelpathno] for p in kp]
 
61
     elif self.mode=='c':
 
62
       desc=self.descriptormodel.process((x,kp))[self.descriptormodelpathno]   
 
63
     else:
 
64
       raise ValueError, "Invalid Mode"
 
65
     if (self.as_couple):
 
66
       return kp,desc
 
67
     else:
 
68
       return numpy.hstack([kp,desc])
 
69
 
 
70
Node=genericnode.pycvf_node_class(None,None)(SpecificKeypoint)
 
71
__call__=Node
 
 
b'\\ No newline at end of file'