258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/opencv-ffi-wrappers/matcher.rb', line 258
def self.bestMatch( train, query, opts = {})
maxDistance = opts[:max_distance] || nil
k = opts[:k] || nil
results = MatchResults.new( train, query )
query.each_with_index { |q,qidx|
this_result = []
train.each_with_index { |t,tidx|
distance = block_given? ? yield( t,q ) : t.distance_to(q)
if maxDistance
if distance < maxDistance
this_result << MatchResult.new( tidx, qidx, distance )
end
elsif k
if this_result.length < k
this_result << MatchResult.new( tidx, qidx, distance )
else
this_result.sort! { |a,b| a.distance <=> b.distance }
if distance < this_result[0].distance
this_result[0] = MatchResult.new( tidx, qidx, distance )
end
end
else
if this_result.length == 0 or distance < this_result[0].distance
this_result = [ MatchResult.new( tidx, qidx, distance ) ]
end
end
}
this_result.each { |r| results.add_result( r ) }
}
results
end
|