From 8636eca0c47d1743887d3e98be4eb9f0ca5d3e20 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sat, 13 Aug 2016 02:37:23 +0900 Subject: [PATCH] add Mat::circle --- ext/opencv/mat.cpp | 3 ++ ext/opencv/mat_drawing.cpp | 59 ++++++++++++++++++++++++++++++++++++++ ext/opencv/mat_drawing.hpp | 3 ++ test/test_mat_drawing.rb | 59 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100755 test/test_mat_drawing.rb diff --git a/ext/opencv/mat.cpp b/ext/opencv/mat.cpp index 45e6077..69a1e79 100644 --- a/ext/opencv/mat.cpp +++ b/ext/opencv/mat.cpp @@ -1238,6 +1238,9 @@ namespace rubyopencv { rb_define_method(rb_klass, "line", RUBY_METHOD_FUNC(rb_line), -1); // in ext/opencv/mat_drawing.cpp rb_define_method(rb_klass, "line!", RUBY_METHOD_FUNC(rb_line_bang), -1); // in ext/opencv/mat_drawing.cpp + rb_define_method(rb_klass, "circle", RUBY_METHOD_FUNC(rb_circle), -1); // in ext/opencv/mat_drawing.cpp + rb_define_method(rb_klass, "circle!", RUBY_METHOD_FUNC(rb_circle_bang), -1); // in ext/opencv/mat_drawing.cpp + rb_define_method(rb_klass, "rectangle", RUBY_METHOD_FUNC(rb_rectangle), -1); // in ext/opencv/mat_drawing.cpp rb_define_method(rb_klass, "rectangle!", RUBY_METHOD_FUNC(rb_rectangle_bang), -1); // in ext/opencv/mat_drawing.cpp diff --git a/ext/opencv/mat_drawing.cpp b/ext/opencv/mat_drawing.cpp index 29ca1be..b45a0b2 100644 --- a/ext/opencv/mat_drawing.cpp +++ b/ext/opencv/mat_drawing.cpp @@ -154,5 +154,64 @@ namespace rubyopencv { } return self; } + + /* + * Draws a circle + * + * @overload circle(center, radius, options = nil) + * @param center [Point] Center of the circle. + * @param radius [Integer] Radius of the circle. + * @param color [Scalar] Circle color. + * @param options [Hash] Drawing options + * @option options [Integer] :thickness Line thickness. + * @option options [Integer] :line_type Type of the line. + * * 8 - 8-connected line. + * * 4 - 4-connected line. + * * CV_AA - Antialiased line. + * @option options [Integer] :shift Number of fractional bits in the coordinates of + * the center and in the radius value. + * @return [Mat] Output array + * @opencv_func cv::circle + */ + VALUE rb_circle(int argc, VALUE *argv, VALUE self) { + VALUE dst = rb_clone(self); + return rb_circle_bang(argc, argv, dst); + } + + /* + * Draws a circle + * + * @overload circle!(center, radius, options = nil) + * @param center [Point] Center of the circle. + * @param radius [Integer] Radius of the circle. + * @param color [Scalar] Circle color. + * @param options [Hash] Drawing options + * @option options [Integer] :thickness Line thickness. + * @option options [Integer] :line_type Type of the line. + * * 8 - 8-connected line. + * * 4 - 4-connected line. + * * CV_AA - Antialiased line. + * @option options [Integer] :shift Number of fractional bits in the coordinates of + * the center and in the radius value. + * @return [Mat] Output array + * @opencv_func cv::circle + */ + VALUE rb_circle_bang(int argc, VALUE *argv, VALUE self) { + VALUE center, radius, color, option; + rb_scan_args(argc, argv, "31", ¢er, &radius, &color, &option); + + cv::Mat* selfptr = obj2mat(self); + drawing_option_t opt = drawing_option(option); + try { + cv::Point center_value = Point::conpatible_obj2point(center); + cv::Scalar color_value = *(Scalar::obj2scalar(color)); + cv::circle(*selfptr, center_value, NUM2INT(radius), color_value, opt.thickness, opt.line_type, opt.shift); + } + catch (cv::Exception& e) { + Error::raise(e); + } + + return self; + } } } diff --git a/ext/opencv/mat_drawing.hpp b/ext/opencv/mat_drawing.hpp index 21f27b9..2110698 100644 --- a/ext/opencv/mat_drawing.hpp +++ b/ext/opencv/mat_drawing.hpp @@ -7,5 +7,8 @@ namespace rubyopencv { VALUE rb_rectangle(int argc, VALUE *argv, VALUE self); VALUE rb_rectangle_bang(int argc, VALUE *argv, VALUE self); + + VALUE rb_circle(int argc, VALUE *argv, VALUE self); + VALUE rb_circle_bang(int argc, VALUE *argv, VALUE self); } } diff --git a/test/test_mat_drawing.rb b/test/test_mat_drawing.rb new file mode 100755 index 0000000..3c4623f --- /dev/null +++ b/test/test_mat_drawing.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8 -*- +require 'opencv' +require File.expand_path(File.dirname(__FILE__)) + '/helper' + +include Cv + +class TestMat < OpenCVTestCase + def test_circle + m0 = Cv::Mat.zeros(128, 128, Cv::CV_8UC3) + Cv::Scalar.new(255, 255, 255) + center = Cv::Point.new(m0.cols * 0.5, m0.rows * 0.5) + radius = 32 + color = Cv::Scalar.new(255, 0, 0) + option = { thickness: 2, line_type: Cv::CV_AA, shift: 0 } + + m1 = m0.circle(center, radius, color, option) + assert_equal(m0.class, m1.class) + assert_equal(m0.rows, m1.rows) + assert_equal(m0.cols, m1.cols) + + m2 = m0.clone + m2.circle!(center, radius, color, option) + assert_equal(m0.class, m2.class) + assert_equal(m0.rows, m2.rows) + assert_equal(m0.cols, m2.cols) + + assert_raise(TypeError) { + m0.circle(DUMMY_OBJ, radius, color, option) + } + assert_raise(TypeError) { + m0.circle(center, DUMMY_OBJ, color, option) + } + assert_raise(TypeError) { + m0.circle(center, radius, DUMMY_OBJ, option) + } + assert_raise(TypeError) { + m0.circle(center, radius, color, DUMMY_OBJ) + } + + assert_raise(TypeError) { + m0.circle!(DUMMY_OBJ, radius, color, option) + } + assert_raise(TypeError) { + m0.circle!(center, DUMMY_OBJ, color, option) + } + assert_raise(TypeError) { + m0.circle!(center, radius, DUMMY_OBJ, option) + } + assert_raise(TypeError) { + m0.circle!(center, radius, color, DUMMY_OBJ) + } + + # w1 = Window.new('Circle 1') + # w1.show m1 + # w2 = Window.new('Circle 2') + # w2.show m2 + # Cv::wait_key + end +end