Class: CVFFI::Mat

Inherits:
Object
  • Object
show all
Includes:
CvMatFunctions
Defined in:
lib/opencv-ffi-wrappers/core/mat.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CvMatFunctions

#at_f, #at_scalar, #clone, #coerce, included, #mat_size, #print, #set_f, #set_scalar, #to_CvMat, #to_Matrix, #to_ScalarMatrix, #to_Vector, #to_a, #transpose, #twin, #zero

Constructor Details

#initialize(*args) ⇒ Mat

Returns a new instance of Mat.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 160

def initialize( *args )
  a1,a2,a3 = *args
  case a1
  when Mat
    # Copy constructor
    @mat = a1.mat
  when CvMat
    @mat = a1
  else
    rows,cols,opts = a1,a2,a3
    opts ||= {}
    opts = { type: opts } if opts.is_a? Symbol
    doZero = opts[:zero] || true
    @type = opts[:type] || :CV_32F
    @mat = CVFFI::cvCreateMat( rows, cols, @type )

    mat.zero if doZero
  end

  if block_given?
  rows.times { |i|
    cols.times { |j|
      set(i,j, yield(i,j) )
    }
  }
  end

end

Instance Attribute Details

#matObject

Returns the value of attribute mat.



157
158
159
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 157

def mat
  @mat
end

#typeObject

Returns the value of attribute type.



158
159
160
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 158

def type
  @type
end

Class Method Details

.build(rows, cols, opts = {}, &blk) ⇒ Object



224
225
226
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 224

def self.build( rows, cols, opts = {}, &blk )
  Mat.new( rows, cols, opts ) { |i,j| blk.call(i,j) }
end

.eye(size, opts = {}) ⇒ Object



228
229
230
231
232
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 228

def self.eye( size, opts = {} )
  m = Mat.new( size,size,opts )
  size.times { |i| m[i,i] = 1 }
  m
end

.pass_function(s) ⇒ Object



151
152
153
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 151

def self.pass_function( s )
  class_eval "def #{s}( *args ); mat.#{s}(*args); end"
end

.rows(r, opts = {}) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 234

def self.rows( r, opts = {} )
  height = r.length
  width = r[0].length

  Mat.build( height, width, opts ) { |i,j|
    r[i][j]
  }
end

.wrap_function(s) ⇒ Object



147
148
149
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 147

def self.wrap_function( s )
  class_eval "def #{s}( *args ); Mat.new( mat.#{s}(*args) ); end"
end

Instance Method Details

#==(b) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 255

def ==(b)
  return false if width != b.width or height != b.height or type != b.type

  cmpResult = Mat.new( height, width, :CV_8U )
  # Fills cmpResults with 1 for each element which is not equal
  CVFFI::cvCmp( self, b, cmpResult, :CV_CMP_NE )

  # If results are equal, cmpResult will be all zeros
  sum = CVFFI::cvSum( cmpResult )
  sum.w == 0
end

#at(i, j) ⇒ Object Also known as: []



199
200
201
202
203
204
205
206
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 199

def at(i,j)
  if type == :CV_32F
    mat.at_f( i,j)
  else
    s = mat.at_scalar( i,j )
    s.w
  end
end

#at=(i, j, v) ⇒ Object Also known as: []=, set



189
190
191
192
193
194
195
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 189

def at=(i,j,v)
  if type == :CV_32F
  mat.set_f( i,j, v)
  else
    mat.set_scalar( i,j, CVFFI::CvScalar.new( { w: v, x: v, y: v, z: v } ) )
  end
end

#each(&blk) ⇒ Object



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

def each( &blk )
  each_with_indices { |v,i,j| blk.call( v ) }
end

#each_with_indices(&blk) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 213

def each_with_indices( &blk )
  height.times { |i|
    width.times { |j|
      blk.call at(i,j), i, j 
    }
  }
end

#l2distance(b) ⇒ Object



253
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 253

def l2distance(b); norm(b, :CV_L2 ); end

#norm(b, type = :CV_L2) ⇒ Object



250
251
252
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 250

def norm( b, type = :CV_L2 )
  CVFFI::cvNorm( self, b, type )
end

#to_ptrObject

Should impedence match Mat anywhere a CvMat * is required… TODO: Figure out how to handler passing by_value



246
247
248
# File 'lib/opencv-ffi-wrappers/core/mat.rb', line 246

def to_ptr
  @mat.pointer
end