From ca92405ae8eda5871bab8a82b3a29a180253da32 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Fri, 15 Apr 2011 01:17:57 +0900 Subject: [PATCH] fixed a bug (sizes of velx, vely) of CvMat#optical_flow_bm --- ext/cvmat.cpp | 6 +++-- test/test_cvmat_imageprocessing.rb | 40 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/ext/cvmat.cpp b/ext/cvmat.cpp index dd397c0..cbd947c 100644 --- a/ext/cvmat.cpp +++ b/ext/cvmat.cpp @@ -4968,11 +4968,13 @@ rb_optical_flow_bm(int argc, VALUE *argv, VALUE self) block_size = BM_BLOCK_SIZE(options), shift_size = BM_SHIFT_SIZE(options), max_range = BM_MAX_RANGE(options), - velocity_size = cvSize(image_size.width / block_size.width, image_size.height / block_size.height); + velocity_size = cvSize((image_size.width - block_size.width) / shift_size.width, + (image_size.height - block_size.height) / shift_size.height); if (NIL_P(velx) && NIL_P(vely)) { velx = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1)); vely = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1)); - } else { + } + else { if (rb_obj_is_kind_of(velx, cCvMat::rb_class()) && rb_obj_is_kind_of(vely, cCvMat::rb_class())) use_previous = 1; else diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index be23956..0c59483 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1698,5 +1698,45 @@ class TestCvMat_imageprocessing < OpenCVTestCase curr.optical_flow_lk('foobar', CvSize.new(3, 3)) } end + + def test_optical_flow_bm + size = 128 + prev = create_cvmat(size, size, :cv8u, 1) { |j, i| + if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size + CvColor::Black + else + CvColor::White + end + } + curr = create_cvmat(size, size, :cv8u, 1) { |j, i| + if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size + CvColor::Black + else + CvColor::White + end + } + + [curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(4, 4), + :shift_size => CvSize.new(1, 1), :max_range => CvSize.new(4, 4)), + curr.optical_flow_bm(prev)].each { |velx, vely| + assert_equal('08e73a6fa9af7684a5eddc4f30fd46e7', hash_img(velx)) + assert_equal('aabaf1b7393b950c2297f567b6f57d5d', hash_img(vely)) + } + + velx, vely = curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(3, 3), + :shift_size => CvSize.new(2, 2), :max_range => CvSize.new(3, 3)); + assert_equal('ec6441e73edf2b2933165034362fc129', hash_img(velx)) + assert_equal('88b965b0003514f4239b9e97179f9c1e', hash_img(vely)) + + velx, vely = curr.optical_flow_bm(prev) + velx, vely = curr.optical_flow_bm(prev, velx, vely) + assert_equal('6ad6b7a5c935379c0df4b9ec5666f3de', hash_img(velx)) + assert_equal('b317b0b9d4fdb0e5cd40beb0dd4143b4', hash_img(vely)) + + assert_raise(ArgumentError) { + curr.optical_flow_bm(prev, 'foo', 'bar') + } + + end end