Module: CVFFI::ImagePatch

Defined in:
lib/opencv-ffi-wrappers/features2d/image_patch.rb

Defined Under Namespace

Classes: CircularMask, Mask, Params, Result, ResultsArray, SquareMask

Class Method Summary collapse

Class Method Details

.describe(img, keypoints, params) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/opencv-ffi-wrappers/features2d/image_patch.rb', line 217

def self.describe( img, keypoints, params )
  img = img.to_IplImage.ensure_greyscale
  preOriented = false

  half_size = (params.size/2).floor

  results = ResultsArray.new( params )
  mask = results.mask

  puts "Extracting #{keypoints.length} keypoints"
  keypoints.each_with_index { |kp,idx|
    next if kp.x < half_size or 
    kp.y < half_size or
    (img.width - kp.x) <= half_size or
    (img.height - kp.y) <= half_size

    angle = 0.0
    rect = Rect.new( [ kp.x-half_size, kp.y-half_size, params.size, params.size ] )
    CVFFI::cvSetImageROI( img, rect.to_CvRect )
    #
    ## Simple single-channel implementation
    #  Patch is row-major (i == row == y, j = column == x)
    patch = Array.new( params.size ) { |i|
      Array.new( params.size ) { |j|
         mask.valid?(i,j) ?  CVFFI::cvGetReal2D( img, i,j ) : 0.0
      }
    }
    CVFFI::cvResetImageROI( img )

    if params.oriented  == true
      # Calculate covariance matrix by Yingen Xiong
      patch_sum = mxi = mxj = 0.0
      patch.each_index { |i|
        patch[i].each_index { |j|
          if mask.valid?(i,j)
            patch_sum += patch[i][j]
            mxi += i*patch[i][j]
            mxj += j*patch[i][j]
          end
        }
      }
      mxi /= patch_sum
      mxj /= patch_sum

    #  puts "Medoids = " + [mxi,mxj].join(',')

      c11 = c12 = c22 = 0.0
      patch.each_index { |i|
        patch[i].each_index { |j|
          if mask.valid?(i,j)
            c11 += patch[i][j] * (i-mxi)*(i-mxi)
            c12 += patch[i][j] * (i-mxi)*(j-mxj)
            c22 += patch[i][j] * (j-mxj)*(j-mxj)
          end
        }
      }

      c = Matrix.rows( [ [c11,c12],[c12,c22] ] )
      d,v = CVFFI::Eigen.eigen( c )

      d = d.to_a
      i = if d[0] == d[1]
            # Equal eigenvalues
            0
          else
            d.find_index( d.max )
          end

      vec = v.to_Matrix.column_vectors[i]

      # Eigenvector corresponding to larger eignvector defines orientation
      #  However it's in i-j space, which is OpenCV (y-down, x-right)
      #  Subtract from 2PI to put in mathematical (x-right, y-up) space
      angle = 2*Math::PI - Math::atan2( vec[0],vec[1] )
      angle %= 2*Math::PI
      #puts "Computed angle #{angle * 180.0/Math::PI}"

        ## Pre-orient patch
       # puts "Pre-orienting patch"
        rotmat = CVFFI::CvMat.new CVFFI::cvCreateMat( 2,3, :CV_32F )
        CVFFI::cv2DRotationMatrix( kp.to_CvPoint2D32f, -angle*180.0/Math::PI, 1.0, rotmat )

        dstimg = img.twin
        CVFFI::cvWarpAffine( img, dstimg, rotmat )
        CVFFI::cvSetImageROI( dstimg, rect.to_CvRect )
        patch = Array.new( params.size ) { |i|
          Array.new( params.size ) { |j|
            mask.valid?(i,j) ?  CVFFI::cvGetReal2D( dstimg, i,j ) : 0.0
          }
        }
        CVFFI::cvResetImageROI( dstimg )
        CVFFI::cvReleaseImage( dstimg )
        preOriented = true

        GC.start if (idx % 5) == 0 

    end

    results << Result.new( kp, patch, angle, preOriented )
  }

  results
end