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

add function setting roi using Mat#new

This commit is contained in:
ser1zw 2016-04-10 05:22:49 +09:00
parent c21f32d356
commit 12734952ad
3 changed files with 35 additions and 8 deletions

View file

@ -6,7 +6,7 @@
#include "mat_imgproc.hpp" #include "mat_imgproc.hpp"
#include "mat_drawing.hpp" #include "mat_drawing.hpp"
#include "scalar.hpp" #include "scalar.hpp"
#include "rect.hpp"
#include "error.hpp" #include "error.hpp"
/* /*
@ -73,26 +73,40 @@ namespace rubyopencv {
* @param type [Integer] * @param type [Integer]
* The type of the matrix elements in the form of constant <b><tt>CV_<bit depth><S|U|F></tt></b>. * The type of the matrix elements in the form of constant <b><tt>CV_<bit depth><S|U|F></tt></b>.
* @return [Mat] Created matrix * @return [Mat] Created matrix
* @overload new(m, roi)
* @param m [Mat] Array that (as a whole or partly) is assigned to the constructed matrix.
* No data is copied by these constructors. Instead, the header pointing to m data or its sub-array
* is constructed and associated with it. The reference counter, if any, is incremented.
* So, when you modify the matrix formed using such a constructor, you also modify the corresponding
* elements of m.
* @param roi [Rect] Regeion of interest.
* @return [Mat] Created matrix
* @opencv_func cv::Mat * @opencv_func cv::Mat
* @example * @example
* mat1 = Mat.new(3, 4) # Creates a 3-channels 3x4 matrix whose elements are 8bit unsigned. * mat1 = Mat.new(3, 4) # Creates a 3-channels 3x4 matrix whose elements are 8bit unsigned.
* mat2 = Mat.new(5, 6, CV_32F) # Creates a 1-channel 5x6 matrix whose elements are 32bit float. * mat2 = Mat.new(5, 6, CV_32F) # Creates a 1-channel 5x6 matrix whose elements are 32bit float.
*/ */
VALUE rb_initialize(int argc, VALUE *argv, VALUE self) { VALUE rb_initialize(int argc, VALUE *argv, VALUE self) {
VALUE row, column, type; VALUE v1, v2, type;
rb_scan_args(argc, argv, "21", &row, &column, &type); rb_scan_args(argc, argv, "21", &v1, &v2, &type);
cv::Mat* dataptr = NULL; cv::Mat* dataptr = NULL;
try { try {
cv::Mat tempdata(NUM2INT(row), NUM2INT(column), (NIL_P(type) ? CV_8UC1 : NUM2INT(type))); if (RTEST(rb_obj_is_kind_of(v1, rb_cNumeric))) {
if (tempdata.empty()) { dataptr = new cv::Mat(NUM2INT(v1), NUM2INT(v2), (NIL_P(type) ? CV_8UC1 : NUM2INT(type)));
}
else {
cv::Mat* matptr = obj2mat(v1);
cv::Rect* rectptr = Rect::obj2rect(v2);
dataptr = new cv::Mat(*matptr, *rectptr);
}
if (dataptr->empty()) {
delete dataptr;
rb_raise(rb_eNoMemError, "Failed to create matrix"); rb_raise(rb_eNoMemError, "Failed to create matrix");
return Qnil; return Qnil;
} }
dataptr = new cv::Mat();
tempdata.copyTo(*dataptr);
RTYPEDDATA_DATA(self) = dataptr; RTYPEDDATA_DATA(self) = dataptr;
} }
catch (cv::Exception& e) { catch (cv::Exception& e) {

View file

@ -7,6 +7,7 @@ namespace rubyopencv {
namespace Rect { namespace Rect {
void init(); void init();
VALUE rect2obj (cv::Rect rect); VALUE rect2obj (cv::Rect rect);
cv::Rect* obj2rect(VALUE obj);
} }
} }

View file

@ -29,6 +29,18 @@ class TestMat < OpenCVTestCase
} }
} }
# ROI
m0 = Mat.zeros(5, 5, CV_8U)
roi = Rect.new(0, 0, 2, 3)
m = Mat.new(m0, roi)
assert_equal(roi.height, m.rows)
assert_equal(roi.width, m.cols)
assert_equal(m0.depth, m.depth)
assert_equal(m0.dims, m.dims)
assert_equal(m0.channels, m.channels)
m.set_to(Scalar.new(1))
assert_equal(m[0, 0][0], m0[0, 0][0])
assert_raise(TypeError) { assert_raise(TypeError) {
m = Mat.new(DUMMY_OBJ, 20, CV_8U) m = Mat.new(DUMMY_OBJ, 20, CV_8U)
} }