Module: CVFFI

Extended by:
NiceFFI::Library
Defined in:
lib/opencv-ffi/core.rb,
lib/opencv-ffi/cvffi.rb,
lib/opencv-ffi/calib3d.rb,
lib/opencv-ffi/highgui.rb,
lib/opencv-ffi/imgproc.rb,
lib/opencv-ffi/version.rb,
lib/opencv-ffi-ext/sift.rb,
lib/opencv-ffi-ext/eigen.rb,
lib/opencv-ffi/core/draw.rb,
lib/opencv-ffi/core/point.rb,
lib/opencv-ffi/core/types.rb,
lib/opencv-ffi/features2d.rb,
lib/opencv-ffi-ext/opensurf.rb,
lib/opencv-ffi/core/dynamic.rb,
lib/opencv-ffi/core/library.rb,
lib/opencv-ffi/imgproc/misc.rb,
lib/opencv-ffi-wrappers/core.rb,
lib/opencv-ffi-wrappers/misc.rb,
lib/opencv-ffi-ext/vector_math.rb,
lib/opencv-ffi/core/operations.rb,
lib/opencv-ffi/features2d/star.rb,
lib/opencv-ffi/features2d/surf.rb,
lib/opencv-ffi/imgproc/library.rb,
lib/opencv-ffi-wrappers/highgui.rb,
lib/opencv-ffi-wrappers/matcher.rb,
lib/opencv-ffi-wrappers/vectors.rb,
lib/opencv-ffi/imgproc/features.rb,
lib/opencv-ffi-wrappers/core/mat.rb,
lib/opencv-ffi-wrappers/sequence.rb,
lib/opencv-ffi/imgproc/geometric.rb,
lib/opencv-ffi-ext/matcher_helper.rb,
lib/opencv-ffi-wrappers/core/rect.rb,
lib/opencv-ffi-wrappers/core/size.rb,
lib/opencv-ffi/features2d/library.rb,
lib/opencv-ffi-wrappers/core/point.rb,
lib/opencv-ffi-wrappers/features2d.rb,
lib/opencv-ffi-wrappers/core/scalar.rb,
lib/opencv-ffi-wrappers/misc/params.rb,
lib/opencv-ffi-wrappers/core/iplimage.rb,
lib/opencv-ffi-wrappers/core/misc_draw.rb,
lib/opencv-ffi-wrappers/features2d/star.rb,
lib/opencv-ffi-wrappers/features2d/surf.rb,
lib/opencv-ffi-wrappers/imgproc/features.rb,
lib/opencv-ffi-wrappers/imgproc/geometric.rb,
lib/opencv-ffi-wrappers/features2d/image_patch.rb

Defined Under Namespace

Modules: CvMatFunctions, CvPoint3DCastMethods, CvPoint3DMethods, CvPointCastMethods, CvPointMethods, CvScalarFunctions, CvSizeCastMethods, CvSizeFunctions, Eigen, ImagePatch, IplImageFunctions, OpenSURF, SIFT, STAR, SURF, VectorMath Classes: BruteForceMatcher, CvFont, CvMat, CvMemBlock, CvMemStorage, CvPoint, CvPoint2D32f, CvPoint2D64f, CvPoint3D32f, CvPoint3D64f, CvPoint3DBase, CvPointBase, CvRect, CvSURFParams, CvSURFPoint, CvScalar, CvSeq, CvSeqBlock, CvSize, CvSize2D32f, CvSize2D64f, CvSizeBase, CvStarDetectorParams, CvStarKeypoint, Float128, Float64, FloatArrayCommon, GetAffineTransformPoints, GoodFeaturesParams, IplImage, Mat, Match, MatchResult, MatchResults, MatchSet, Params, Point, Point3D, Rect, Scalar, Sequence, Size

Constant Summary collapse

CV_LOAD_IMAGE_UNCHANGED =
-1
CV_LOAD_IMAGE_GRAYSCALE =
0
CV_LOAD_IMAGE_COLOR =
1
VERSION =
"0.0.1"
CvFontDefines =
enum [:CV_FONT_HERSHEY_SIMPLEX, 0,
:CV_FONT_HERSHEY_PLAIN,
:CV_FONT_HERSHEY_DUPLEX,
:CV_FONT_HERSHEY_COMPLEX,
:CV_FONT_HERSHEY_TRIPLEX,
:CV_FONT_HERSHEY_COMPLEX_SMALL,
:CV_FONT_HERSHEY_SCRIPT_SIMPLEX,
:CV_FONT_HERSHEY_SCRIPT_COMPLEX,
:CV_FONT_NORMAL, 8,
:CV_FONT_ITALIC, 16 ]
CvMatType =
enum :cvMatType, [ :CV_8U, 0,
:CV_8UC1, 0,
:CV_8S, 
:CV_16U,
:CV_16S,
:CV_32S,
:CV_32F,
:CV_64F,
:CV_USRTYPE1,
:CV_8UC2, 8,
:CV_8SC2, 
:CV_16UC2,
:CV_16SC2,
:CV_32SC2,
:CV_32FC2,
:CV_64FC2,
:CV_8UC3, 16,
:CV_8SC3, 
:CV_16UC3,
:CV_16SC3,
:CV_32SC3,
:CV_32FC3,
:CV_64FC3 ]

Class Method Summary collapse

Class Method Details

.compute_reproj_error(match, model) ⇒ Object



15
16
17
# File 'lib/opencv-ffi-ext/matcher_helper.rb', line 15

def self.compute_reproj_error( match, model )
  computeReprojError( match, model.to_CvMat( :type => :CV_64F ) )
end

.compute_set_reproj_error(matchSet, model) ⇒ Object



19
20
21
# File 'lib/opencv-ffi-ext/matcher_helper.rb', line 19

def self.compute_set_reproj_error( matchSet, model )
  computeSetReprojError( matchSet, model.to_CvMat( :type => :CV_64F ) ) 
end

.cv_warp_flags_to_i(a) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/opencv-ffi/imgproc/geometric.rb', line 14

def self.cv_warp_flags_to_i( a )
  if @cvWarpFlags.symbols.include? a
    @cvWarpFlags[a] 
  else
    raise ::RuntimeError, "Undefined cvWarpFlags value #{a.inspect}"
  end
end

.cvGoodFeaturesToTrack(image, eig_image, temp_image, max_corners, quality_level, min_distance, mask = nil, block_size = 3, use_harris = false, k = 0.04) ⇒ Object

This version diverges from the OpenCV API because the original returns an array of Point2D32f in corners … this functions will provide is as a return value.

Parameters:

  • max_corners

    The maximum number of corners to return

Returns:

  • An array of CvPoint2D32f giving the detected corners.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opencv-ffi/imgproc/features.rb', line 23

def self.cvGoodFeaturesToTrack( image, eig_image, temp_image, max_corners, quality_level, min_distance, 
                           mask = nil, block_size = 3, use_harris = false, k = 0.04 )

  corner_count_ptr = FFI::MemoryPointer.new :int
  corner_count_ptr.write_int max_corners
  corner_ptr = FFI::MemoryPointer.new CvPoint2D32f, max_corners
  CVFFI::real_cvGoodFeaturesToTrack( image, eig_image, temp_image, corner_ptr, corner_count_ptr, quality_level, min_distance, mask, block_size, use_harris ? 1 : 0, k ) 

  corner_count = corner_count_ptr.read_int
  corners = Array.new( corner_count ) { |i|
    CvPoint2D32f.new( corner_ptr[i] )
  }
end

.cvLoadImage(fname, color = CV_LOAD_IMAGE_COLOR) ⇒ Object



21
22
23
# File 'lib/opencv-ffi/highgui.rb', line 21

def self.cvLoadImage( fname, color = CV_LOAD_IMAGE_COLOR )
  cvLoadImageFull( fname, color )
end

.cvLoadImageM(fname, color = CV_LOAD_IMAGE_COLOR) ⇒ Object



17
18
19
# File 'lib/opencv-ffi/highgui.rb', line 17

def self.cvLoadImageM( fname, color = CV_LOAD_IMAGE_COLOR )
  cvLoadImageMFull( fname, color )
end

.cvNorm(arr1, arr2 = nil, normType = :CV_L2, mask = nil) ⇒ Object



89
90
91
# File 'lib/opencv-ffi/core/operations.rb', line 89

def self.cvNorm( arr1, arr2 = nil, normType = :CV_L2, mask = nil )
  real_cvNorm( arr1, arr2, normType, mask )
end

.cvReleaseImage(iplImage) ⇒ Object



117
118
119
120
121
# File 'lib/opencv-ffi/core/operations.rb', line 117

def self.cvReleaseImage( iplImage )
  #ptr = FFI::MemoryPointer.new :pointer
  #ptr.put_pointer(0, iplImage.to_ptr )
  #cvReleaseImageReal iplImage
end

.cvSaveImage(name, ptr, params = nil) ⇒ Object



27
28
29
# File 'lib/opencv-ffi/highgui.rb', line 27

def self.cvSaveImage( name, ptr, params = nil )
  cvSaveImageFull( name, ptr, params )
end

.cvWarpAffine(src, dst, map_matrix, flags = nil, fillval = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/opencv-ffi/imgproc/geometric.rb', line 29

def self.cvWarpAffine( src, dst, map_matrix, flags = nil, fillval = nil )
  flags ||= @cvWarpFlags[:CV_INTER_LINEAR]+@cvWarpFlags[:CV_WARP_FILL_OUTLIERS]
  fillval ||= CVFFI::CvScalar.new( [0,0,0,0] )

  cvWarpAffineReal( src,dst,map_matrix,flags,fillval )
end

.draw_circle(img, point, opts = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/opencv-ffi-wrappers/core/misc_draw.rb', line 7

def self.draw_circle( img, point, opts={} )
  color = opts[:color] || CVFFI::CvScalar.new( {:w=>255, :x=>255, :y=>255, :z=>0} )
  thickness = opts[:thickness] || 5
  radius = opts[:radius] || 1

  CVFFI::cvCircle( img.to_IplImage, point.to_CvPoint, radius, color, thickness,8,0 )
end

.draw_keypoints(img, pts, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/opencv-ffi-wrappers/features2d.rb', line 6

def self.draw_keypoints( img, pts, opts = {} )
  color = opts[:color] || CVFFI::CvScalar.new( {:w=>255, :x=>255, :y=>255, :z=>0} )
  radius = opts[:radius] || 2
  fill = opts[:fill] || true

  pts.each { |kp|
    CVFFI::cvCircle( img, CVFFI::Point.new( kp ).to_CvPoint,
                    radius, color, (fill ? -1: 1), 8, 0 )
  }
  img
end

.draw_line(img, aPoint, bPoint, opts) ⇒ Object



21
22
23
24
25
26
# File 'lib/opencv-ffi-wrappers/core/misc_draw.rb', line 21

def self.draw_line( img, aPoint, bPoint, opts )
  color = opts[:color] || CVFFI::CvScalar.new( {:w=>255, :x=>255, :y=>255, :z=>0} )
  thickness = opts[:thickness] || 5

  CVFFI::cvLine( img.to_IplImage, aPoint.to_CvPoint, bPoint.to_CvPoint, color, thickness, 8, 0 )
end

.draw_point(img, point, opts = {}) ⇒ Object



15
16
17
18
# File 'lib/opencv-ffi-wrappers/core/misc_draw.rb', line 15

def self.draw_point( img, point, opts={} )
  opts[:thickness] = -1
  draw_circle( img, point, opts )
end

.get_affine_transform(src, dst) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/opencv-ffi-wrappers/imgproc/geometric.rb', line 12

def self.get_affine_transform( src, dst )
  result = CVFFI::cvCreateMat( 2, 3, :CV_32F )

  srcPts = GetAffineTransformPoints.new '\0'
  dstPts = GetAffineTransformPoints.new '\0'

  0.upto(2) { |i|
    srcPts.a[i].x = src[i].x
    srcPts.a[i].y = src[i].y
    dstPts.a[i].x = dst[i].x
    dstPts.a[i].y = dst[i].y
  }

  CVFFI::cvGetAffineTransform( srcPts, dstPts, result )
end

.goodFeaturesToTrack(image, params = GoodFeaturesParams.new) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/opencv-ffi-wrappers/imgproc/features.rb', line 20

def self.goodFeaturesToTrack( image, params = GoodFeaturesParams.new )

  # TODO, technically, cvGoodFeaturesToTrack can also take 32FC1 data as well,
  # doesn't necessarily need to be cast to 8UC1 greyscale
  img = image.to_IplImage.ensure_greyscale
  eig_image = img.twin( :IPL_DEPTH_32F )
  temp_image = eig_image.twin

  CVFFI::cvGoodFeaturesToTrack( img, eig_image, temp_image, params.max_corners, 
                               params.quality_level, params.min_distance, params.mask,
                               params.block_size, params.use_harris, params.k )
end

.matChannels(m) ⇒ Object



45
46
47
# File 'lib/opencv-ffi/core/operations.rb', line 45

def self.matChannels( m )
  (matMagicType(m) >> 3) + 1
end

.matMagicType(m) ⇒ Object

As present, the type encodes 9 bits of nChannels and 3 bits of typ



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/opencv-ffi/core/operations.rb', line 32

def self.matMagicType( m )
  type = case m.type
         when Fixnum
           m.type
         when Symbol
           CvMatType[m.type]
         else
           raise "Can't convert #{m.type} to a numberic OpenCV type"
         end

  type & 0xFFF
end

.matType(m) ⇒ Object

A bit clumsy



50
51
52
53
54
55
56
# File 'lib/opencv-ffi/core/operations.rb', line 50

def self.matType( m )
  case m.type & 0x7
  when :CV_8U; :CV_8U
  else
    assert RuntimeError, "This shouldn't happen!"
  end
end


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/opencv-ffi-wrappers/misc.rb', line 5

def self.print_matrix(m, opts={})
  opts ||= {}
  opts = { :caption=>opts } if opts.is_a? String
  puts opts[:caption] if opts[:caption]

  # Lots of potential to make this better...
  f = '%'
  case opts[:format]
  when :e, :exp, :exponential 
    f += '- 10.5e'
  else
    f += '- 10.5f'
  end

  nChannels = CVFFI::matChannels(m)
  #  puts "#{nChannels} channel data"

  m.height.times { |i|
    m.width.times {|j|
      scalar = CVFFI::cvGet2D( m, i, j )
      case nChannels
      when 1
        printf "#{f}  ", scalar.w
      when 2
        printf "[#{f} #{f}] ", scalar.w, scalar.x
      when 3
        printf "[#{f} #{f} #{f}] ", scalar.w, scalar.x, scalar.y
      when 4
        printf "[#{f} #{f} #{f} #{f}] ", scalar.w, scalar.x, scalar.y, scalar.z
      end
    }
    puts
  }

end

.put_text(img, text, point, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/opencv-ffi-wrappers/core/misc_draw.rb', line 28

def self.put_text( img, text, point, opts = {} )
  color = opts[:color] || CVFFI::Scalar.new( 255,255,255,0 )
  thickness = opts[:thickness] || 2
  face = opts[:face] || opts[:typeface] || :CV_FONT_HERSHEY_SIMPLEX
  font = opts[:font] || nil
  hscale = opts[:hscale] || opts[:scale] || 1.0
  vscale = opts[:vscale] || opts[:scale] || hscale
  shear = opts[:shear] || 0.0

  unless font
    font = CVFFI::CvFont.new '\0'
    CVFFI::cvInitFont( font, face, hscale, vscale, shear, thickness, :CV_AA )
  end

  CVFFI::cvPutText( img.to_IplImage, text, point.to_CvPoint, font, color.to_CvScalar )
end

.save_image(fname, img) ⇒ Object



7
8
9
# File 'lib/opencv-ffi-wrappers/highgui.rb', line 7

def self.save_image( fname, img )
  CVFFI::cvSaveImage( fname, img.to_IplImage )
end

.solveCubic(coeffs) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/opencv-ffi-wrappers/core.rb', line 16

def self.solveCubic( coeffs )
  roots = CVFFI::CvMat.new CVFFI::cvCreateMat( 1,3, :CV_32F )

  CVFFI::cvSolveCubic( coeffs.to_CvMat, roots )

  roots.coerce( coeffs )[1]
end

.warp_affine(src, dst, mat, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/opencv-ffi-wrappers/imgproc/geometric.rb', line 28

def self.warp_affine( src, dst, mat, opts={} )
  flags = opts[:flags] || [ :CV_INTER_LINEAR, :CV_WARP_FILL_OUTLIERS ]
  flags << :CV_WARP_INVERSE_MAP if opts[:inverse]

  flags = flags.inject(0) { |x,i| x + cv_warp_flags_to_i( i ) }
  CVFFI::cvWarpAffine( src.to_IplImage, dst, mat,  flags,
                      CVFFI::CvScalar.new( [ 0.0, 0.0, 0.0, 0.0 ] ) )


  dst
end