Class: CVFFI::MatchResults

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/opencv-ffi-wrappers/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#inject_with_index

Constructor Details

#initialize(tset, qset) ⇒ MatchResults

Returns a new instance of MatchResults.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 62

def initialize( tset, qset )
  @train_set = tset
  @query_set = qset

  @results = []
  @match_cache = []

  @by_train = []
  @by_train.extend( AddMapWithIndex )

  @by_query = []

  @mask = []
end

Instance Attribute Details

#query_setObject

Returns the value of attribute query_set.



59
60
61
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 59

def query_set
  @query_set
end

#resultsObject

Returns the value of attribute results.



60
61
62
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 60

def results
  @results
end

#train_setObject

Returns the value of attribute train_set.



59
60
61
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 59

def train_set
  @train_set
end

Instance Method Details

#[](i) ⇒ Object Also known as: element



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 113

def [](i)
  if @match_cache[i]
    @match_cache[i]
  else
    m=Match.new( '\0' )
    m.distance = @results[i].distance
   
    # This is a bit awkward with nested FFI structs
    m.train.x = @train_set[@results[i].tidx].x
    m.train.y = @train_set[@results[i].tidx].y
    m.query.x = @query_set[@results[i].qidx].x
    m.query.y = @query_set[@results[i].qidx].y
    @match_cache[i] = m
    m
  end
end

#add(tidx, qidx, dist) ⇒ Object



86
87
88
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 86

def add( tidx, qidx, dist )
  add_result MatchResult.new( tidx, qidx, dist )
end

#add_result(r) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 77

def add_result(r)
raise RuntimeError "index greater than size of training set (#{r.train_idx} > #{train_set.size})" if r.train_idx >= train_set.size
raise RuntimeError "index greater than size of query set (#{r.query_idx} > #{query_set.size})" if r.query_idx >= query_set.size
  @results << r
  (@by_train[r.train_idx] ||= Array.new) << r
  (@by_query[r.query_idx] ||= Array.new) << r
  r
end

#clear_mask(i) ⇒ Object



200
201
202
203
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 200

def clear_mask(i)
  invalidate_match_set
  @mask = []
end

#each(include_masked = false, &blk) ⇒ Object

The “public” each yields a Match to the block



157
158
159
160
161
162
163
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 157

def each( include_masked = false, &blk )
  @results.length.times { |i|
    if !include_masked and !masked?(i)
      blk.yield( element(i) )
    end
  }
end

#get_maskObject



205
206
207
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 205

def get_mask
  @mask
end

#invalidate_match_setObject



154
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 154

def invalidate_match_set; @match_set = nil; end

#length(include_masked = false) ⇒ Object Also known as: size



103
104
105
106
107
108
109
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 103

def length( include_masked = false )
  if( include_masked )
  @results.length
  else
    num_unmasked
  end
end

#mask(i) ⇒ Object



190
191
192
193
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 190

def mask( i )
  invalidate_match_set
  @mask[i] = true
end

#masked?(i) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 186

def masked?( i )
  @mask[i] == true
end

#match_setObject

Experiment



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 132

def match_set
  if @match_set
    @match_set
  else
  m = FFI::MemoryPointer.new( Match, length )
  results_each_with_index { |r,i|
    match = Match.new( m + i*Match.size )
    match.distance = r.distance
   
    # This is a bit awkward with nested FFI structs
    match.train.x = @train_set[r.tidx].x
    match.train.y = @train_set[r.tidx].y
    match.query.x = @query_set[r.qidx].x
    match.query.y = @query_set[r.qidx].y
  }
  ms = MatchSet.new( :length => length,
                     :d => m,
                     :err => FFI::MemoryPointer.new( :double, length ) )
  @match_set = ms
  end
end

#merge_mask(m) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 214

def merge_mask(m)
  invalidate_match_set
  old_mask = @mask
  @mask = Array.new( [old_mask.length, m.length].max ) { |i|
    old_mask[i] or m[i]
  }
end

#num_maskedObject



178
179
180
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 178

def num_masked
  @mask.inject(0) { |m,s| s ? m+1 : m }
end

#num_unmaskedObject



182
183
184
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 182

def num_unmasked
  @results.length - num_masked
end

#results_each(include_masked = false, &blk) ⇒ Object



174
175
176
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 174

def results_each( include_masked = false, &blk )
  results_each_with_index { |r,i| blk.yield(r) }
end

#results_each_with_index(include_masked = false, &blk) ⇒ Object

the “private” each yelds a MatchResult to the block



166
167
168
169
170
171
172
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 166

def results_each_with_index( include_masked = false, &blk )
  @results.each_with_index { |r,i|
    if !include_masked and !masked?(i)
      blk.yield( r,i )
    end
  }
end

#set_mask(m) ⇒ Object



209
210
211
212
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 209

def set_mask(m)
  invalidate_match_set
  @mask = m
end

#to_aObject



97
98
99
100
101
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 97

def to_a
  Array.new( size ) { |i|
    [ @results[i].tidx, @results[i].qidx, @results[i].dist ]
  }
end

#to_CvMats(include_masked = false) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 235

def to_CvMats( include_masked = false )
  pointsOne = CVFFI::cvCreateMat( num_unmasked, 2, :CV_32F )
  pointsTwo = CVFFI::cvCreateMat( num_unmasked, 2, :CV_32F )

  results_each_with_index( include_masked ) { |r,i|
      CVFFI::cvSetReal2D( pointsOne, i, 0, @train_set[r.tidx].x )
      CVFFI::cvSetReal2D( pointsOne, i, 1, @train_set[r.tidx].y )
      CVFFI::cvSetReal2D( pointsTwo, i, 0, @query_set[r.qidx].x )
      CVFFI::cvSetReal2D( pointsTwo, i, 1, @query_set[r.qidx].y )
  }

  [pointsOne, pointsTwo]
end

#to_Points(include_masked = false) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 222

def to_Points( include_masked = false )
  pointsOne = []
  pointsTwo = []

  results_each( include_masked ) { |r|
      pointsOne << @train_set[r.tidx].to_Point
      pointsTwo << @query_set[r.qidx].to_Point
  }

  [pointsOne, pointsTwo]
end

#to_sObject



90
91
92
93
94
95
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 90

def to_s
  [ "Total matches: #{@results.size}" ] + 
  @by_train.map_with_index { |r,tidx|
     "Tidx #{tidx} (#{r.nil? ? 0 : r.length}): #{r.nil? ? "" : r.map {|r| sprintf "%d(%.2e)", r.query_idx, r.dist }.join(' ') }"
  }
end

#unmask(i) ⇒ Object



195
196
197
198
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 195

def unmask(i)
  invalidate_match_set
  @mask[i] = false
end