2010-12-31 03:29:31 -05:00
|
|
|
#!/usr/bin/env ruby
|
2011-01-01 07:10:17 -05:00
|
|
|
# -*- mode: ruby; coding: utf-8-unix -*-
|
2010-12-31 03:29:31 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'opencv'
|
|
|
|
require 'pp'
|
2011-01-01 13:04:27 -05:00
|
|
|
require File.expand_path(File.dirname(__FILE__)) + '/helper'
|
2010-12-31 03:29:31 -05:00
|
|
|
|
|
|
|
include OpenCV
|
|
|
|
|
2010-12-31 03:32:28 -05:00
|
|
|
# Tests for OpenCV::CvMat
|
2011-01-01 13:04:27 -05:00
|
|
|
class TestCvMat < OpenCVTestCase
|
2011-01-03 01:46:21 -05:00
|
|
|
def test_initialize
|
|
|
|
m = CvMat.new(10, 20)
|
|
|
|
assert_equal(10, m.rows)
|
|
|
|
assert_equal(20, m.cols)
|
|
|
|
assert_equal(:cv8u, m.depth)
|
|
|
|
assert_equal(3, m.channel)
|
2011-01-05 11:16:10 -05:00
|
|
|
|
|
|
|
depth_table = {
|
|
|
|
CV_8U => :cv8u,
|
|
|
|
CV_8S => :cv8s,
|
|
|
|
CV_16U => :cv16u,
|
|
|
|
CV_16S => :cv16s,
|
|
|
|
CV_32S => :cv32s,
|
|
|
|
CV_32F => :cv32f,
|
|
|
|
CV_64F => :cv64f
|
|
|
|
}
|
|
|
|
|
|
|
|
[CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F,
|
|
|
|
:cv8u, :cv8s, :cv16u, :cv16s, :cv32s, :cv32f, :cv64f].each { |depth|
|
|
|
|
[1, 2, 3, 4].each { |ch|
|
|
|
|
m = CvMat.new(10, 20, depth, ch)
|
|
|
|
assert_equal(10, m.rows)
|
|
|
|
assert_equal(20, m.cols)
|
|
|
|
depth = depth_table[depth] unless depth.is_a? Symbol
|
|
|
|
assert_equal(depth, m.depth)
|
|
|
|
assert_equal(ch, m.channel)
|
|
|
|
}
|
|
|
|
}
|
2011-01-03 01:46:21 -05:00
|
|
|
end
|
|
|
|
|
2010-12-31 04:29:18 -05:00
|
|
|
def test_DRAWING_OPTION
|
|
|
|
CvMat::DRAWING_OPTION[:color].to_ary.each { |c|
|
|
|
|
assert_in_delta(0, c, 0.01)
|
|
|
|
}
|
|
|
|
assert_equal(1, CvMat::DRAWING_OPTION[:thickness])
|
|
|
|
assert_equal(8, CvMat::DRAWING_OPTION[:line_type])
|
|
|
|
assert_equal(0, CvMat::DRAWING_OPTION[:shift])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_GOOD_FEATURES_TO_TRACK_OPTION
|
|
|
|
assert_equal(0xff, CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:max])
|
|
|
|
assert_nil(CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:mask])
|
|
|
|
assert_equal(3, CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:block_size])
|
|
|
|
assert((not CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:use_harris]))
|
|
|
|
assert_in_delta(0.04, CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:k], 0.01)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_FLOOD_FILL_OPTION
|
|
|
|
assert_equal(4, CvMat::FLOOD_FILL_OPTION[:connectivity])
|
|
|
|
assert((not CvMat::FLOOD_FILL_OPTION[:fixed_range]))
|
|
|
|
assert((not CvMat::FLOOD_FILL_OPTION[:mask_only]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_FIND_CONTOURS_OPTION
|
|
|
|
assert_equal(1, CvMat::FIND_CONTOURS_OPTION[:mode])
|
|
|
|
assert_equal(2, CvMat::FIND_CONTOURS_OPTION[:method])
|
|
|
|
assert_equal(0, CvMat::FIND_CONTOURS_OPTION[:offset].x)
|
|
|
|
assert_equal(0, CvMat::FIND_CONTOURS_OPTION[:offset].y)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_OPTICAL_FLOW_HS_OPTION
|
|
|
|
assert_in_delta(0.0005, CvMat::OPTICAL_FLOW_HS_OPTION[:lambda], 0.000001)
|
|
|
|
assert_equal(1, CvMat::OPTICAL_FLOW_HS_OPTION[:criteria].max)
|
|
|
|
assert_in_delta(0.001, CvMat::OPTICAL_FLOW_HS_OPTION[:criteria].eps, 0.00001)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_OPTICAL_FLOW_BM_OPTION
|
|
|
|
assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:block_size].width)
|
|
|
|
assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:block_size].height)
|
|
|
|
assert_equal(1, CvMat::OPTICAL_FLOW_BM_OPTION[:shift_size].width)
|
|
|
|
assert_equal(1, CvMat::OPTICAL_FLOW_BM_OPTION[:shift_size].height)
|
|
|
|
assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:max_range].width)
|
|
|
|
assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:max_range].height)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_FIND_FUNDAMENTAL_MAT_OPTION
|
|
|
|
assert((not CvMat::FIND_FUNDAMENTAL_MAT_OPTION[:with_status]))
|
|
|
|
assert_in_delta(1.0, CvMat::FIND_FUNDAMENTAL_MAT_OPTION[:maximum_distance], 0.01)
|
|
|
|
assert_in_delta(0.99, CvMat::FIND_FUNDAMENTAL_MAT_OPTION[:desirable_level], 0.01)
|
|
|
|
end
|
|
|
|
|
2010-12-31 07:34:36 -05:00
|
|
|
def test_to_s
|
|
|
|
m = CvMat.new(10, 20)
|
|
|
|
assert_equal('<OpenCV::CvMat:20x10,depth=cv8u,channel=3>', m.to_s)
|
|
|
|
m = CvMat.new(10, 20, :cv16s)
|
|
|
|
assert_equal('<OpenCV::CvMat:20x10,depth=cv16s,channel=3>', m.to_s)
|
|
|
|
m = CvMat.new(10, 20, :cv32f, 1)
|
|
|
|
assert_equal('<OpenCV::CvMat:20x10,depth=cv32f,channel=1>', m.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parent
|
|
|
|
m1 = CvMat.new(10, 20)
|
|
|
|
assert((not m1.has_parent?))
|
|
|
|
assert_nil(m1.parent)
|
|
|
|
|
2010-12-31 14:37:40 -05:00
|
|
|
flunk('FIXME: resolve unexpected ABORT of CvMat#to_CvMat')
|
2011-01-01 18:46:08 -05:00
|
|
|
m2 = m1.to_CvMat
|
|
|
|
assert(m2.has_parent?)
|
|
|
|
assert_same(m1, m2.parent)
|
2010-12-31 07:34:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inside
|
|
|
|
m = CvMat.new(20, 10)
|
|
|
|
assert(m.inside? CvPoint.new(0, 0))
|
|
|
|
assert(m.inside? CvPoint.new(9, 19))
|
|
|
|
assert((not m.inside? CvPoint.new(10, 0)))
|
|
|
|
assert((not m.inside? CvPoint.new(0, 20)))
|
|
|
|
assert((not m.inside? CvPoint.new(10, 20)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_IplConvKernel
|
|
|
|
kernel = CvMat.new(10, 20).to_IplConvKernel(CvPoint.new(2, 3))
|
|
|
|
assert_equal(10, kernel.rows)
|
|
|
|
assert_equal(20, kernel.cols)
|
|
|
|
assert_equal(2, kernel.anchor.x)
|
|
|
|
assert_equal(3, kernel.anchor.y)
|
|
|
|
assert_equal(2, kernel.anchor_x)
|
|
|
|
assert_equal(3, kernel.anchor_y)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_mask
|
|
|
|
mask = CvMat.new(10, 20).create_mask
|
|
|
|
assert_equal(20, mask.width)
|
|
|
|
assert_equal(10, mask.height)
|
|
|
|
assert_equal(:cv8u, mask.depth)
|
|
|
|
assert_equal(1, mask.channel)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fields
|
|
|
|
m = CvMat.new(20, 10)
|
|
|
|
assert_equal(10, m.width)
|
|
|
|
assert_equal(10, m.columns)
|
|
|
|
assert_equal(10, m.cols)
|
|
|
|
assert_equal(20, m.height)
|
|
|
|
assert_equal(20, m.rows)
|
|
|
|
assert_equal(:cv8u, m.depth)
|
|
|
|
assert_equal(3, m.channel)
|
|
|
|
|
|
|
|
m = CvMat.new(20, 10, :cv16s, 1)
|
|
|
|
assert_equal(10, m.width)
|
|
|
|
assert_equal(10, m.columns)
|
|
|
|
assert_equal(10, m.cols)
|
|
|
|
assert_equal(20, m.height)
|
|
|
|
assert_equal(20, m.rows)
|
|
|
|
assert_equal(:cv16s, m.depth)
|
|
|
|
assert_equal(1, m.channel)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clone
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(10, 20)
|
2010-12-31 07:34:36 -05:00
|
|
|
m2 = m1.clone
|
|
|
|
assert_equal(m1.data, m2.data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_copy
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(10, 20, CV_32F, 1)
|
2010-12-31 07:34:36 -05:00
|
|
|
|
|
|
|
m2 = m1.copy
|
|
|
|
assert_equal(m1.data, m2.data)
|
|
|
|
|
|
|
|
m2 = CvMat.new(10, 20, CV_32F, 1)
|
|
|
|
m1.copy(m2)
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_equal(m1.data, m2.data)
|
|
|
|
|
|
|
|
a = m1.copy(2)
|
|
|
|
assert_equal(2, a.size)
|
2011-01-01 16:25:38 -05:00
|
|
|
|
2011-01-01 13:04:27 -05:00
|
|
|
a.each { |m|
|
2011-01-01 16:25:38 -05:00
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
2011-01-01 13:04:27 -05:00
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[j, i], m[j, i])
|
2011-01-01 13:04:27 -05:00
|
|
|
}
|
2010-12-31 07:34:36 -05:00
|
|
|
}
|
|
|
|
}
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_nil(m1.copy(-1))
|
2010-12-31 07:34:36 -05:00
|
|
|
|
2010-12-31 14:37:40 -05:00
|
|
|
flunk('FIXME: CvUnmatchedSizes and CvUnmatchedFormats are not implemented yet')
|
2010-12-31 07:34:36 -05:00
|
|
|
m2 = CvMat.new(1, 2, CV_32F, 1)
|
|
|
|
assert_raise(CvUnmatchedSizes) {
|
|
|
|
m1.copy(m2)
|
|
|
|
}
|
|
|
|
|
|
|
|
m2 = CvMat.new(10, 20, CV_32F, 3)
|
|
|
|
assert_raise(CvUnmatchedFormats) {
|
|
|
|
m1.copy(m2)
|
|
|
|
}
|
|
|
|
m2 = CvMat.new(10, 20, CV_8U, 1)
|
|
|
|
assert_raise(CvUnmatchedFormats) {
|
|
|
|
m1.copy(m2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_convert_depth
|
|
|
|
m = CvMat.new(10, 20, :cv32f)
|
|
|
|
assert_equal(:cv8u, m.to_8u.depth)
|
|
|
|
assert_equal(:cv8s, m.to_8s.depth)
|
|
|
|
assert_equal(:cv16u, m.to_16u.depth)
|
|
|
|
assert_equal(:cv16s, m.to_16s.depth)
|
|
|
|
assert_equal(:cv32s, m.to_32s.depth)
|
|
|
|
assert_equal(:cv32f, m.to_32f.depth)
|
|
|
|
assert_equal(:cv64f, m.to_64f.depth)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_vector
|
|
|
|
m = CvMat.new(1, 2)
|
|
|
|
assert(m.vector?)
|
|
|
|
|
|
|
|
m = CvMat.new(2, 2)
|
|
|
|
assert((not m.vector?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_square
|
|
|
|
m = CvMat.new(2, 2)
|
|
|
|
assert(m.square?)
|
|
|
|
m = CvMat.new(1, 2)
|
|
|
|
assert((not m.square?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_CvMat
|
2011-01-01 18:46:08 -05:00
|
|
|
m1 = CvMat.new(2, 2)
|
2010-12-31 14:37:40 -05:00
|
|
|
flunk('FIXME: resolve unexpected ABORT of CvMat#to_CvMat')
|
2011-01-01 18:46:08 -05:00
|
|
|
m2 = m1.to_CvMat
|
|
|
|
assert_same(m1, m2.parent)
|
2010-12-31 07:34:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sub_rect
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(10, 10)
|
2010-12-31 07:34:36 -05:00
|
|
|
|
|
|
|
assert_raise(ArgumentError) {
|
|
|
|
m1.sub_rect
|
|
|
|
}
|
|
|
|
|
|
|
|
m2 = m1.sub_rect(CvRect.new(0, 0, 2, 3))
|
|
|
|
assert_equal(2, m2.width)
|
|
|
|
assert_equal(3, m2.height)
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[j, i], m2[j, i])
|
2010-12-31 07:34:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
topleft = CvPoint.new(2, 3)
|
|
|
|
m2 = m1.sub_rect(topleft, CvSize.new(4, 5))
|
|
|
|
assert_equal(4, m2.width)
|
|
|
|
assert_equal(5, m2.height)
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[topleft.y + j, topleft.x + i], m2[j, i])
|
2010-12-31 07:34:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
topleft = CvPoint.new(1, 2)
|
|
|
|
m2 = m1.sub_rect(topleft.x, topleft.y, 3, 4)
|
|
|
|
assert_equal(3, m2.width)
|
|
|
|
assert_equal(4, m2.height)
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[topleft.y + j, topleft.x + i], m2[j, i])
|
2010-12-31 07:34:36 -05:00
|
|
|
}
|
|
|
|
}
|
2010-12-31 14:37:40 -05:00
|
|
|
|
|
|
|
# Alias
|
|
|
|
m2 = m1.subrect(CvRect.new(0, 0, 2, 3))
|
|
|
|
assert_equal(2, m2.width)
|
|
|
|
assert_equal(3, m2.height)
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[j, i], m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
2010-12-31 07:34:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_slice_width
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(10, 40, :cv32f, 1)
|
2010-12-31 07:34:36 -05:00
|
|
|
ml, mr = m1.slice_width(2)
|
|
|
|
[ml, mr].each { |m|
|
|
|
|
assert_equal(10, m.height)
|
|
|
|
assert_equal(20, m.width)
|
|
|
|
assert_equal(:cv32f, m.depth)
|
|
|
|
assert_equal(1, m.channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
ml.height.times { |j|
|
|
|
|
ml.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[j, i], ml[j, i])
|
|
|
|
assert_cvscalar_equal(m1[j, (m1.width / 2) + i], mr[j, i])
|
2010-12-31 07:34:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_slice_height
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(10, 20, :cv32f, 1)
|
2010-12-31 07:34:36 -05:00
|
|
|
mt, mb = m1.slice_height(2)
|
|
|
|
[mt, mb].each { |m|
|
|
|
|
assert_equal(5, m.height)
|
|
|
|
assert_equal(20, m.width)
|
|
|
|
assert_equal(:cv32f, m.depth)
|
|
|
|
assert_equal(1, m.channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
mt.height.times { |j|
|
|
|
|
mt.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[j, i], mt[j, i])
|
|
|
|
assert_cvscalar_equal(m1[(m1.height / 2) + j, i], mb[j, i])
|
2010-12-31 07:34:36 -05:00
|
|
|
}
|
2010-12-31 03:29:31 -05:00
|
|
|
}
|
|
|
|
end
|
2010-12-31 07:34:36 -05:00
|
|
|
|
2010-12-31 07:47:15 -05:00
|
|
|
def test_row
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(10, 20)
|
2010-12-31 07:47:15 -05:00
|
|
|
|
|
|
|
m2 = m1.row(2)
|
|
|
|
assert_equal(1, m2.height)
|
|
|
|
assert_equal(m1.width, m2.width)
|
|
|
|
m1.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[2, i], m2[i])
|
2010-12-31 07:47:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m2, m3 = m1.row(1, 2)
|
|
|
|
[m2, m3].each { |m|
|
|
|
|
assert_equal(1, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
}
|
|
|
|
m1.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[1, i], m2[i])
|
|
|
|
assert_cvscalar_equal(m1[2, i], m3[i])
|
2010-12-31 07:47:15 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_col
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(10, 20)
|
2010-12-31 07:47:15 -05:00
|
|
|
|
|
|
|
m2 = m1.col(2)
|
|
|
|
assert_equal(1, m2.width)
|
|
|
|
assert_equal(m1.height, m2.height)
|
|
|
|
m1.height.times { |j|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[j, 2], m2[j])
|
2010-12-31 07:47:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m2, m3 = m1.col(1, 2)
|
|
|
|
[m2, m3].each { |m|
|
|
|
|
assert_equal(1, m.width)
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
}
|
|
|
|
m1.height.times { |j|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m1[j, 1], m2[j])
|
|
|
|
assert_cvscalar_equal(m1[j, 2], m3[j])
|
2010-12-31 07:47:15 -05:00
|
|
|
}
|
|
|
|
end
|
2010-12-31 14:37:40 -05:00
|
|
|
|
|
|
|
def test_each_row
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(2, 3)
|
2010-12-31 14:37:40 -05:00
|
|
|
a = [[1, 2, 3], [4, 5, 6]]
|
|
|
|
a.map! { |a1|
|
|
|
|
a1.map! { |a2|
|
|
|
|
CvScalar.new(a2, a2, a2, a2).to_ary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0
|
|
|
|
m1.each_row { |r|
|
|
|
|
a[j].size.times { |i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(a[j][i], r[i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
j += 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_col
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(2, 3)
|
2010-12-31 14:37:40 -05:00
|
|
|
a = [[1, 4], [2, 5], [3, 6]]
|
|
|
|
a.map! { |a1|
|
|
|
|
a1.map! { |a2|
|
|
|
|
CvScalar.new(a2, a2, a2, a2).to_ary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0
|
|
|
|
m1.each_col { |c|
|
|
|
|
a[j].size.times { |i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(a[j][i], c[i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
j += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
j = 0
|
|
|
|
m1.each_column { |c|
|
|
|
|
a[j].size.times { |i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(a[j][i], c[i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
j += 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_diag
|
2011-01-01 13:04:27 -05:00
|
|
|
m = create_cvmat(5, 5)
|
2010-12-31 14:37:40 -05:00
|
|
|
a = [1, 7, 13, 19, 25].map { |x| CvScalar.new(x, x, x, x) }
|
|
|
|
d = m.diag
|
2011-01-01 03:25:42 -05:00
|
|
|
|
2010-12-31 14:37:40 -05:00
|
|
|
a.each_with_index { |s, i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(s, d[i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
a = [2, 8, 14, 20].map { |x| CvScalar.new(x, x, x, x) }
|
|
|
|
d = m.diag(1)
|
|
|
|
a.each_with_index { |s, i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(s, d[i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
a = [6, 12, 18, 24].map { |x| CvScalar.new(x, x, x, x) }
|
|
|
|
d = m.diag(-1)
|
|
|
|
a.each_with_index { |s, i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(s, d[i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
a = [1, 7, 13, 19, 25].map { |x| CvScalar.new(x, x, x, x) }
|
|
|
|
d = m.diagonal
|
|
|
|
a.each_with_index { |s, i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(s, d[i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size
|
|
|
|
m = CvMat.new(2, 3)
|
|
|
|
assert_equal(3, m.size.width)
|
|
|
|
assert_equal(2, m.size.height)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dims
|
|
|
|
m = CvMat.new(2, 3)
|
|
|
|
assert_equal([2, 3], m.dims)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dim_size
|
|
|
|
m = CvMat.new(2, 3)
|
|
|
|
assert_equal(2, m.dim_size(0))
|
|
|
|
assert_equal(3, m.dim_size(1))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_aref
|
2011-01-01 13:04:27 -05:00
|
|
|
m = create_cvmat(2, 3)
|
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 1, 1, 1), m[0])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(5, 5, 5, 5), m[4])
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(2, 2, 2, 2), m[0, 1])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(4, 4, 4, 4), m[1, 0])
|
2011-01-05 11:57:33 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(2, 2, 2, 2), m[0, 1, 2])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(4, 4, 4, 4), m[1, 0, 3, 4])
|
2010-12-31 14:37:40 -05:00
|
|
|
|
|
|
|
# Alias
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 1, 1, 1), m.at(0))
|
2010-12-31 14:37:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_aset
|
2011-01-01 13:04:27 -05:00
|
|
|
m = create_cvmat(2, 3)
|
2010-12-31 14:37:40 -05:00
|
|
|
m[0] = CvScalar.new(10, 10, 10, 10)
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(10, 10, 10, 10), m[0])
|
2010-12-31 14:37:40 -05:00
|
|
|
m[1, 0] = CvScalar.new(20, 20, 20, 20)
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(20, 20, 20, 20), m[1, 0])
|
2011-01-05 11:57:33 -05:00
|
|
|
m[1, 0, 2] = CvScalar.new(4, 4, 4, 4)
|
|
|
|
assert_cvscalar_equal(CvScalar.new(4, 4, 4, 4), m[1, 0])
|
|
|
|
m[1, 0, 2, 4] = CvScalar.new(5, 5, 5, 5)
|
|
|
|
assert_cvscalar_equal(CvScalar.new(5, 5, 5, 5), m[1, 0])
|
2010-12-31 14:37:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fill
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(2, 3)
|
2010-12-31 14:37:40 -05:00
|
|
|
m2 = m1.fill(CvScalar.new(1, 2, 3, 4))
|
|
|
|
m1.fill!(CvScalar.new(1, 2, 3, 4))
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(5, 5)
|
2010-12-31 14:37:40 -05:00
|
|
|
m0 = m1.clone
|
2011-01-01 07:08:01 -05:00
|
|
|
mask = CvMat.new(m1.height, m1.width, :cv8u, 1).clear
|
2010-12-31 14:37:40 -05:00
|
|
|
2.times { |j|
|
|
|
|
2.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
mask[j, i] = CvScalar.new(1, 1, 1, 1)
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m2 = m1.fill(CvScalar.new(1, 2, 3, 4), mask)
|
|
|
|
m1.fill!(CvScalar.new(1, 2, 3, 4), mask)
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
|
|
|
if i < 2 and j < 2
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
else
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m0[j, i], m1[j, i])
|
|
|
|
assert_cvscalar_equal(m0[j, i], m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2011-01-01 07:08:01 -05:00
|
|
|
|
2010-12-31 14:37:40 -05:00
|
|
|
# Alias
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(2, 3)
|
2010-12-31 14:37:40 -05:00
|
|
|
m2 = m1.set(CvScalar.new(1, 2, 3, 4))
|
|
|
|
m1.set!(CvScalar.new(1, 2, 3, 4))
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(5, 5)
|
2010-12-31 14:37:40 -05:00
|
|
|
m0 = m1.clone
|
2011-01-01 07:08:01 -05:00
|
|
|
mask = CvMat.new(m1.height, m1.width, CV_8U, 1).clear
|
2010-12-31 14:37:40 -05:00
|
|
|
2.times { |j|
|
|
|
|
2.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
mask[j, i] = CvScalar.new(1, 1, 1, 1)
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m2 = m1.set(CvScalar.new(1, 2, 3, 4), mask)
|
|
|
|
m1.set!(CvScalar.new(1, 2, 3, 4), mask)
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
|
|
|
if i < 2 and j < 2
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
else
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m0[j, i], m1[j, i])
|
|
|
|
assert_cvscalar_equal(m0[j, i], m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(2, 3)
|
2010-12-31 14:37:40 -05:00
|
|
|
m2 = m1.clear
|
|
|
|
m1.clear!
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(2, 3)
|
2010-12-31 14:37:40 -05:00
|
|
|
m2 = m1.set_zero
|
|
|
|
m1.set_zero!
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_identity
|
2011-01-01 13:04:27 -05:00
|
|
|
m1 = create_cvmat(5, 5)
|
2010-12-31 14:37:40 -05:00
|
|
|
m2 = m1.identity
|
|
|
|
m1.identity!
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
|
|
|
if i == j
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 0, 0, 0), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 0, 0, 0), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
else
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m2[j, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_range
|
2011-01-01 07:08:01 -05:00
|
|
|
m1 = CvMat.new(1, 10, CV_32S, 1)
|
|
|
|
m2 = m1.range(0, m1.cols)
|
|
|
|
m1.range!(0, m1.cols)
|
2010-12-31 14:37:40 -05:00
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(i, 0, 0, 0), m1[0, i])
|
|
|
|
assert_cvscalar_equal(CvScalar.new(i, 0, 0, 0), m2[0, i])
|
2010-12-31 14:37:40 -05:00
|
|
|
}
|
|
|
|
end
|
2010-12-31 15:45:13 -05:00
|
|
|
|
|
|
|
def test_reshape
|
2011-01-01 13:04:27 -05:00
|
|
|
m = create_cvmat(2, 3, CV_8U, 3)
|
2010-12-31 15:45:13 -05:00
|
|
|
assert_raise(TypeError) {
|
|
|
|
m.reshape(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
vec = m.reshape(:rows => 1)
|
|
|
|
assert_equal(6, vec.width)
|
|
|
|
assert_equal(1, vec.height)
|
|
|
|
size = m.width * m.height
|
|
|
|
size.times { |i|
|
2011-01-01 13:04:27 -05:00
|
|
|
assert_cvscalar_equal(m[i], vec[i])
|
2010-12-31 15:45:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ch1 = m.reshape(:channel => 1)
|
|
|
|
assert_equal(9, ch1.width)
|
2011-01-01 07:29:35 -05:00
|
|
|
assert_equal(2, ch1.height)
|
2010-12-31 15:45:13 -05:00
|
|
|
|
|
|
|
m.height.times { |j|
|
|
|
|
m.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
s1 = ch1[j, i * 3][0]
|
|
|
|
s2 = ch1[j, i * 3 + 1][0]
|
|
|
|
s3 = ch1[j, i * 3 + 2][0]
|
|
|
|
assert_cvscalar_equal(m[j, i], CvScalar.new(s1, s2, s3, 0))
|
2010-12-31 15:45:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_repeat
|
2011-01-01 17:07:02 -05:00
|
|
|
m1 = create_cvmat(2, 3, :cv8u, 3)
|
2010-12-31 15:45:13 -05:00
|
|
|
assert_raise(TypeError) {
|
|
|
|
m1.repeat(1)
|
|
|
|
}
|
2011-01-01 17:07:02 -05:00
|
|
|
m2 = CvMat.new(6, 9, :cv8u, 3)
|
2010-12-31 15:45:13 -05:00
|
|
|
m2 = m1.repeat(m2)
|
|
|
|
m2.height.times { |j|
|
|
|
|
m2.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
a = m1[j % m1.height, i % m1.width]
|
|
|
|
assert_cvscalar_equal(m2[j, i], a)
|
2010-12-31 15:45:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2011-01-01 14:06:12 -05:00
|
|
|
|
|
|
|
def test_flip
|
|
|
|
m0 = create_cvmat(2, 3)
|
|
|
|
|
|
|
|
m1 = m0.clone
|
|
|
|
m1.flip!(:x)
|
2011-01-03 01:46:21 -05:00
|
|
|
m2 = m0.flip(:x)
|
2011-01-01 14:06:12 -05:00
|
|
|
m3 = m0.clone
|
|
|
|
m3.flip!(:y)
|
2011-01-03 01:46:21 -05:00
|
|
|
m4 = m0.flip(:y)
|
2011-01-01 14:06:12 -05:00
|
|
|
m5 = m0.clone
|
|
|
|
m5.flip!(:xy)
|
2011-01-03 01:46:21 -05:00
|
|
|
m6 = m0.flip(:xy)
|
2011-01-01 14:06:12 -05:00
|
|
|
m7 = m0.clone
|
|
|
|
m7.flip!
|
2011-01-03 01:46:21 -05:00
|
|
|
m8 = m0.flip
|
2011-01-01 14:06:12 -05:00
|
|
|
|
|
|
|
[m1, m2, m3, m4, m5, m6, m7, m8].each { |m|
|
|
|
|
assert_equal(m0.height, m.height)
|
|
|
|
assert_equal(m0.width, m.width)
|
|
|
|
}
|
|
|
|
m0.height.times { |j|
|
|
|
|
m0.width.times { |i|
|
|
|
|
ri = m0.width - i - 1
|
|
|
|
rj = m0.height - j - 1
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m0[j, ri], m1[j, i])
|
|
|
|
assert_cvscalar_equal(m0[j, ri], m2[j, i])
|
|
|
|
assert_cvscalar_equal(m0[rj, i], m3[j, i])
|
|
|
|
assert_cvscalar_equal(m0[rj, i], m4[j, i])
|
|
|
|
assert_cvscalar_equal(m0[rj, ri], m5[j, i])
|
|
|
|
assert_cvscalar_equal(m0[rj, ri], m6[j, i])
|
|
|
|
assert_cvscalar_equal(m0[rj, i], m7[j, i])
|
|
|
|
assert_cvscalar_equal(m0[rj, i], m8[j, i])
|
2011-01-01 14:06:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2011-01-01 14:59:19 -05:00
|
|
|
|
|
|
|
def test_split
|
|
|
|
m0 = create_cvmat(2, 3, :cv8u, 3) { |j, i, c|
|
|
|
|
CvScalar.new(c * 10, c * 20, c * 30)
|
|
|
|
}
|
|
|
|
m0.split.each_with_index { |m, idx|
|
|
|
|
assert_equal(m0.height, m.height)
|
|
|
|
assert_equal(m0.width, m.width)
|
|
|
|
|
|
|
|
c = 0
|
|
|
|
m0.height.times { |j|
|
|
|
|
m0.width.times { |i|
|
|
|
|
val = c * 10 * (idx + 1)
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(val), m[j, i])
|
2011-01-01 14:59:19 -05:00
|
|
|
c += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge
|
|
|
|
m0 = create_cvmat(2, 3, :cv8u, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c * 10, c * 20, c * 30, c * 40)
|
|
|
|
}
|
|
|
|
m1 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(c * 10)
|
|
|
|
}
|
|
|
|
m2 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(c * 20)
|
|
|
|
}
|
|
|
|
m3 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(c * 30)
|
|
|
|
}
|
|
|
|
m4 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(c * 40)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = CvMat.merge(m1, m2, m3, m4)
|
|
|
|
|
|
|
|
assert_equal(m0.height, m.height)
|
|
|
|
assert_equal(m0.width, m.width)
|
|
|
|
m0.height.times { |j|
|
|
|
|
m0.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m0[j, i], m[j, i])
|
2011-01-01 14:59:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mix_channels
|
2011-01-02 23:47:47 -05:00
|
|
|
flunk('FIXME: CvMat.mix_channels is not implemented yet.')
|
2011-01-01 14:59:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rand_shuffle
|
|
|
|
m0 = create_cvmat(2, 3)
|
|
|
|
m1 = m0.clone
|
|
|
|
m1.rand_shuffle!
|
|
|
|
m2 = m0.rand_shuffle
|
|
|
|
m3 = m0.clone
|
|
|
|
m3.rand_shuffle!(123, 234)
|
|
|
|
m4 = m0.rand_shuffle(123, 234)
|
|
|
|
|
|
|
|
assert_shuffled_equal = lambda { |src, shuffled|
|
|
|
|
assert_equal(src.width, shuffled.width)
|
|
|
|
assert_equal(src.height, shuffled.height)
|
|
|
|
mat0, mat1 = [], []
|
|
|
|
src.height { |j|
|
|
|
|
src.width { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
mat0 << src[j, i].to_s
|
|
|
|
mat1 << shuffled[j, i].to_s
|
2011-01-01 14:59:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_equal(0, (mat0 - mat1).size)
|
|
|
|
}
|
|
|
|
|
|
|
|
[m1, m2, m3, m4].each { |m|
|
|
|
|
assert_shuffled_equal.call(m0, m)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lut
|
2011-01-01 15:51:53 -05:00
|
|
|
m0 = create_cvmat(2, 3, :cv8u, 3)
|
|
|
|
lut_mat = create_cvmat(1, 256, :cv8u, 3) { |j, i, c|
|
|
|
|
CvScalar.new(255 - c, 255 - c, 255 - c)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = m0.lut(lut_mat)
|
|
|
|
assert_equal(m0.height, m.height)
|
|
|
|
assert_equal(m0.width, m.width)
|
|
|
|
m0.height.times { |j|
|
|
|
|
m0.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
r, g, b = m0[j, i].to_ary.map { |c| 255 - c }
|
|
|
|
assert_cvscalar_equal(CvScalar.new(r, g, b, 0), m[j, i])
|
2011-01-01 15:51:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_convert_scale
|
|
|
|
m0 = create_cvmat(2, 3, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(-c, -c, -c, -c)
|
|
|
|
}
|
|
|
|
|
|
|
|
m1 = m0.convert_scale(:depth => :cv8u)
|
|
|
|
m2 = m0.convert_scale(:scale => 1.5)
|
|
|
|
m3 = m0.convert_scale(:shift => 10.0)
|
|
|
|
m4 = m0.convert_scale(:depth => CV_16U)
|
|
|
|
|
|
|
|
[m1, m2, m3, m4].each { |m|
|
|
|
|
assert_equal(m0.height, m.height)
|
|
|
|
assert_equal(m0.width, m.width)
|
|
|
|
}
|
|
|
|
m0.height.times { |j|
|
|
|
|
m0.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
|
|
|
|
a = m0[j, i].to_ary.map { |x| x * 1.5 }
|
|
|
|
assert_in_delta(a, m2[j, i], 0.001)
|
|
|
|
a = m0[j, i].to_ary.map { |x| x + 10.0 }
|
|
|
|
assert_in_delta(a, m3[j, i], 0.001)
|
|
|
|
assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m4[j, i])
|
2011-01-01 15:51:53 -05:00
|
|
|
}
|
|
|
|
}
|
2011-01-01 14:59:19 -05:00
|
|
|
end
|
2011-01-01 18:46:08 -05:00
|
|
|
|
|
|
|
def test_convert_scale_abs
|
|
|
|
m0 = create_cvmat(2, 3, :cv8u, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c, c, c, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
m1 = m0.convert_scale_abs(:depth => :cv64f)
|
|
|
|
m2 = m0.convert_scale_abs(:scale => 2)
|
|
|
|
m3 = m0.convert_scale_abs(:shift => 10.0)
|
|
|
|
m4 = m0.convert_scale_abs(:depth => CV_64F)
|
|
|
|
|
|
|
|
[m1, m2, m3, m4].each { |m|
|
|
|
|
assert_equal(m0.height, m.height)
|
|
|
|
assert_equal(m0.width, m.width)
|
|
|
|
}
|
|
|
|
m0.height.times { |j|
|
|
|
|
m0.width.times { |i|
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_cvscalar_equal(m0[j, i], m1[j, i])
|
|
|
|
a = m0[j, i].to_ary.map { |x| (x * 2).abs }
|
|
|
|
assert_in_delta(a, m2[j, i], 0.001)
|
|
|
|
a = m0[j, i].to_ary.map { |x| (x + 10.0).abs }
|
|
|
|
assert_in_delta(a, m3[j, i], 0.001)
|
|
|
|
assert_cvscalar_equal(m0[j, i], m4[j, i])
|
2011-01-01 18:46:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add
|
|
|
|
m1 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c * 0.1, c * 0.2, c * 0.3, c * 0.4)
|
|
|
|
}
|
|
|
|
m2 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c * 1, c * 2, c * 3, c * 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat + CvMat
|
|
|
|
m3 = m1.add(m2)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
s = CvScalar.new(n * 1.1, n * 2.2, n * 3.3, n * 4.4)
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m3[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat + CvScalar
|
|
|
|
s1 = CvScalar.new(1, 2, 3, 4)
|
|
|
|
m3 = m1.add(s1)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
s = CvScalar.new(n * 0.1 + 1, n * 0.2 + 2, n * 0.3 + 3, n * 0.4 + 4)
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m3[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
m3 = m1 + m2
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
s = CvScalar.new(n * 1.1, n * 2.2, n * 3.3, n * 4.4)
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m3[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat + CvMat with Mask
|
|
|
|
mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
(i < 3 and j < 2) ? 1 : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
m4 = m1.add(m2, mask)
|
|
|
|
assert_equal(m1.height, m4.height)
|
|
|
|
assert_equal(m1.width, m4.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
if i < 3 and j < 2
|
|
|
|
s = CvScalar.new(n * 1.1, n * 2.2, n * 3.3, n * 4.4)
|
|
|
|
else
|
2011-01-05 11:16:10 -05:00
|
|
|
s = m1[j, i]
|
2011-01-01 18:46:08 -05:00
|
|
|
end
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m4[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat + CvScalar with Mask
|
|
|
|
m4 = m1.add(s1, mask)
|
|
|
|
assert_equal(m1.height, m4.height)
|
|
|
|
assert_equal(m1.width, m4.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
if i < 3 and j < 2
|
|
|
|
s = CvScalar.new(n * 0.1 + 1, n * 0.2 + 2, n * 0.3 + 3, n * 0.4 + 4)
|
|
|
|
else
|
2011-01-05 11:16:10 -05:00
|
|
|
s = m1[j, i]
|
2011-01-01 18:46:08 -05:00
|
|
|
end
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m4[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sub
|
|
|
|
m1 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c * 0.1, c * 0.2, c * 0.3, c * 0.4)
|
|
|
|
}
|
|
|
|
m2 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c * 1, c * 2, c * 3, c * 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat - CvMat
|
|
|
|
m3 = m1.sub(m2)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
s = CvScalar.new(-n * 0.9, -n * 1.8, -n * 2.7, -n * 3.6)
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m3[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat - CvScalar
|
|
|
|
s1 = CvScalar.new(1, 2, 3, 4)
|
|
|
|
m3 = m1.sub(s1)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
s = CvScalar.new(n * 0.1 - 1, n * 0.2 - 2, n * 0.3 - 3, n * 0.4 - 4)
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m3[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
m3 = m1 - m2
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
s = CvScalar.new(-n * 0.9, -n * 1.8, -n * 2.7, -n * 3.6)
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m3[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
(i < 3 and j < 2) ? 1 : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat - CvMat with Mask
|
|
|
|
m4 = m1.sub(m2, mask)
|
|
|
|
assert_equal(m1.height, m4.height)
|
|
|
|
assert_equal(m1.width, m4.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
if i < 3 and j < 2
|
|
|
|
s = CvScalar.new(-n * 0.9, -n * 1.8, -n * 2.7, -n * 3.6)
|
|
|
|
else
|
2011-01-05 11:16:10 -05:00
|
|
|
s = m1[j, i]
|
2011-01-01 18:46:08 -05:00
|
|
|
end
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m4[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat - CvScalar with Mask
|
2011-01-05 11:16:10 -05:00
|
|
|
m4 = m1.sub(s1, mask)
|
2011-01-01 18:46:08 -05:00
|
|
|
assert_equal(m1.height, m4.height)
|
|
|
|
assert_equal(m1.width, m4.width)
|
|
|
|
n = 0
|
|
|
|
m1.height.times { |j|
|
|
|
|
m1.width.times { |i|
|
|
|
|
if i < 3 and j < 2
|
|
|
|
s = CvScalar.new(n * 0.1 - 1, n * 0.2 - 2, n * 0.3 - 3, n * 0.4 - 4)
|
|
|
|
else
|
2011-01-05 11:16:10 -05:00
|
|
|
s = m1[j, i]
|
2011-01-01 18:46:08 -05:00
|
|
|
end
|
2011-01-03 01:46:21 -05:00
|
|
|
assert_in_delta(s, m4[j, i], 0.001)
|
2011-01-01 18:46:08 -05:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2011-01-02 23:47:47 -05:00
|
|
|
|
|
|
|
def test_mul
|
|
|
|
m1 = create_cvmat(3, 3, :cv32f)
|
|
|
|
s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4)
|
|
|
|
m2 = create_cvmat(3, 3, :cv32f) { s1 }
|
|
|
|
|
|
|
|
# CvMat * CvMat
|
|
|
|
m3 = m1.mul(m2)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat * CvMat * scale
|
|
|
|
scale = 2.5
|
|
|
|
m3 = m1.mul(m2, scale)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = (c + 1) * scale
|
|
|
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat * CvScalar
|
|
|
|
scale = 2.5
|
|
|
|
m3 = m1.mul(s1)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat * CvScalar * scale
|
|
|
|
m3 = m1.mul(s1, scale)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = (c + 1) * scale
|
|
|
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mat_mul
|
|
|
|
flunk('FIXME: CvMat#mat_mul is not implemented yet.')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_div
|
|
|
|
m1 = create_cvmat(3, 3, :cv32f)
|
|
|
|
s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4)
|
|
|
|
m2 = create_cvmat(3, 3, :cv32f) { s1 }
|
|
|
|
|
|
|
|
# CvMat / CvMat
|
|
|
|
m3 = m1.div(m2)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# scale * CvMat / CvMat
|
|
|
|
scale = 2.5
|
|
|
|
m3 = m1.div(m2, scale)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = (c + 1) * scale
|
|
|
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat / CvScalar
|
|
|
|
scale = 2.5
|
|
|
|
m3 = m1.div(s1)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# scale * CvMat / CvScalar
|
|
|
|
m3 = m1.div(s1, scale)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = (c + 1) * scale
|
|
|
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
m3 = m1 / m2
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_and
|
|
|
|
m1 = create_cvmat(6, 4)
|
|
|
|
s1 = CvScalar.new(1, 2, 3, 4)
|
|
|
|
m2 = create_cvmat(6, 4) { s1 }
|
|
|
|
mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
s = (i < 3 and j < 2) ? 1 : 0
|
|
|
|
CvScalar.new(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat & CvMat
|
|
|
|
m3 = m1.and(m2)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:47:47 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat & CvMat with mask
|
|
|
|
m3 = m1.and(m2, mask)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:47:47 -05:00
|
|
|
n = c + 1
|
|
|
|
if i < 3 and j < 2
|
|
|
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
|
|
|
else
|
|
|
|
CvScalar.new(n, n, n, n)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat & CvScalar
|
|
|
|
m3 = m1.and(s1)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:47:47 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat & CvScalar with mask
|
|
|
|
m3 = m1.and(s1, mask)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:47:47 -05:00
|
|
|
n = c + 1
|
|
|
|
if i < 3 and j < 2
|
|
|
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
|
|
|
else
|
|
|
|
CvScalar.new(n, n, n, n)
|
|
|
|
end
|
|
|
|
}
|
2011-01-02 23:54:27 -05:00
|
|
|
|
|
|
|
# Alias
|
|
|
|
m3 = m1 & m2
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
m3 = m1 & s1
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_or
|
|
|
|
m1 = create_cvmat(6, 4)
|
|
|
|
s1 = CvScalar.new(1, 2, 3, 4)
|
|
|
|
m2 = create_cvmat(6, 4) { s1 }
|
|
|
|
mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
s = (i < 3 and j < 2) ? 1 : 0
|
|
|
|
CvScalar.new(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat | CvMat
|
|
|
|
m3 = m1.or(m2)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat | CvMat with mask
|
|
|
|
m3 = m1.or(m2, mask)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
if i < 3 and j < 2
|
|
|
|
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
|
|
|
|
else
|
|
|
|
CvScalar.new(n, n, n, n)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat | CvScalar
|
|
|
|
m3 = m1.or(s1)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat | CvScalar with mask
|
|
|
|
m3 = m1.or(s1, mask)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
if i < 3 and j < 2
|
|
|
|
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
|
|
|
|
else
|
|
|
|
CvScalar.new(n, n, n, n)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
m3 = m1 | m2
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
m3 = m1 | s1
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
2011-01-02 23:54:27 -05:00
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
|
|
|
|
}
|
2011-01-02 23:47:47 -05:00
|
|
|
end
|
|
|
|
|
2011-01-03 00:21:38 -05:00
|
|
|
|
|
|
|
def test_xor
|
|
|
|
m1 = create_cvmat(6, 4)
|
|
|
|
s1 = CvScalar.new(1, 2, 3, 4)
|
|
|
|
m2 = create_cvmat(6, 4) { s1 }
|
|
|
|
mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
s = (i < 3 and j < 2) ? 1 : 0
|
|
|
|
CvScalar.new(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat ^ CvMat
|
|
|
|
m3 = m1.xor(m2)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat ^ CvMat with mask
|
|
|
|
m3 = m1.xor(m2, mask)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
if i < 3 and j < 2
|
|
|
|
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
|
|
|
|
else
|
|
|
|
CvScalar.new(n, n, n, n)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat ^ CvScalar
|
|
|
|
m3 = m1.xor(s1)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
# CvMat ^ CvScalar with mask
|
|
|
|
m3 = m1.xor(s1, mask)
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
if i < 3 and j < 2
|
|
|
|
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
|
|
|
|
else
|
|
|
|
CvScalar.new(n, n, n, n)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
m3 = m1 ^ m2
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
m3 = m1 ^ s1
|
|
|
|
assert_equal(m1.height, m3.height)
|
|
|
|
assert_equal(m1.width, m3.width)
|
|
|
|
assert_each_cvscalar(m3) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_not
|
|
|
|
m1 = create_cvmat(6, 4, :cv8s)
|
|
|
|
m2 = m1.not;
|
|
|
|
m3 = m1.clone
|
|
|
|
m3.not!
|
|
|
|
[m2, m3].each { |m|
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
assert_each_cvscalar(m) { |j, i, c|
|
|
|
|
n = c + 1
|
|
|
|
CvScalar.new(~n, ~n, ~n, ~n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-01-03 00:38:02 -05:00
|
|
|
def test_eq
|
|
|
|
m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
n = (c.even?) ? 10 : c
|
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(10, 0, 0, 0)
|
|
|
|
}
|
|
|
|
s1 = CvScalar.new(10, 0, 0, 0)
|
|
|
|
m3 = m1.eq(m2)
|
|
|
|
m4 = m1.eq(s1)
|
|
|
|
m5 = m1.eq(10)
|
2011-01-03 00:38:02 -05:00
|
|
|
|
2011-01-03 03:49:09 -05:00
|
|
|
[m3, m4, m5].each { |m|
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
assert_each_cvscalar(m) { |j, i, c|
|
|
|
|
n = (c.even?) ? 0xff : 0
|
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 00:38:02 -05:00
|
|
|
}
|
|
|
|
end
|
2011-01-03 03:30:36 -05:00
|
|
|
|
|
|
|
def test_gt
|
2011-01-03 03:49:09 -05:00
|
|
|
m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
2011-01-03 03:30:36 -05:00
|
|
|
CvScalar.new(c, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(10, 0, 0, 0)
|
2011-01-03 03:30:36 -05:00
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
s1 = CvScalar.new(10, 0, 0, 0)
|
|
|
|
m3 = m1.gt(m2)
|
|
|
|
m4 = m1.gt(s1)
|
|
|
|
m5 = m1.gt(10)
|
2011-01-03 03:30:36 -05:00
|
|
|
|
2011-01-03 03:49:09 -05:00
|
|
|
[m3, m4, m5].each { |m|
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
assert_each_cvscalar(m) { |j, i, c|
|
|
|
|
n = (c > 10) ? 0xff : 0
|
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
|
|
|
}
|
2011-01-03 03:30:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ge
|
2011-01-03 03:49:09 -05:00
|
|
|
m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
2011-01-03 03:30:36 -05:00
|
|
|
CvScalar.new(c, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(10, 0, 0, 0)
|
|
|
|
}
|
|
|
|
s1 = CvScalar.new(10, 0, 0, 0)
|
|
|
|
m3 = m1.ge(m2)
|
|
|
|
m4 = m1.ge(s1)
|
|
|
|
m5 = m1.ge(10)
|
2011-01-03 03:30:36 -05:00
|
|
|
|
2011-01-03 03:49:09 -05:00
|
|
|
[m3, m4, m5].each { |m|
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
assert_each_cvscalar(m) { |j, i, c|
|
|
|
|
n = (c >= 10) ? 0xff : 0
|
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:30:36 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lt
|
2011-01-03 03:49:09 -05:00
|
|
|
m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
2011-01-03 03:30:36 -05:00
|
|
|
CvScalar.new(c, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(10, 0, 0, 0)
|
|
|
|
}
|
|
|
|
s1 = CvScalar.new(10, 0, 0, 0)
|
|
|
|
m3 = m1.lt(m2)
|
|
|
|
m4 = m1.lt(s1)
|
|
|
|
m5 = m1.lt(10)
|
|
|
|
|
|
|
|
[m3, m4, m5].each { |m|
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
assert_each_cvscalar(m) { |j, i, c|
|
|
|
|
n = (c < 10) ? 0xff : 0
|
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:30:36 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_le
|
2011-01-03 03:49:09 -05:00
|
|
|
m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
2011-01-03 03:30:36 -05:00
|
|
|
CvScalar.new(c, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(10, 0, 0, 0)
|
|
|
|
}
|
|
|
|
s1 = CvScalar.new(10, 0, 0, 0)
|
|
|
|
m3 = m1.le(m2)
|
|
|
|
m4 = m1.le(s1)
|
|
|
|
m5 = m1.le(10)
|
|
|
|
|
|
|
|
[m3, m4, m5].each { |m|
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
assert_each_cvscalar(m) { |j, i, c|
|
|
|
|
n = (c <= 10) ? 0xff : 0
|
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:30:36 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-01-03 05:13:58 -05:00
|
|
|
def test_in_range
|
|
|
|
m0 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
CvScalar.new(c + 5, 0, 0, 0)
|
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
2011-01-03 05:13:58 -05:00
|
|
|
CvScalar.new(10, 0, 0, 0)
|
2011-01-03 03:30:36 -05:00
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
2011-01-03 05:13:58 -05:00
|
|
|
CvScalar.new(20, 0, 0, 0)
|
2011-01-03 03:30:36 -05:00
|
|
|
}
|
2011-01-03 03:49:09 -05:00
|
|
|
s1 = CvScalar.new(10, 0, 0, 0)
|
2011-01-03 05:13:58 -05:00
|
|
|
s2 = CvScalar.new(20, 0, 0, 0)
|
|
|
|
|
|
|
|
m3 = m0.in_range(m1, m2)
|
|
|
|
m4 = m0.in_range(s1, s2)
|
|
|
|
m5 = m0.in_range(10, 20)
|
2011-01-03 03:30:36 -05:00
|
|
|
|
2011-01-03 03:49:09 -05:00
|
|
|
[m3, m4, m5].each { |m|
|
2011-01-03 05:13:58 -05:00
|
|
|
assert_equal(m0.height, m.height)
|
|
|
|
assert_equal(m0.width, m.width)
|
2011-01-03 03:49:09 -05:00
|
|
|
assert_each_cvscalar(m) { |j, i, c|
|
2011-01-03 05:13:58 -05:00
|
|
|
val = m0[j, i][0]
|
|
|
|
n = ((val >= 10) and (val < 20)) ? 0xff : 0
|
2011-01-03 03:49:09 -05:00
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2011-01-03 03:30:36 -05:00
|
|
|
|
2011-01-03 05:13:58 -05:00
|
|
|
def test_abs_diff
|
|
|
|
m0 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(-10 + 10.5, 20 + 10.5, -30 + 10.5, 40 - 10.5)
|
|
|
|
}
|
|
|
|
m1 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c + 10.5, c - 10.5, c + 10.5, c - 10.5)
|
|
|
|
}
|
|
|
|
m2 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c, c, c, c)
|
|
|
|
}
|
2011-01-03 03:30:36 -05:00
|
|
|
|
2011-01-03 05:13:58 -05:00
|
|
|
s1 = CvScalar.new(-10, 20, -30, 40)
|
|
|
|
m3 = m1.abs_diff(m2)
|
|
|
|
m4 = m0.abs_diff(s1)
|
|
|
|
|
|
|
|
[m3, m4].each { |m|
|
|
|
|
assert_equal(m1.width, m.width)
|
|
|
|
assert_equal(m1.height, m.height)
|
|
|
|
assert_each_cvscalar(m, 0.001) {
|
|
|
|
CvScalar.new(10.5, 10.5, 10.5, 10.5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_count_non_zero
|
|
|
|
m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
|
|
|
|
n = 0
|
|
|
|
n = 1 if i == 0
|
|
|
|
CvScalar.new(n, 0, 0, 0)
|
|
|
|
}
|
|
|
|
assert_equal(6, m0.count_non_zero)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sum
|
|
|
|
m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
|
|
|
|
CvScalar.new(c, c, c, c)
|
|
|
|
}
|
|
|
|
assert_cvscalar_equal(CvScalar.new(276, 0, 0, 0), m0.sum)
|
|
|
|
|
|
|
|
m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
|
|
|
|
CvScalar.new(-c)
|
|
|
|
}
|
|
|
|
assert_cvscalar_equal(CvScalar.new(-276, 0, 0, 0), m0.sum)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_avg_sdv
|
|
|
|
m0 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
|
|
|
|
CvScalar.new(c * 0.1, -c * 0.1, c, -c)
|
|
|
|
}
|
2011-01-03 05:28:30 -05:00
|
|
|
# CvMat#avg
|
2011-01-03 05:19:21 -05:00
|
|
|
assert_in_delta(CvScalar.new(1.15, -1.15, 11.5, -11.5), m0.avg, 0.001)
|
2011-01-03 05:28:30 -05:00
|
|
|
# CvMat#sdv
|
2011-01-03 05:19:21 -05:00
|
|
|
assert_in_delta(CvScalar.new(0.69221, 0.69221, 6.9221, 6.9221), m0.sdv, 0.001)
|
2011-01-03 05:28:30 -05:00
|
|
|
# CvMat#avg_sdv
|
2011-01-03 05:13:58 -05:00
|
|
|
avg, sdv = m0.avg_sdv
|
|
|
|
assert_in_delta(CvScalar.new(1.15, -1.15, 11.5, -11.5), avg, 0.001)
|
|
|
|
assert_in_delta(CvScalar.new(0.69221, 0.69221, 6.9221, 6.9221), sdv, 0.001)
|
|
|
|
|
|
|
|
mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
|
|
|
|
n = (i == j) ? 1 : 0
|
|
|
|
CvScalar.new(n)
|
|
|
|
}
|
2011-01-03 05:28:30 -05:00
|
|
|
# CvMat#avg
|
2011-01-03 05:19:21 -05:00
|
|
|
assert_in_delta(CvScalar.new(0.75, -0.75, 7.5, -7.5), m0.avg(mask), 0.001)
|
2011-01-03 05:28:30 -05:00
|
|
|
# CvMat#sdv
|
2011-01-03 05:19:21 -05:00
|
|
|
assert_in_delta(CvScalar.new(0.55901, 0.55901, 5.5901, 5.5901), m0.sdv(mask), 0.001)
|
2011-01-03 05:28:30 -05:00
|
|
|
# CvMat#avg_sdv
|
2011-01-03 05:13:58 -05:00
|
|
|
avg, sdv = m0.avg_sdv(mask)
|
|
|
|
assert_in_delta(CvScalar.new(0.75, -0.75, 7.5, -7.5), avg, 0.001)
|
|
|
|
assert_in_delta(CvScalar.new(0.55901, 0.55901, 5.5901, 5.5901), sdv, 0.001)
|
|
|
|
end
|
2011-01-03 05:28:30 -05:00
|
|
|
|
|
|
|
def test_min_max_loc
|
|
|
|
m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
|
|
|
|
CvScalar.new(c * 0.5)
|
|
|
|
}
|
|
|
|
m0[2, 3] = CvScalar.new(100.5) # Max
|
|
|
|
m0[5, 1] = CvScalar.new(-100.5) # Min
|
|
|
|
|
|
|
|
min_val, max_val, min_loc, max_loc = m0.min_max_loc
|
|
|
|
assert_equal(-100.5, min_val)
|
|
|
|
assert_equal(5, min_loc.y)
|
|
|
|
assert_equal(1, min_loc.x)
|
|
|
|
assert_equal(100.5, max_val)
|
|
|
|
assert_equal(2, max_loc.y)
|
|
|
|
assert_equal(3, max_loc.x)
|
|
|
|
end
|
2010-12-31 03:29:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|