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

modified some image processing functions of CvMat, and added some tests

This commit is contained in:
ser1zw 2011-01-16 04:29:15 +09:00
parent 6207e00143
commit 230fa6da47
6 changed files with 259 additions and 55 deletions

View file

@ -1662,7 +1662,7 @@ rb_convert_scale_abs(VALUE self, VALUE hash)
scale = rb_hash_aref(hash, ID2SYM(rb_intern("scale"))),
shift = rb_hash_aref(hash, ID2SYM(rb_intern("shift"))),
dest = new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_8U, CV_MAT_CN(CVMAT(self)->type)));
cvConvertScale(CVARR(self), CVARR(dest), IF_DBL(scale, 1.0), IF_DBL(shift, 0.0));
cvConvertScaleAbs(CVARR(self), CVARR(dest), IF_DBL(scale, 1.0), IF_DBL(shift, 0.0));
return dest;
}
@ -3007,7 +3007,7 @@ rb_put_text_bang(int argc, VALUE *argv, VALUE self)
* sobel(<i>xorder,yorder[,aperture_size=3]</i>) -> cvmat
*
* Calculates first, second, third or mixed image derivatives using extended Sobel operator.
* <i>self</i> should be single-channel 8bit signed/unsigned or 32bit floating-point.
* <i>self</i> should be single-channel 8bit unsigned or 32bit floating-point.
*
* link:../images/CvMat_sobel.png
*/
@ -3019,14 +3019,13 @@ rb_sobel(int argc, VALUE *argv, VALUE self)
aperture_size = INT2FIX(3);
switch(CV_MAT_DEPTH(CVMAT(self)->type)) {
case CV_8U:
case CV_8S:
dest = new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_16S, 1));
break;
case CV_32F:
dest = new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_32F, 1));
break;
default:
rb_raise(rb_eNotImpError, "source depth should be CV_8U or CV_8S or CV_32F.");
rb_raise(rb_eRuntimeError, "source depth should be CV_8U or CV_32F.");
}
cvSobel(CVARR(self), CVARR(dest), NUM2INT(xorder), NUM2INT(yorder), NUM2INT(aperture_size));
return dest;
@ -3037,7 +3036,7 @@ rb_sobel(int argc, VALUE *argv, VALUE self)
* laplace(<i>[aperture_size = 3]</i>) -> cvmat
*
* Calculates Laplacian of the image.
* <i>self</i> should be single-channel 8bit signed/unsigned or 32bit floating-point.
* <i>self</i> should be single-channel 8bit unsigned or 32bit floating-point.
*/
VALUE
rb_laplace(int argc, VALUE *argv, VALUE self)
@ -3047,14 +3046,13 @@ rb_laplace(int argc, VALUE *argv, VALUE self)
aperture_size = INT2FIX(3);
switch(CV_MAT_DEPTH(CVMAT(self)->type)) {
case CV_8U:
case CV_8S:
dest = new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_16S, 1));
break;
case CV_32F:
dest = new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_32F, 1));
break;
default:
rb_raise(rb_eNotImpError, "source depth should be CV_8U or CV_8S or CV_32F.");
rb_raise(rb_eRuntimeError, "source depth should be CV_8U or CV_32F.");
}
cvLaplace(CVARR(self), CVARR(dest), NUM2INT(aperture_size));
return dest;
@ -3108,7 +3106,7 @@ VALUE
rb_corner_eigenvv(int argc, VALUE *argv, VALUE self)
{
VALUE block_size, aperture_size, dest;
if (rb_scan_args(argc, argv, "11", &block_size, &aperture_size) < 1)
if (rb_scan_args(argc, argv, "11", &block_size, &aperture_size) < 2)
aperture_size = INT2FIX(3);
Check_Type(block_size, T_FIXNUM);
CvSize size = cvGetSize(CVARR(self));
@ -3127,7 +3125,8 @@ VALUE
rb_corner_min_eigen_val(int argc, VALUE *argv, VALUE self)
{
VALUE block_size, aperture_size, dest;
rb_scan_args(argc, argv, "11", &block_size, &aperture_size);
if (rb_scan_args(argc, argv, "11", &block_size, &aperture_size) < 2)
aperture_size = INT2FIX(3);
dest = new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_32F, 1));
cvCornerMinEigenVal(CVARR(self), CVARR(dest), FIX2INT(block_size), FIX2INT(aperture_size));
return dest;
@ -3178,28 +3177,33 @@ rbi_find_corner_sub_pix(int argc, VALUE *argv, VALUE self)
VALUE
rb_good_features_to_track(int argc, VALUE *argv, VALUE self)
{
VALUE quality_level, min_distance, good_features_to_track_option, eigen, tmp, storage;
VALUE quality_level, min_distance, good_features_to_track_option;
CvMat *eigen, *tmp;
int i;
rb_scan_args(argc, argv, "21", &quality_level, &min_distance, &good_features_to_track_option);
good_features_to_track_option = GOOD_FEATURES_TO_TRACK_OPTION(good_features_to_track_option);
CvMat *src = CVMAT(self);
eigen = cCvMat::new_object(cvGetSize(src), CV_MAKETYPE(CV_32F, 1));
tmp = cCvMat::new_object(cvGetSize(src), CV_MAKETYPE(CV_32F, 1));
CvSize size = cvGetSize(src);
eigen = cvCreateMat(size.height, size.width, CV_MAKETYPE(CV_32F, 1));
tmp = cvCreateMat(size.height, size.width, CV_MAKETYPE(CV_32F, 1));
int np = GF_MAX(good_features_to_track_option);
if(!(np > 0))
rb_raise(rb_eArgError, "option :max should be positive value.");
CvPoint2D32f *p32 = (CvPoint2D32f*)cvAlloc(sizeof(CvPoint2D32f) * np);
if(!p32)
rb_raise(rb_eNoMemError, "failed allocate memory.");
cvGoodFeaturesToTrack(src, CVARR(eigen), CVARR(tmp), p32, &np, NUM2DBL(quality_level), NUM2DBL(min_distance),
cvGoodFeaturesToTrack(src, &eigen, &tmp, p32, &np, NUM2DBL(quality_level), NUM2DBL(min_distance),
GF_MASK(good_features_to_track_option),
GF_BLOCK_SIZE(good_features_to_track_option),
GF_USE_HARRIS(good_features_to_track_option),
GF_K(good_features_to_track_option));
storage = cCvMemStorage::new_object();
CvSeq *pseq = cvCreateSeq(CV_SEQ_POINT_SET, sizeof(CvSeq), sizeof(CvPoint2D32f), CVMEMSTORAGE(storage));
cvSeqPushMulti(pseq, p32, np);
VALUE corners = rb_ary_new2(np);
for (i = 0; i < np; i++)
rb_ary_store(corners, i, cCvPoint2D32f::new_object(p32[i]));
cvFree(&p32);
return cCvSeq::new_sequence(cCvSeq::rb_class(), pseq, cCvPoint2D32f::rb_class(), storage);
cvReleaseMat(&eigen);
cvReleaseMat(&tmp);
return corners;
}
/*

View file

@ -39,9 +39,29 @@ class OpenCVTestCase < Test::Unit::TestCase
end
def snap(*images)
win = []
images.size.times { |i| win << GUI::Window.new("snap-#{i}") }
win.each_with_index { |w, i| w.show images[i] }
n = -1
images.map! { |val|
n += 1
if val.is_a? Hash
val
elsif val.is_a? Array
{:title => val[0], :image => val[1] }
else
{:title => "snap-#{n}", :image => val }
end
}
pos = CvPoint.new(0, 0)
images.each { |img|
w = GUI::Window.new(img[:title])
w.show(img[:image])
w.move(pos)
pos.x += img[:image].width
if pos.x > 800
pos.y += img[:image].height
pos.x = 0
end
}
GUI::wait_key
GUI::Window::destroy_all

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
test/samples/lena-32x32.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 983 B

View file

@ -31,10 +31,8 @@ class TestCvMat_drawing < OpenCVTestCase
m1.line!(CvPoint.new(1, 0), CvPoint.new(m0.width - 1, m0.height - 1),
:color => CvColor::Blue, :thickness => 1, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Line: Blue, thickness = 1').show(m1)
# GUI::Window.new('Line: Red, thickness = 3').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Line: Blue, thickness = 1', m1], ['Line: Red, thickness = 3', m2])
end
def test_rectangle
@ -45,10 +43,8 @@ class TestCvMat_drawing < OpenCVTestCase
m1.rectangle!(CvPoint.new(20, 20), CvPoint.new(m0.width - 20, m0.height - 20),
:color => CvColor::Blue, :thickness => 1, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Rectangle: Blue, thickness = 1').show(m1)
# GUI::Window.new('Rectangle: Red, thickness = 3').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Rectangle: Blue, thickness = 1', m1], ['Rectangle: Red, thickness = 3', m2])
end
def test_circle
@ -58,10 +54,8 @@ class TestCvMat_drawing < OpenCVTestCase
:color => CvColor::Red, :thickness => 3, :line_type => :aa)
m1.circle!(CvPoint.new(m0.width / 2, m0.height / 2), 80,
:color => CvColor::Blue, :thickness => 1, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Circle: Blue, thickness = 1').show(m1)
# GUI::Window.new('Circle: Red, thickness = 3').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Circle: Blue, thickness = 1', m1], ['Circle: Red, thickness = 3', m2])
end
def test_ellipse
@ -72,10 +66,8 @@ class TestCvMat_drawing < OpenCVTestCase
m1.ellipse!(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), 30, 0, 360,
:color => CvColor::Blue, :thickness => 1, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Ellipse: Blue, thickness = 1').show(m1)
# GUI::Window.new('Ellipse: Red, thickness = 3').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Ellipse: Blue, thickness = 1', m1], ['Ellipse: Red, thickness = 3', m2])
end
def test_ellipse_box
@ -85,10 +77,8 @@ class TestCvMat_drawing < OpenCVTestCase
m2 = m0.ellipse_box(box, :color => CvColor::Red, :thickness => 3, :line_type => :aa)
m1.ellipse_box!(box, :color => CvColor::Blue, :thickness => 1, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Ellipse box: Blue, thickness = 1').show(m1)
# GUI::Window.new('Ellipse box: Red, thickness = 3').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Ellipse box: Blue, thickness = 1', m1], ['Ellipse box: Red, thickness = 3', m2])
end
def test_fill_poly
@ -101,10 +91,8 @@ class TestCvMat_drawing < OpenCVTestCase
m2 = m0.fill_poly(pt, :color => CvColor::Red, :line_type => :aa)
m1.fill_poly!(pt, :color => CvColor::Blue, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Fill poly: Blue').show(m1)
# GUI::Window.new('Fill poly: Red').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Fill poly: Blue', m1], ['Fill poly: Red', m2])
end
def test_fill_convex_poly
@ -115,10 +103,8 @@ class TestCvMat_drawing < OpenCVTestCase
m2 = m0.fill_convex_poly(pt, :color => CvColor::Red, :line_type => :aa)
m1.fill_convex_poly!(pt, :color => CvColor::Blue, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Fill convex poly: Blue').show(m1)
# GUI::Window.new('Fill convex poly: Red').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Fill convex poly: Blue', m1], ['Fill convex poly: Red', m2])
end
def test_poly_line
@ -130,10 +116,8 @@ class TestCvMat_drawing < OpenCVTestCase
m2 = m0.poly_line(pt, :color => CvColor::Red, :thickness => 3, :line_type => :aa)
m1.poly_line!(pt, :color => CvColor::Blue, :thickness => 1, :line_type => :aa)
# Uncomment the following lines to view the image
# GUI::Window.new('Poly line: Blue, thickness = 1').show(m1)
# GUI::Window.new('Poly line: Red, thickness = 3').show(m2)
# GUI::wait_key
# Uncomment the following line to view the image
# snap(['Fill poly line: Blue, thickness = 1', m1], ['Fill poly line: Red, thickness = 3', m2])
end
def test_put_text
@ -145,9 +129,7 @@ class TestCvMat_drawing < OpenCVTestCase
m2 = m0.put_text('test 2', CvPoint.new(30, 80), font, CvColor::Red)
# Uncomment the following lines to view the image
# GUI::Window.new('Put text: Blue, thickness = 1').show(m1)
# GUI::Window.new('Put text: Red, thickness = 3').show(m2)
# GUI::wait_key
# snap(['Put text: Blue, thickness = 1', m1], ['Put text: Red, thickness = 3', m2])
end
end

View file

@ -0,0 +1,198 @@
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8-unix -*-
require 'test/unit'
require 'opencv'
require File.expand_path(File.dirname(__FILE__)) + '/helper'
include OpenCV
# Tests for image processing functions of OpenCV::CvMat
class TestCvMat_imageprocessing < OpenCVTestCase
FILENAME_LENA256x256 = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-256x256.jpg'
FILENAME_LENA32x32 = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-32x32.jpg'
def test_sobel
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
mat1 = mat0.sobel(1, 0).convert_scale_abs(:scale => 1, :shift => 0)
mat2 = mat0.sobel(0, 1).convert_scale_abs(:scale => 1, :shift => 0)
mat3 = mat0.sobel(1, 1).convert_scale_abs(:scale => 1, :shift => 0)
mat4 = mat0.sobel(1, 1, 3).convert_scale_abs(:scale => 1, :shift => 0)
mat5 = mat0.sobel(1, 1, 5).convert_scale_abs(:scale => 1, :shift => 0)
assert_equal('30a26b7287fac75bb697bc7eef6bb53a', hash_img(mat1))
assert_equal('b740afb13b556d55280fa785190ac902', hash_img(mat2))
assert_equal('36c29ca64a599e0f5633f4f3948ed858', hash_img(mat3))
assert_equal('36c29ca64a599e0f5633f4f3948ed858', hash_img(mat4))
assert_equal('30b9e8fd64e7f86c50fb67d8703628e3', hash_img(mat5))
assert_equal(:cv16s, CvMat.new(16, 16, :cv8u, 1).sobel(1, 1).depth)
assert_equal(:cv32f, CvMat.new(16, 16, :cv32f, 1).sobel(1, 1).depth)
(DEPTH.keys - [:cv8u, :cv32f]).each { |depth|
assert_raise(RuntimeError) {
CvMat.new(3, 3, depth).sobel(1, 1)
}
}
# Uncomment the following lines to view the images
# snap(['original', mat0], ['sobel(1,0)', mat1], ['sobel(0,1)', mat2],
# ['sobel(1,1)', mat3], ['sobel(1,1,3)', mat4], ['sobel(1,1,5)', mat5])
end
def test_laplace
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
mat1 = mat0.laplace.convert_scale_abs(:scale => 1, :shift => 0)
mat2 = mat0.laplace(3).convert_scale_abs(:scale => 1, :shift => 0)
mat3 = mat0.laplace(5).convert_scale_abs(:scale => 1, :shift => 0)
assert_equal('824f8de75bfead5d83c4226f3948ce69', hash_img(mat1))
assert_equal('824f8de75bfead5d83c4226f3948ce69', hash_img(mat2))
assert_equal('23850bb8cfe9fd1b82cd73b7b4659369', hash_img(mat3))
assert_equal(:cv16s, CvMat.new(16, 16, :cv8u, 1).laplace.depth)
assert_equal(:cv32f, CvMat.new(16, 16, :cv32f, 1).laplace.depth)
(DEPTH.keys - [:cv8u, :cv32f]).each { |depth|
assert_raise(RuntimeError) {
CvMat.new(3, 3, depth).laplace
}
}
# Uncomment the following line to view the images
# snap(['original', mat0], ['laplace', mat1], ['laplace(3)', mat2], ['laplace(5)', mat3])
end
def test_canny
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
mat1 = mat0.canny(50, 200)
mat2 = mat0.canny(50, 200, 3)
mat3 = mat0.canny(50, 200, 5)
assert_equal('ec3e88035bb98b5c5f1a08c8e07ab0a8', hash_img(mat1))
assert_equal('ec3e88035bb98b5c5f1a08c8e07ab0a8', hash_img(mat2))
assert_equal('1983a6d325d11eea3261462103b0dae1', hash_img(mat3))
# Uncomment the following line to view the images
# snap(['canny(50,200)', mat1], ['canny(50,200,3)', mat2], ['canny(50,200,5)', mat3])
end
def test_pre_corner_detect
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
mat1 = mat0.pre_corner_detect
mat2 = mat0.pre_corner_detect(3)
mat3 = mat0.pre_corner_detect(5)
assert_equal('fe7c8a1d07a3dd0fb6a02d6a6de0fe9f', hash_img(mat1))
assert_equal('fe7c8a1d07a3dd0fb6a02d6a6de0fe9f', hash_img(mat2))
assert_equal('42e7443ffd389d15343d3c6bdc42f553', hash_img(mat3))
# Uncomment the following lines to show the images
# snap(['original', mat0], ['pre_coner_detect', mat1],
# ['pre_coner_detect(3)', mat2], ['pre_coner_detect(5)', mat3])
end
def test_corner_eigenvv
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
mat1 = mat0.corner_eigenvv(3)
mat2 = mat0.corner_eigenvv(3, 3)
flunk('FIXME: CvMat#corner_eigenvv is not tested yet.')
end
def test_corner_min_eigen_val
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
mat1 = mat0.corner_min_eigen_val(3)
mat2 = mat0.corner_min_eigen_val(3, 3)
flunk('FIXME: CvMat#corner_min_eigen_val is not tested yet.')
end
def test_corner_harris
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
mat1 = mat0.corner_harris(3)
mat2 = mat0.corner_harris(3, 3)
mat3 = mat0.corner_harris(3, 3, 0.04)
mat4 = mat0.corner_harris(3, 7, 0.01)
assert_equal('6ceb54b54cc98a72de7cb75649fb0a12', hash_img(mat1))
assert_equal('6ceb54b54cc98a72de7cb75649fb0a12', hash_img(mat2))
assert_equal('6ceb54b54cc98a72de7cb75649fb0a12', hash_img(mat3))
assert_equal('4e703deb9a418bbf37e3283f4a7d4d32', hash_img(mat4))
# Uncomment the following lines to show the images
# snap(['original', mat0], ['corner_harris(3)', mat1], ['corner_harris(3,3)', mat2],
# ['corner_harris(3,3,0.04)', mat3], ['corner_harris(3,7,0.01)', mat4])
end
def test_find_corner_sub_pix
flunk('FIXME: CvMat#corner_min_eigen_val is not implemented yet.')
end
def test_good_features_to_track
mat0 = CvMat.load(FILENAME_LENA32x32, CV_LOAD_IMAGE_GRAYSCALE)
mask = create_cvmat(mat0.rows, mat0.cols, :cv8u, 1) { |j, i, c|
if (i > 8 and i < 18) and (j > 8 and j < 18)
CvScalar.new(1)
else
CvScalar.new(0)
end
}
corners1 = mat0.good_features_to_track(0.2, 5)
corners2 = mat0.good_features_to_track(0.2, 5, :mask => mask)
corners3 = mat0.good_features_to_track(0.2, 5, :block_size => 7)
corners4 = mat0.good_features_to_track(0.2, 5, :use_harris => true)
corners5 = mat0.good_features_to_track(0.2, 5, :k => 0.01)
corners6 = mat0.good_features_to_track(0.2, 5, :max => 1)
expected1 = [[24, 7], [20, 23], [17, 11], [26, 29], [30, 24],
[19, 16], [28, 2], [13, 18], [14, 4]]
assert_equal(expected1.size, corners1.size)
expected1.each_with_index { |e, i|
assert_equal(e[0], corners1[i].x.to_i)
assert_equal(e[1], corners1[i].y.to_i)
}
expected2 = [[17, 11], [17, 16]]
assert_equal(expected2.size, corners2.size)
expected2.each_with_index { |e, i|
assert_equal(e[0], corners2[i].x.to_i)
assert_equal(e[1], corners2[i].y.to_i)
}
expected3 = [[21, 7], [22, 23], [18, 12], [28, 4], [28, 26],
[17, 27], [13, 20], [10, 11], [14, 5]]
assert_equal(expected3.size, corners3.size)
expected3.each_with_index { |e, i|
assert_equal(e[0], corners3[i].x.to_i)
assert_equal(e[1], corners3[i].y.to_i)
}
expected4 = [[24, 8], [20, 23], [16, 11],
[20, 16],[27, 28], [28, 2]]
assert_equal(expected4.size, corners4.size)
expected4.each_with_index { |e, i|
assert_equal(e[0], corners4[i].x.to_i)
assert_equal(e[1], corners4[i].y.to_i)
}
expected5 = [[24, 7], [20, 23], [17, 11], [26, 29], [30, 24],
[19, 16], [28, 2], [13, 18], [14, 4]]
assert_equal(expected5.size, corners5.size)
expected5.each_with_index { |e, i|
assert_equal(e[0], corners5[i].x.to_i)
assert_equal(e[1], corners5[i].y.to_i)
}
assert_equal(1, corners6.size)
assert_equal(24, corners6[0].x.to_i)
assert_equal(7, corners6[0].y.to_i)
assert_raise(ArgumentError) {
mat0.good_features_to_track(0.2, 5, :max => 0)
}
end
end