Skip to content
This repository was archived by the owner on Dec 3, 2021. It is now read-only.

Commit ca92405

Browse files
committed
fixed a bug (sizes of velx, vely) of CvMat#optical_flow_bm
1 parent b4f8c13 commit ca92405

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

ext/cvmat.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4968,11 +4968,13 @@ rb_optical_flow_bm(int argc, VALUE *argv, VALUE self)
49684968
block_size = BM_BLOCK_SIZE(options),
49694969
shift_size = BM_SHIFT_SIZE(options),
49704970
max_range = BM_MAX_RANGE(options),
4971-
velocity_size = cvSize(image_size.width / block_size.width, image_size.height / block_size.height);
4971+
velocity_size = cvSize((image_size.width - block_size.width) / shift_size.width,
4972+
(image_size.height - block_size.height) / shift_size.height);
49724973
if (NIL_P(velx) && NIL_P(vely)) {
49734974
velx = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1));
49744975
vely = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1));
4975-
} else {
4976+
}
4977+
else {
49764978
if (rb_obj_is_kind_of(velx, cCvMat::rb_class()) && rb_obj_is_kind_of(vely, cCvMat::rb_class()))
49774979
use_previous = 1;
49784980
else

test/test_cvmat_imageprocessing.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,5 +1698,45 @@ def test_optical_flow_lk
16981698
curr.optical_flow_lk('foobar', CvSize.new(3, 3))
16991699
}
17001700
end
1701+
1702+
def test_optical_flow_bm
1703+
size = 128
1704+
prev = create_cvmat(size, size, :cv8u, 1) { |j, i|
1705+
if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size
1706+
CvColor::Black
1707+
else
1708+
CvColor::White
1709+
end
1710+
}
1711+
curr = create_cvmat(size, size, :cv8u, 1) { |j, i|
1712+
if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size
1713+
CvColor::Black
1714+
else
1715+
CvColor::White
1716+
end
1717+
}
1718+
1719+
[curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(4, 4),
1720+
:shift_size => CvSize.new(1, 1), :max_range => CvSize.new(4, 4)),
1721+
curr.optical_flow_bm(prev)].each { |velx, vely|
1722+
assert_equal('08e73a6fa9af7684a5eddc4f30fd46e7', hash_img(velx))
1723+
assert_equal('aabaf1b7393b950c2297f567b6f57d5d', hash_img(vely))
1724+
}
1725+
1726+
velx, vely = curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(3, 3),
1727+
:shift_size => CvSize.new(2, 2), :max_range => CvSize.new(3, 3));
1728+
assert_equal('ec6441e73edf2b2933165034362fc129', hash_img(velx))
1729+
assert_equal('88b965b0003514f4239b9e97179f9c1e', hash_img(vely))
1730+
1731+
velx, vely = curr.optical_flow_bm(prev)
1732+
velx, vely = curr.optical_flow_bm(prev, velx, vely)
1733+
assert_equal('6ad6b7a5c935379c0df4b9ec5666f3de', hash_img(velx))
1734+
assert_equal('b317b0b9d4fdb0e5cd40beb0dd4143b4', hash_img(vely))
1735+
1736+
assert_raise(ArgumentError) {
1737+
curr.optical_flow_bm(prev, 'foo', 'bar')
1738+
}
1739+
1740+
end
17011741
end
17021742

0 commit comments

Comments
 (0)