1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

fixed a comment of CvMat#rand_shuffle, and added some tests

This commit is contained in:
ser1zw 2011-01-02 04:59:19 +09:00
parent 2235b89552
commit 49d44dcdb8
2 changed files with 90 additions and 7 deletions

View file

@ -1515,9 +1515,9 @@ rb_mix_channels(int argc, VALUE *argv, VALUE self)
/* /*
* call-seq: * call-seq:
* rand_shuffle([seed = nil][,iter = 1]) * rand_shuffle([seed = nil][,iter_factor = 1]) -> cvmat
* *
* Return filled the destination array with values from the look-up table. * Return shuffled matrix
* *
* see rand_shuffle! * see rand_shuffle!
*/ */
@ -1529,12 +1529,13 @@ rb_rand_shuffle(int argc, VALUE *argv, VALUE self)
/* /*
* call-seq: * call-seq:
* rand_shuffle!([seed = nil][,iter = 1]) * rand_shuffle!([seed = nil][,iter_factor = 1]) -> cvmat
* *
* fills the destination array with values from the look-up table. * Shuffles the matrix by swapping randomly chosen pairs of the matrix elements on each iteration
* Indices of the entries are taken from the source array. That is, the function processes each element of src as following: * (where each element may contain several components in case of multi-channel arrays). The number of
* dst(I)=lut[src(I)+DELTA] * iterations (i.e. pairs swapped) is (iter_factor*mat.rows*mat.cols).round, so iter_factor=0 means
* where DELTA=0 if src has depth :cv8u, and DELTA=128 if src has depth :cv8s. * that no shuffling is done, iter_factor=1 means that the function swaps rows(mat)*cols(mat) random
* pairs etc.
*/ */
VALUE VALUE
rb_rand_shuffle_bang(int argc, VALUE *argv, VALUE self) rb_rand_shuffle_bang(int argc, VALUE *argv, VALUE self)

View file

@ -640,6 +640,88 @@ class TestCvMat < OpenCVTestCase
} }
} }
end end
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)
assert_cvscalar_equal(CvScalar.new(val), m[i, j])
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|
assert_cvscalar_equal(m0[i, j], m[i, j])
}
}
end
def test_mix_channels
flunk('CvMat.mix_channels is not implemented yet.')
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|
mat0 << src[i, j].to_s
mat1 << shuffled[i, j].to_s
}
}
assert_equal(0, (mat0 - mat1).size)
}
[m1, m2, m3, m4].each { |m|
assert_shuffled_equal.call(m0, m)
}
end
def test_lut
end
# def test_avg_sdv # def test_avg_sdv
# m = CvMat.new(1, 8, CV_32F) # m = CvMat.new(1, 8, CV_32F)