From 37182e83c1668a61bcc8985176e4fef354d6ec3d Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 29 May 2011 03:36:09 +0900 Subject: [PATCH] added cvGetCaptureProperty wrapping methods to CvCapture --- .gitignore | 1 + ext/opencv/cvcapture.cpp | 125 +++++++++++++++++++++++++++++++-- ext/opencv/cvcapture.h | 14 +++- test/helper.rb | 1 + test/test_cvcapture.rb | 148 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 282 insertions(+), 7 deletions(-) create mode 100755 test/test_cvcapture.rb diff --git a/.gitignore b/.gitignore index 1b48dba..79fe4fc 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ ruby-1.9.2-p180 ext/opencv/test.txt pkg/ log.txt +*.avi diff --git a/ext/opencv/cvcapture.cpp b/ext/opencv/cvcapture.cpp index 840800b..e80c68e 100644 --- a/ext/opencv/cvcapture.cpp +++ b/ext/opencv/cvcapture.cpp @@ -7,7 +7,7 @@ Copyright (C) 2005-2006 Masakazu Yonekura ************************************************************/ -#include"cvcapture.h" +#include "cvcapture.h" /* * Document-class: OpenCV::CvCapture * @@ -67,18 +67,30 @@ define_ruby_class() rb_define_method(rb_klass, "query", RUBY_METHOD_FUNC(rb_query), 0); rb_define_method(rb_klass, "millisecond", RUBY_METHOD_FUNC(rb_millisecond), 0); rb_define_method(rb_klass, "frames", RUBY_METHOD_FUNC(rb_frames), 0); + rb_define_method(rb_klass, "avi_ratio", RUBY_METHOD_FUNC(rb_avi_ratio), 0); rb_define_method(rb_klass, "size", RUBY_METHOD_FUNC(rb_size), 0); rb_define_method(rb_klass, "width", RUBY_METHOD_FUNC(rb_width), 0); rb_define_method(rb_klass, "height", RUBY_METHOD_FUNC(rb_height), 0); rb_define_method(rb_klass, "fps", RUBY_METHOD_FUNC(rb_fps), 0); rb_define_method(rb_klass, "fourcc", RUBY_METHOD_FUNC(rb_fourcc), 0); rb_define_method(rb_klass, "frame_count", RUBY_METHOD_FUNC(rb_frame_count), 0); + rb_define_method(rb_klass, "format", RUBY_METHOD_FUNC(rb_format), 0); + rb_define_method(rb_klass, "mode", RUBY_METHOD_FUNC(rb_mode), 0); + rb_define_method(rb_klass, "brightness", RUBY_METHOD_FUNC(rb_brightness), 0); + rb_define_method(rb_klass, "contrast", RUBY_METHOD_FUNC(rb_contrast), 0); + rb_define_method(rb_klass, "saturation", RUBY_METHOD_FUNC(rb_saturation), 0); + rb_define_method(rb_klass, "hue", RUBY_METHOD_FUNC(rb_hue), 0); + rb_define_method(rb_klass, "gain", RUBY_METHOD_FUNC(rb_gain), 0); + rb_define_method(rb_klass, "exposure", RUBY_METHOD_FUNC(rb_exposure), 0); + rb_define_method(rb_klass, "convert_rgb", RUBY_METHOD_FUNC(rb_convert_rgb), 0); + rb_define_method(rb_klass, "white_balance", RUBY_METHOD_FUNC(rb_white_balance), 0); + rb_define_method(rb_klass, "rectification", RUBY_METHOD_FUNC(rb_rectification), 0); } void free(void *ptr) { - if(ptr) + if (ptr) cvReleaseCapture((CvCapture**)&ptr); } @@ -147,7 +159,7 @@ rb_grab(VALUE self) /* * call-seq: - * retrieve -> CvMat or nil + * retrieve -> IplImage or nil * * Gets the image grabbed with grab. */ @@ -168,7 +180,7 @@ rb_retrieve(VALUE self) /* * call-seq: - * query -> CvMat or nil + * query -> IplImage or nil * * Grabs and returns a frame camera or file. Just a combination of grab and retrieve in one call. */ @@ -181,7 +193,8 @@ rb_query(VALUE self) VALUE image = cIplImage::new_object(cvSize(frame->width, frame->height), CV_MAKETYPE(CV_8U, frame->nChannels)); if (frame->origin == IPL_ORIGIN_TL) { cvCopy(frame, CVARR(image)); - } else { + } + else { cvFlip(frame, CVARR(image)); } return image; @@ -220,7 +233,8 @@ rb_avi_ratio(VALUE self) VALUE rb_size(VALUE self) { - return cCvSize::new_object(cvSize((int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_HEIGHT))); + return cCvSize::new_object(cvSize((int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_WIDTH), + (int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_HEIGHT))); } /* @@ -271,6 +285,105 @@ rb_frame_count(VALUE self) return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_COUNT)); } +/* + * The format of the Mat objects returned by CvCapture#retrieve + */ +VALUE +rb_format(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FORMAT)); +} + +/* + * A backend-specific value indicating the current capture mode + */ +VALUE +rb_mode(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_MODE)); +} + +/* + * Brightness of the image (only for cameras) + */ +VALUE +rb_brightness(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_BRIGHTNESS)); +} + +/* + * Contrast of the image (only for cameras) + */ +VALUE +rb_contrast(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_CONTRAST)); +} + +/* + * Saturation of the image (only for cameras) + */ +VALUE +rb_saturation(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_SATURATION)); +} + +/* + * Hue of the image (only for cameras) + */ +VALUE +rb_hue(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_HUE)); +} + +/* + * Gain of the image (only for cameras) + */ +VALUE +rb_gain(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_GAIN)); +} + +/* + * Exposure (only for cameras) + */ +VALUE +rb_exposure(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_EXPOSURE)); +} + +/* + * Boolean flags indicating whether images should be converted to RGB + */ +VALUE +rb_convert_rgb(VALUE self) +{ + int flag = (int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_CONVERT_RGB); + return (flag == 1) ? Qtrue : Qfalse; +} + +/* + * Currently unsupported + */ +VALUE +rb_white_balance(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_WHITE_BALANCE)); +} + +/* + * TOWRITE (note: only supported by DC1394 v 2.x backend currently) + */ +VALUE +rb_rectification(VALUE self) +{ + return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_RECTIFICATION)); +} __NAMESPACE_END_CVCAPTURE __NAMESPACE_END_OPENCV diff --git a/ext/opencv/cvcapture.h b/ext/opencv/cvcapture.h index 1363852..f72bdd4 100644 --- a/ext/opencv/cvcapture.h +++ b/ext/opencv/cvcapture.h @@ -39,11 +39,23 @@ VALUE rb_height(VALUE self); VALUE rb_fps(VALUE self); VALUE rb_fourcc(VALUE self); VALUE rb_frame_count(VALUE self); +VALUE rb_format(VALUE self); +VALUE rb_mode(VALUE self); +VALUE rb_brightness(VALUE self); +VALUE rb_contrast(VALUE self); +VALUE rb_saturation(VALUE self); +VALUE rb_hue(VALUE self); +VALUE rb_gain(VALUE self); +VALUE rb_exposure(VALUE self); +VALUE rb_convert_rgb(VALUE self); +VALUE rb_white_balance(VALUE self); +VALUE rb_rectification(VALUE self); __NAMESPACE_END_CVCAPTURE -inline CvCapture *CVCAPTURE(VALUE object){ +inline CvCapture* +CVCAPTURE(VALUE object) { CvCapture *ptr; Data_Get_Struct(object, CvCapture, ptr); return ptr; diff --git a/test/helper.rb b/test/helper.rb index 617f2e9..7c05be6 100755 --- a/test/helper.rb +++ b/test/helper.rb @@ -10,6 +10,7 @@ class OpenCVTestCase < Test::Unit::TestCase FILENAME_CAT = SAMPLE_DIR + 'cat.jpg' FILENAME_LENA256x256 = SAMPLE_DIR + 'lena-256x256.jpg' HAARCASCADE_FRONTALFACE_ALT = SAMPLE_DIR + 'haarcascade_frontalface_alt.xml.gz' + AVI_SAMPLE = SAMPLE_DIR + 'movie_sample.avi' CvMat.class_eval do # Range check for debug diff --git a/test/test_cvcapture.rb b/test/test_cvcapture.rb new file mode 100755 index 0000000..07d1b71 --- /dev/null +++ b/test/test_cvcapture.rb @@ -0,0 +1,148 @@ +#!/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 OpenCV::CvCapture +class TestCvCapture < OpenCVTestCase + def setup + @cap = CvCapture.open(AVI_SAMPLE) + end + + def teardown + @cap = nil + end + + def test_INTERFACE + assert_equal(CvCapture::INTERFACE[:any], 0) + assert_equal(CvCapture::INTERFACE[:mil], 100) + assert_equal(CvCapture::INTERFACE[:vfw], 200) + assert_equal(CvCapture::INTERFACE[:v4l], 200) + assert_equal(CvCapture::INTERFACE[:v4l2], 200) + assert_equal(CvCapture::INTERFACE[:fireware], 300) + assert_equal(CvCapture::INTERFACE[:ieee1394], 300) + assert_equal(CvCapture::INTERFACE[:dc1394], 300) + assert_equal(CvCapture::INTERFACE[:cmu1394], 300) + assert_equal(CvCapture::INTERFACE[:stereo], 400) + assert_equal(CvCapture::INTERFACE[:tyzx], 400) + assert_equal(CvCapture::INTERFACE[:tyzx_left], 400) + assert_equal(CvCapture::INTERFACE[:tyzx_right], 401) + assert_equal(CvCapture::INTERFACE[:tyzx_color], 402) + assert_equal(CvCapture::INTERFACE[:tyzx_z], 403) + assert_equal(CvCapture::INTERFACE[:qt], 500) + assert_equal(CvCapture::INTERFACE[:quicktime], 500) + end + + def test_open + cap1 = CvCapture.open(AVI_SAMPLE) + assert_equal(CvCapture, cap1.class) + + # Uncomment the following lines to test capturing from camera + # cap2 = CvCapture.open(0) + # assert_equal(CvCapture, cap2.class) + # CvCapture::INTERFACE.each { |k, v| + # cap3 = CvCapture.open(k) + # assert_equal(CvCapture, cap3.class) + # } + end + + def test_grab + assert(@cap.grab) + end + + def test_retrieve + @cap.grab + img = @cap.retrieve + assert_equal(IplImage, img.class) + end + + def test_query + img = @cap.query + assert_equal(IplImage, img.class) + end + + def test_millisecond + assert(@cap.millisecond.is_a? Numeric) + end + + def test_frames + assert(@cap.frames.is_a? Numeric) + end + + def test_avi_ratio + assert(@cap.avi_ratio.is_a? Numeric) + end + + def test_size + assert_equal(CvSize, @cap.size.class) + end + + def test_width + assert(@cap.width.is_a? Numeric) + end + + def test_height + assert(@cap.height.is_a? Numeric) + end + + def test_fps + assert(@cap.fps.is_a? Numeric) + end + + def test_fourcc + assert_equal(String, @cap.fourcc.class) + end + + def test_frame_count + assert(@cap.frame_count.is_a? Numeric) + end + + def test_format + assert(@cap.format.is_a? Numeric) + end + + def test_mode + assert(@cap.mode.is_a? Numeric) + end + + def test_brightness + assert(@cap.brightness.is_a? Numeric) + end + + def test_contrast + assert(@cap.contrast.is_a? Numeric) + end + + def test_saturation + assert(@cap.saturation.is_a? Numeric) + end + + def test_hue + assert(@cap.hue.is_a? Numeric) + end + + def test_gain + assert(@cap.gain.is_a? Numeric) + end + + def test_exposure + assert(@cap.exposure.is_a? Numeric) + end + + def test_convert_rgb + assert((@cap.convert_rgb == true) || + (@cap.convert_rgb == false)) + end + + def test_white_balance + assert(@cap.white_balance.is_a? Numeric) + end + + def test_rectification + assert(@cap.rectification.is_a? Numeric) + end +end +