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

add IplImage#decode (wrapper of cvDecodeImage)

This commit is contained in:
ser1zw 2012-07-15 03:14:39 +09:00
parent 35537b6248
commit 36f6864dfa
5 changed files with 108 additions and 15 deletions

View file

@ -537,31 +537,21 @@ rb_encode_imageM(int argc, VALUE *argv, VALUE self)
return array;
}
/*
* call-seq:
* decode_image(buf[, iscolor=CV_LOAD_IMAGE_COLOR]) -> CvMat
*
* Reads an image from a buffer in memory.
*
* Parameters:
* buf <CvMat, Array> - Input array
* iscolor <Integer> - Flags specifying the color type of a decoded image (the same flags as CvMat#load)
*/
VALUE
rb_decode_imageM(int argc, VALUE *argv, VALUE self)
CvMat*
prepare_decoding(int argc, VALUE *argv, int* iscolor, int* need_release)
{
VALUE _buff, _iscolor;
rb_scan_args(argc, argv, "11", &_buff, &_iscolor);
int iscolor = NIL_P(_iscolor) ? CV_LOAD_IMAGE_COLOR : NUM2INT(_iscolor);
*iscolor = NIL_P(_iscolor) ? CV_LOAD_IMAGE_COLOR : NUM2INT(_iscolor);
CvMat* buff = NULL;
int need_release = 0;
*need_release = 0;
switch (TYPE(_buff)) {
case T_STRING:
_buff = rb_funcall(_buff, rb_intern("unpack"), 1, rb_str_new("c*", 2));
case T_ARRAY: {
int cols = RARRAY_LEN(_buff);
need_release = 1;
*need_release = 1;
try {
buff = rb_cvCreateMat(1, cols, CV_8UC1);
VALUE *ary_ptr = RARRAY_PTR(_buff);
@ -583,6 +573,24 @@ rb_decode_imageM(int argc, VALUE *argv, VALUE self)
raise_typeerror(_buff, "CvMat, Array or String");
}
return buff;
}
/*
* call-seq:
* decode_image(buf[, iscolor=CV_LOAD_IMAGE_COLOR]) -> CvMat
*
* Reads an image from a buffer in memory.
*
* Parameters:
* buf <CvMat, Array> - Input array
* iscolor <Integer> - Flags specifying the color type of a decoded image (the same flags as CvMat#load)
*/
VALUE
rb_decode_imageM(int argc, VALUE *argv, VALUE self)
{
int iscolor, need_release;
CvMat* buff = prepare_decoding(argc, argv, &iscolor, &need_release);
CvMat* mat_ptr = NULL;
try {
mat_ptr = cvDecodeImageM(buff, iscolor);

View file

@ -244,6 +244,8 @@ VALUE new_object(CvSize size, int type);
VALUE new_mat_kind_object(CvSize size, VALUE ref_obj);
VALUE new_mat_kind_object(CvSize size, VALUE ref_obj, int cvmat_depth, int channel);
CvMat* prepare_decoding(int argc, VALUE *argv, int* iscolor, int* need_release);
__NAMESPACE_END_CVMAT
inline CvMat*

View file

@ -62,6 +62,9 @@ define_ruby_class()
rb_define_method(rb_klass, "reset_coi", RUBY_METHOD_FUNC(rb_reset_coi), 0);
rb_define_method(rb_klass, "smoothness", RUBY_METHOD_FUNC(rb_smoothness), -1);
rb_define_singleton_method(rb_klass, "decode_image", RUBY_METHOD_FUNC(rb_decode_image), -1);
rb_define_alias(rb_singleton_class(rb_klass), "decode", "decode_image");
}
VALUE
@ -133,6 +136,35 @@ rb_load_image(int argc, VALUE *argv, VALUE self)
return OPENCV_OBJECT(rb_klass, image);
}
/*
* call-seq:
* decode_image(buf[, iscolor=CV_LOAD_IMAGE_COLOR]) -> CvMat
*
* Reads an image from a buffer in memory.
*
* Parameters:
* buf <CvMat, Array, String> - Input array
* iscolor <Integer> - Flags specifying the color type of a decoded image (the same flags as CvMat#load)
*/
VALUE
rb_decode_image(int argc, VALUE *argv, VALUE self)
{
int iscolor, need_release;
CvMat* buff = cCvMat::prepare_decoding(argc, argv, &iscolor, &need_release);
IplImage* img_ptr = NULL;
try {
img_ptr = cvDecodeImage(buff, iscolor);
if (need_release) {
cvReleaseMat(&buff);
}
}
catch (cv::Exception& e) {
raise_cverror(e);
}
return OPENCV_OBJECT(rb_klass, img_ptr);
}
/*
* Get ROI as CvRect.
*/

View file

@ -26,6 +26,7 @@ VALUE rb_allocate(VALUE klass);
VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
VALUE rb_load_image(int argc, VALUE *argv, VALUE self);
VALUE rb_decode_image(int argc, VALUE *argv, VALUE self);
VALUE rb_color_model(VALUE self);

View file

@ -68,6 +68,56 @@ class TestIplImage < OpenCVTestCase
}
end
def test_decode
data = nil
open(FILENAME_CAT, 'rb') { |f|
data = f.read
}
data_ary = data.unpack("c*")
data_mat = CvMat.new(1, data_ary.size).set_data(data_ary)
expected = IplImage.load(FILENAME_CAT)
img1 = IplImage.decode(data)
img2 = IplImage.decode(data_ary)
img3 = IplImage.decode(data_mat)
img4 = IplImage.decode(data, CV_LOAD_IMAGE_COLOR)
img5 = IplImage.decode(data_ary, CV_LOAD_IMAGE_COLOR)
img6 = IplImage.decode(data_mat, CV_LOAD_IMAGE_COLOR)
expected_hash = hash_img(expected)
[img1, img2, img3, img4, img5, img6].each { |img|
assert_equal(IplImage, img.class)
assert_equal(expected.rows, img.rows)
assert_equal(expected.cols, img.cols)
assert_equal(expected.channel, img.channel)
assert_equal(expected_hash, hash_img(img))
}
expected_c1 = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_GRAYSCALE)
img1c1 = IplImage.decode(data, CV_LOAD_IMAGE_GRAYSCALE)
img2c1 = IplImage.decode(data_ary, CV_LOAD_IMAGE_GRAYSCALE)
img3c1 = IplImage.decode(data_mat, CV_LOAD_IMAGE_GRAYSCALE)
expected_hash_c1 = hash_img(expected_c1)
[img1c1, img2c1, img3c1].each { |img|
assert_equal(IplImage, img.class)
assert_equal(expected_c1.rows, img.rows)
assert_equal(expected_c1.cols, img.cols)
assert_equal(expected_c1.channel, img.channel)
assert_equal(expected_hash_c1, hash_img(img))
}
assert_raise(TypeError) {
IplImage.decode(DUMMY_OBJ)
}
assert_raise(TypeError) {
IplImage.decode(data, DUMMY_OBJ)
}
# Uncomment the following line to show the result images
# snap img1, img2, img3
end
def test_roi
img = IplImage.new(20, 30)
rect = img.roi