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

change arguments of CvMat#dft, CvMat#dct

This commit is contained in:
ser1zw 2012-11-07 01:11:37 +09:00
parent ad3fcebd25
commit ea8622fa8f
4 changed files with 43 additions and 67 deletions

View file

@ -2954,44 +2954,29 @@ rb_mahalonobis(int argc, VALUE *argv, VALUE self)
/* /*
* call-seq: * call-seq:
* dft(<i>anyflags...</i>) -> cvmat * dft(flags = CV_DXT_FORWARD, nonzero_rows = 0) -> cvmat
* *
* Performs forward or inverse Discrete Fourier Transform(DFT) of 1D or 2D floating-point array. * Performs forward or inverse Discrete Fourier Transform(DFT) of 1D or 2D floating-point array.
* Argument should be following symbol or combination of these.
* *
* * :forward or :inverse * Params:
* Do forward or inverse transform. The result is not scaled. * * flags - transformation flags
* * :scale * * nonzero_rows - when the parameter is not zero, the function assumes that only
* Scale the result: divide it by the number of array elements. * the first <i>nonzero_rows</i> rows of the input array (CV_DXT_INVERSE is not set)
* * :rows * or only the first nonzero_rows of the output array (CV_DXT_INVERSE is set) contain non-zeros.
* Do forward or inverse transform of every individual row of the self.
* This flag allow user to transofrm multiple vectors simulaneously and can be used to decrease the overhand
* (which sometimes several times larger then the processing itself), to do 3D and higher-dimensional transforms etc.
*
* e.g.
* mat.dft(:inverse)
* mat.dft(:forward, :scale) etc...
*/ */
VALUE VALUE
rb_dft(int argc, VALUE *argv, VALUE self) rb_dft(int argc, VALUE *argv, VALUE self)
{ {
int type = CV_DXT_FORWARD; VALUE flag_value, nonzero_row_value;
int num_rows = 0; rb_scan_args(argc, argv, "02", &flag_value, &nonzero_row_value);
if (argc > 0) {
int num_flags = argc; int flags = NIL_P(flag_value) ? CV_DXT_FORWARD : NUM2INT(flag_value);
if (FIXNUM_P(argv[argc -1])) { int nonzero_rows = NIL_P(nonzero_row_value) ? 0 : NUM2INT(nonzero_row_value);
num_flags = argc - 1;
num_rows = FIX2INT(argv[argc - 1]);
}
for (int i = 0; i < num_flags; ++i) {
type |= CVMETHOD("DXT_FLAG", argv[i]);
}
}
CvArr* self_ptr = CVARR(self); CvArr* self_ptr = CVARR(self);
VALUE dest = Qnil; VALUE dest = Qnil;
try { try {
dest = new_mat_kind_object(cvGetSize(self_ptr), self); dest = new_mat_kind_object(cvGetSize(self_ptr), self);
cvDFT(self_ptr, CVARR(dest), type, num_rows); cvDFT(self_ptr, CVARR(dest), flags, nonzero_rows);
} }
catch (cv::Exception& e) { catch (cv::Exception& e) {
raise_cverror(e); raise_cverror(e);
@ -3001,32 +2986,25 @@ rb_dft(int argc, VALUE *argv, VALUE self)
/* /*
* call-seq: * call-seq:
* dct(<i>anyflags...</i>) -> cvmat * dct(flags = CV_DXT_FORWARD) -> cvmat
* *
* Performs forward or inverse Discrete Cosine Transform(DCT) of 1D or 2D floating-point array. * Performs forward or inverse Discrete Cosine Transform(DCT) of 1D or 2D floating-point array.
* Argument should be following symbol or combination of these.
* *
* * :forward or :inverse * Params:
* Do forward or inverse transform. * * flags - transformation flags
* * :rows
* Do forward or inverse transform of every individual row of the self.
* This flag allow user to transofrm multiple vectors simulaneously and can be used to decrease the overhand
* (which sometimes several times larger then the processing itself), to do 3D and higher-dimensional transforms etc.
*/ */
VALUE VALUE
rb_dct(int argc, VALUE *argv, VALUE self) rb_dct(int argc, VALUE *argv, VALUE self)
{ {
int type = CV_DXT_FORWARD; VALUE flag_value;
if (argc > 0) { rb_scan_args(argc, argv, "01", &flag_value);
for (int i = 0; i < argc; ++i) {
type |= CVMETHOD("DXT_FLAG", argv[i]); int flags = NIL_P(flag_value) ? CV_DXT_FORWARD : NUM2INT(flag_value);
}
}
CvArr* self_ptr = CVARR(self); CvArr* self_ptr = CVARR(self);
VALUE dest = Qnil; VALUE dest = Qnil;
try { try {
dest = new_mat_kind_object(cvGetSize(self_ptr), self); dest = new_mat_kind_object(cvGetSize(self_ptr), self);
cvDCT(self_ptr, CVARR(dest), type); cvDCT(self_ptr, CVARR(dest), flags);
} }
catch (cv::Exception& e) { catch (cv::Exception& e) {
raise_cverror(e); raise_cverror(e);

View file

@ -322,6 +322,15 @@ define_ruby_module()
rb_define_const(rb_module, "CV_COMP_INTERSECT", INT2FIX(CV_COMP_INTERSECT)); rb_define_const(rb_module, "CV_COMP_INTERSECT", INT2FIX(CV_COMP_INTERSECT));
rb_define_const(rb_module, "CV_COMP_BHATTACHARYYA", INT2FIX(CV_COMP_BHATTACHARYYA)); rb_define_const(rb_module, "CV_COMP_BHATTACHARYYA", INT2FIX(CV_COMP_BHATTACHARYYA));
/* DFT and DCT flags */
rb_define_const(rb_module, "CV_DXT_FORWARD", INT2FIX(CV_DXT_FORWARD));
rb_define_const(rb_module, "CV_DXT_INVERSE", INT2FIX(CV_DXT_INVERSE));
rb_define_const(rb_module, "CV_DXT_SCALE", INT2FIX(CV_DXT_SCALE));
rb_define_const(rb_module, "CV_DXT_INV_SCALE", INT2FIX(CV_DXT_INV_SCALE));
rb_define_const(rb_module, "CV_DXT_INVERSE_SCALE", INT2FIX(CV_DXT_INVERSE_SCALE));
rb_define_const(rb_module, "CV_DXT_ROWS", INT2FIX(CV_DXT_ROWS));
VALUE inversion_method = rb_hash_new(); VALUE inversion_method = rb_hash_new();
/* {:lu, :svd, :svd_sym(:svd_symmetric)}: Inversion method */ /* {:lu, :svd, :svd_sym(:svd_symmetric)}: Inversion method */
rb_define_const(rb_module, "INVERSION_METHOD", inversion_method); rb_define_const(rb_module, "INVERSION_METHOD", inversion_method);
@ -330,14 +339,6 @@ define_ruby_module()
REGISTER_CVMETHOD(inversion_method, "svd_sym", CV_SVD_SYM); REGISTER_CVMETHOD(inversion_method, "svd_sym", CV_SVD_SYM);
REGISTER_CVMETHOD(inversion_method, "svd_symmetric", CV_SVD_SYM); REGISTER_CVMETHOD(inversion_method, "svd_symmetric", CV_SVD_SYM);
VALUE dxt_flag = rb_hash_new();
/* {:forward, :inverse, :scale, :rows}: DFT and DCT flags */
rb_define_const(rb_module, "DXT_FLAG", dxt_flag);
REGISTER_CVMETHOD(dxt_flag, "forward", CV_DXT_FORWARD);
REGISTER_CVMETHOD(dxt_flag, "inverse", CV_DXT_INVERSE);
REGISTER_CVMETHOD(dxt_flag, "scale", CV_DXT_SCALE);
REGISTER_CVMETHOD(dxt_flag, "rows", CV_DXT_ROWS);
VALUE interpolation_method = rb_hash_new(); VALUE interpolation_method = rb_hash_new();
/* {:nn, :linear, :area, :cubic}: Interpolation method */ /* {:nn, :linear, :area, :cubic}: Interpolation method */
rb_define_const(rb_module, "INTERPOLATION_METHOD", interpolation_method); rb_define_const(rb_module, "INTERPOLATION_METHOD", interpolation_method);

View file

@ -17,9 +17,9 @@ class TestCvMat_dxt < OpenCVTestCase
CvScalar.new(s, s) CvScalar.new(s, s)
} }
mat1 = mat0.dft(:forward) mat1 = mat0.dft(CV_DXT_FORWARD)
mat2 = mat0.dft(:forward, :scale) mat2 = mat0.dft(CV_DXT_FORWARD | CV_DXT_SCALE)
mat3 = mat0.dft(:forward, :scale).dft(:inverse) mat3 = mat0.dft(CV_DXT_FORWARD | CV_DXT_SCALE).dft(CV_DXT_INVERSE)
n.times { |j| n.times { |j|
if j == 1 if j == 1
assert_in_delta(n / 2, mat1[j, 0][0], 0.001) assert_in_delta(n / 2, mat1[j, 0][0], 0.001)
@ -44,6 +44,9 @@ class TestCvMat_dxt < OpenCVTestCase
assert_raise(TypeError) { assert_raise(TypeError) {
mat0.dft(DUMMY_OBJ) mat0.dft(DUMMY_OBJ)
} }
assert_raise(TypeError) {
mat0.dft(CV_DXT_FORWARD, DUMMY_OBJ)
}
end end
def test_dft_2D def test_dft_2D
@ -59,9 +62,9 @@ class TestCvMat_dxt < OpenCVTestCase
c += 1 c += 1
} }
mat1 = mat0.dft(:forward) mat1 = mat0.dft(CV_DXT_FORWARD)
mat2 = mat0.dft(:forward, :scale) mat2 = mat0.dft(CV_DXT_FORWARD | CV_DXT_SCALE)
mat3 = mat0.dft(:forward, :scale).dft(:inverse) mat3 = mat0.dft(CV_DXT_FORWARD | CV_DXT_SCALE).dft(CV_DXT_INVERSE)
n.times { |j| n.times { |j|
n.times { |i| n.times { |i|
if i == 0 and j == 1 if i == 0 and j == 1
@ -95,8 +98,8 @@ class TestCvMat_dxt < OpenCVTestCase
CvScalar.new(s) CvScalar.new(s)
} }
mat1 = mat0.dct mat1 = mat0.dct(CV_DXT_FORWARD)
mat2 = mat0.dct(:forward).dct(:inverse) mat2 = mat0.dct(CV_DXT_FORWARD).dct(CV_DXT_INVERSE)
expected1 = [0, 1.599647, -0.765367, -0.906127, 0, -0.180240, 0, -0.042290] expected1 = [0, 1.599647, -0.765367, -0.906127, 0, -0.180240, 0, -0.042290]
n.times { |j| n.times { |j|
assert_in_delta(expected1[j], mat1[j, 0][0], 0.001) assert_in_delta(expected1[j], mat1[j, 0][0], 0.001)
@ -121,8 +124,8 @@ class TestCvMat_dxt < OpenCVTestCase
c += 1 c += 1
} }
mat1 = mat0.dct(:forward) mat1 = mat0.dct(CV_DXT_FORWARD)
mat2 = mat0.dct(:forward).dct(:inverse) mat2 = mat0.dct(CV_DXT_FORWARD).dct(CV_DXT_INVERSE)
n.times { |j| n.times { |j|
n.times { |i| n.times { |i|
if i == 0 and j == 1 if i == 0 and j == 1

View file

@ -164,12 +164,6 @@ class TestOpenCV < OpenCVTestCase
assert_equal(2, INVERSION_METHOD[:svd_sym]) assert_equal(2, INVERSION_METHOD[:svd_sym])
assert_equal(2, INVERSION_METHOD[:svd_symmetric]) assert_equal(2, INVERSION_METHOD[:svd_symmetric])
# Flags for DFT and DCT
assert_equal(0, DXT_FLAG[:forward])
assert_equal(1, DXT_FLAG[:inverse])
assert_equal(2, DXT_FLAG[:scale])
assert_equal(4, DXT_FLAG[:rows])
# Interpolation methods # Interpolation methods
assert_equal(0, INTERPOLATION_METHOD[:nn]) assert_equal(0, INTERPOLATION_METHOD[:nn])
assert_equal(1, INTERPOLATION_METHOD[:linear]) assert_equal(1, INTERPOLATION_METHOD[:linear])