ruby-opencv 0.0.10 → 0.0.11.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +6 -14
  2. data/Manifest.txt +32 -8
  3. data/README.md +1 -1
  4. data/examples/alpha_blend.rb +2 -2
  5. data/examples/contours/bounding-box-detect-canny.rb +0 -0
  6. data/examples/contours/contour_retrieval_modes.rb +0 -0
  7. data/examples/convexhull.rb +0 -0
  8. data/examples/face_detect.rb +0 -0
  9. data/examples/facerec/create_csv.rb +43 -0
  10. data/examples/facerec/facerec_eigenfaces.rb +132 -0
  11. data/examples/facerec/facerec_fisherfaces.rb +131 -0
  12. data/examples/facerec/facerec_lbph.rb +116 -0
  13. data/examples/facerec/readme.md +111 -0
  14. data/examples/find_obj.rb +2 -2
  15. data/examples/houghcircle.rb +1 -1
  16. data/examples/{box.png → images/box.png} +0 -0
  17. data/examples/{box_in_scene.png → images/box_in_scene.png} +0 -0
  18. data/examples/{inpaint.png → images/inpaint.png} +0 -0
  19. data/examples/images/lena-256x256.jpg +0 -0
  20. data/examples/images/lena-eyes.jpg +0 -0
  21. data/examples/{lenna-rotated.jpg → images/lenna-rotated.jpg} +0 -0
  22. data/examples/{lenna.jpg → images/lenna.jpg} +0 -0
  23. data/examples/{stuff.jpg → images/stuff.jpg} +0 -0
  24. data/examples/{tiffany.jpg → images/tiffany.jpg} +0 -0
  25. data/examples/inpaint.rb +1 -1
  26. data/examples/match_kdtree.rb +2 -2
  27. data/examples/match_template.rb +26 -0
  28. data/examples/{matching_to_many_images.rb → matching_to_many_images/matching_to_many_images.rb} +3 -3
  29. data/examples/matching_to_many_images/query.png +0 -0
  30. data/examples/matching_to_many_images/train/1.png +0 -0
  31. data/examples/matching_to_many_images/train/2.png +0 -0
  32. data/examples/matching_to_many_images/train/3.png +0 -0
  33. data/examples/matching_to_many_images/train/trainImages.txt +0 -0
  34. data/examples/paint.rb +0 -0
  35. data/examples/snake.rb +0 -0
  36. data/ext/opencv/algorithm.cpp +286 -0
  37. data/ext/opencv/algorithm.h +38 -0
  38. data/ext/opencv/cvmat.cpp +205 -76
  39. data/ext/opencv/cvmat.h +8 -1
  40. data/ext/opencv/eigenfaces.cpp +67 -0
  41. data/ext/opencv/eigenfaces.h +30 -0
  42. data/ext/opencv/extconf.rb +0 -0
  43. data/ext/opencv/facerecognizer.cpp +174 -0
  44. data/ext/opencv/facerecognizer.h +46 -0
  45. data/ext/opencv/fisherfaces.cpp +67 -0
  46. data/ext/opencv/fisherfaces.h +30 -0
  47. data/ext/opencv/lbph.cpp +70 -0
  48. data/ext/opencv/lbph.h +30 -0
  49. data/ext/opencv/opencv.cpp +51 -1
  50. data/ext/opencv/opencv.h +6 -0
  51. data/lib/opencv.rb +0 -0
  52. data/lib/opencv/version.rb +1 -1
  53. data/ruby-opencv.gemspec +8 -7
  54. data/test/eigenfaces_save.xml +7524 -0
  55. data/test/fisherfaces_save.xml +7530 -0
  56. data/test/helper.rb +0 -0
  57. data/test/lbph_save.xml +4304 -0
  58. data/test/runner.rb +0 -0
  59. data/test/test_curve.rb +0 -0
  60. data/test/test_cvavgcomp.rb +0 -0
  61. data/test/test_cvbox2d.rb +0 -0
  62. data/test/test_cvcapture.rb +0 -0
  63. data/test/test_cvchain.rb +0 -0
  64. data/test/test_cvcircle32f.rb +0 -0
  65. data/test/test_cvconnectedcomp.rb +0 -0
  66. data/test/test_cvcontour.rb +0 -0
  67. data/test/test_cvcontourtree.rb +0 -0
  68. data/test/test_cverror.rb +0 -0
  69. data/test/test_cvfeaturetree.rb +0 -0
  70. data/test/test_cvfont.rb +0 -0
  71. data/test/test_cvhaarclassifiercascade.rb +0 -0
  72. data/test/test_cvhistogram.rb +0 -0
  73. data/test/test_cvhumoments.rb +0 -0
  74. data/test/test_cvline.rb +0 -0
  75. data/test/test_cvmat.rb +72 -16
  76. data/test/test_cvmat_drawing.rb +0 -0
  77. data/test/test_cvmat_dxt.rb +0 -0
  78. data/test/test_cvmat_imageprocessing.rb +72 -2
  79. data/test/test_cvmat_matching.rb +1 -1
  80. data/test/test_cvmoments.rb +0 -0
  81. data/test/test_cvpoint.rb +0 -0
  82. data/test/test_cvpoint2d32f.rb +0 -0
  83. data/test/test_cvpoint3d32f.rb +0 -0
  84. data/test/test_cvrect.rb +0 -0
  85. data/test/test_cvscalar.rb +0 -0
  86. data/test/test_cvseq.rb +0 -0
  87. data/test/test_cvsize.rb +0 -0
  88. data/test/test_cvsize2d32f.rb +0 -0
  89. data/test/test_cvslice.rb +0 -0
  90. data/test/test_cvsurfparams.rb +0 -0
  91. data/test/test_cvsurfpoint.rb +0 -0
  92. data/test/test_cvtermcriteria.rb +0 -0
  93. data/test/test_cvtwopoints.rb +0 -0
  94. data/test/test_cvvideowriter.rb +0 -0
  95. data/test/test_eigenfaces.rb +93 -0
  96. data/test/test_fisherfaces.rb +93 -0
  97. data/test/test_iplconvkernel.rb +0 -0
  98. data/test/test_iplimage.rb +0 -4
  99. data/test/test_lbph.rb +152 -0
  100. data/test/test_mouseevent.rb +0 -0
  101. data/test/test_opencv.rb +33 -4
  102. data/test/test_pointset.rb +7 -5
  103. data/test/test_preliminary.rb +0 -0
  104. data/test/test_trackbar.rb +0 -0
  105. data/test/test_window.rb +0 -0
  106. metadata +84 -56
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8 -*-
3
+
4
+ # LBPH sample in ruby-opencv, equivalent to http://docs.opencv.org/trunk/_downloads/facerec_lbph.cpp
5
+ # See http://docs.opencv.org/trunk/modules/contrib/doc/facerec/facerec_tutorial.html
6
+ require 'opencv'
7
+ include OpenCV
8
+
9
+ def read_csv(filename, sepalator = ';')
10
+ images = []
11
+ labels = []
12
+ open(filename, 'r') { |f|
13
+ f.each { |line|
14
+ path, label = line.chomp.split(sepalator)
15
+ images << CvMat.load(path, CV_LOAD_IMAGE_GRAYSCALE)
16
+ labels << label.to_i
17
+ }
18
+ }
19
+
20
+ [images, labels]
21
+ end
22
+
23
+ # Check for valid command line arguments, print usage
24
+ # if no arguments were given.
25
+ if ARGV.size < 1
26
+ puts "usage: ruby #{__FILE__} <csv.ext>"
27
+ exit 1
28
+ end
29
+
30
+ # Get the path to your CSV.
31
+ fn_csv = ARGV.shift
32
+
33
+ # Read in the data. This can fail if no valid
34
+ # input filename is given.
35
+ images, labels = read_csv(fn_csv);
36
+
37
+ # Quit if there are not enough images for this demo.
38
+ raise 'This demo needs at least 2 images to work. Please add more images to your data set!' if images.size <= 1
39
+
40
+ # Get the height from the first image. We'll need this
41
+ # later in code to reshape the images to their original size:
42
+ height = images[0].rows;
43
+
44
+ # The following lines simply get the last images from
45
+ # your dataset and remove it from the vector. This is
46
+ # done, so that the training data (which we learn the
47
+ # cv::FaceRecognizer on) and the test data we test
48
+ # the model with, do not overlap.
49
+ test_sample = images.pop
50
+ test_label = labels.pop
51
+
52
+ # The following lines create an LBPH model for
53
+ # face recognition and train it with the images and
54
+ # labels read from the given CSV file.
55
+ #
56
+ # The LBPHFaceRecognizer uses Extended Local Binary Patterns
57
+ # (it's probably configurable with other operators at a later
58
+ # point), and has the following default values
59
+ #
60
+ # radius = 1
61
+ # neighbors = 8
62
+ # grid_x = 8
63
+ # grid_y = 8
64
+ #
65
+ # So if you want a LBPH FaceRecognizer using a radius of
66
+ # 2 and 16 neighbors, call the factory method with:
67
+ #
68
+ # LBPH.new(2, 16);
69
+ #
70
+ # And if you want a threshold (e.g. 123.0) call it with its default values:
71
+ #
72
+ # LBPH.new(1,8,8,8,123.0)
73
+ #
74
+ model = LBPH.new
75
+ model.train(images, labels)
76
+
77
+ # The following line predicts the label of a given test image:
78
+ predicted_label, predicted_confidence = model.predict(test_sample)
79
+
80
+ # To get the confidence of a prediction call the model with:
81
+ #
82
+ # predicted_label = -1;
83
+ # confidence = 0.0;
84
+ # model.predict(test_sample, predicted_label, confidence)
85
+ #
86
+ puts "Predicted class: #{predicted_label} / Actual class: #{test_label}"
87
+
88
+ # Sometimes you'll need to get/set internal model data,
89
+ # which isn't exposed by the public FaceRecognizer.
90
+ # Since each FaceRecognizer is derived from a Algorithm,
91
+ # you can query the data.
92
+ #
93
+ # First we'll use it to set the threshold of the FaceRecognizer
94
+ # to 0.0 without retraining the model. This can be useful if
95
+ # you are evaluating the model:
96
+ model.set_double('threshold', 0.0);
97
+
98
+ # Now the threshold of this model is set to 0.0. A prediction
99
+ # now returns -1, as it's impossible to have a distance below it
100
+ predicted_label = model.predict(test_sample)
101
+ puts "Predicted class = #{predicted_label}"
102
+
103
+ # Show some informations about the model, as there's no cool
104
+ # Model data to display as in Eigenfaces/Fisherfaces.
105
+ # Due to efficiency reasons the LBP images are not stored
106
+ # within the model:
107
+ puts 'Model Information:'
108
+ model_info = "\tLBPH(radius=#{model.get_int('radius')}, neighbors=#{model.get_int('neighbors')}, grid_x=#{model.get_int('grid_x')}, grid_y=#{model.get_int('grid_y')}, threshold=#{model.get_double('threshold')})"
109
+ puts model_info
110
+
111
+ # We could get the histograms for example:
112
+ histgrams = model.get_matvector('histograms');
113
+
114
+ # But should I really visualize it? Probably the length is interesting:
115
+ puts "Size of the histograms: #{histgrams[0].dims.reduce(&:*)}"
116
+
@@ -0,0 +1,111 @@
1
+ # Face recognition with ruby-opencv
2
+
3
+ This is a face recognition sample with ruby-opencv, which equivalent to the following OpenCV's tutorial.
4
+
5
+ [Face Recognition with OpenCV](http://docs.opencv.org/trunk/modules/contrib/doc/facerec/facerec_tutorial.html)
6
+
7
+
8
+ ## Running samples
9
+
10
+ ### 1. Get AT&T Facedatabase
11
+
12
+ Get AT&T Facedatabase from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html and unzip it.
13
+
14
+ ```sh
15
+ $ wget http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip
16
+ $ unzip att_faces.zip
17
+ ```
18
+
19
+ ### 2. Prepare the data
20
+
21
+ Create a CSV file to run samples.
22
+
23
+ ```sh
24
+ $ ruby create_csv.rb att_faces > at.txt
25
+ ```
26
+
27
+ You will get a CSV file which contains lines composed of a filename followed by a ; followed by the label (as integer number).
28
+
29
+ ```sh
30
+ $ cat at.txt
31
+ att_faces/s34/2.pgm;0
32
+ att_faces/s34/3.pgm;0
33
+ att_faces/s34/8.pgm;0
34
+ att_faces/s34/4.pgm;0
35
+ att_faces/s34/5.pgm;0
36
+ att_faces/s34/10.pgm;0
37
+ att_faces/s34/9.pgm;0
38
+ att_faces/s34/7.pgm;0
39
+ att_faces/s34/6.pgm;0
40
+ att_faces/s34/1.pgm;0
41
+ ...
42
+ ```
43
+
44
+ ### 3. Run sample codes
45
+
46
+ #### Eigenfaces
47
+
48
+ ```sh
49
+ $ mkdir output-eigenfaces
50
+ $ ruby facerec_eigenfaces.rb at.txt output-eigenfaces
51
+ ```
52
+
53
+ You will get the predicted class, actual class and eignvalues shown in console.
54
+
55
+ ```sh
56
+ Predicted class: 39 / Actual class: 39
57
+ Eigenvalue #0 = 2823424.500638128
58
+ Eigenvalue #1 = 2062015.3818895558
59
+ Eigenvalue #2 = 1090171.0771557507
60
+ Eigenvalue #3 = 892019.3644237233
61
+ Eigenvalue #4 = 818537.7917991373
62
+ Eigenvalue #5 = 539058.2364753223
63
+ Eigenvalue #6 = 390359.3231975121
64
+ Eigenvalue #7 = 373809.5486713626
65
+ Eigenvalue #8 = 314658.94374918053
66
+ Eigenvalue #9 = 288764.63018440653
67
+ ```
68
+
69
+ The result images will be stored in **output-eigenfaces** .
70
+
71
+
72
+ #### Fisherfaces
73
+
74
+ ```sh
75
+ $ mkdir output-fisherfaces
76
+ $ ruby facerec_fisherfaces.rb at.txt output-fisherfaces
77
+ ```
78
+
79
+ You will get the predicted class, actual class and eignvalues like Eigenfaces sample.
80
+
81
+ The result images will be stored in **output-fisherfaces** .
82
+
83
+
84
+ #### Local Binary Patterns Histograms
85
+
86
+ ```sh
87
+ $ ruby facerec_lbph.rb at.txt
88
+ ```
89
+
90
+ You will get the predicted class, actual class, model information and size of the histgrams.
91
+
92
+ ```
93
+ Predicted class: 39 / Actual class: 39
94
+ Predicted class = -1
95
+ Model Information:
96
+ LBPH(radius=1, neighbors=8, grid_x=8, grid_y=8, threshold=0.0)
97
+ Size of the histograms: 16384
98
+ ```
99
+
100
+ ## Credits
101
+
102
+ ### The Database of Faces
103
+
104
+ The Database of Faces, formerly The ORL Database of Faces, contains a set of face images taken between April 1992 and April 1994. The database was used in the context of a face recognition project carried out in collaboration with the Speech, Vision and Robotics Group of the Cambridge University Engineering Department.
105
+
106
+ There are ten different images of each of 40 distinct subjects. For some subjects, the images were taken at different times, varying the lighting, facial expressions (open / closed eyes, smiling / not smiling) and facial details (glasses / no glasses). All the images were taken against a dark homogeneous background with the subjects in an upright, frontal position (with tolerance for some side movement).
107
+
108
+ The files are in PGM format. The size of each image is 92x112 pixels, with 256 grey levels per pixel. The images are organised in 40 directories (one for each subject), which have names of the form sX, where X indicates the subject number (between 1 and 40). In each of these directories, there are ten different images of that subject, which have names of the form Y.pgm, where Y is the image number for that subject (between 1 and 10).
109
+
110
+ A copy of the database can be retrieved from: http://www.cl.cam.ac.uk/research/dtg/attarchive/pub/data/att_faces.zip.
111
+
data/examples/find_obj.rb CHANGED
@@ -100,8 +100,8 @@ puts 'Usage:'
100
100
  puts "ruby #{__FILE__} <object_filename> <scene_filename>, default is box.png and box_in_scene.png"
101
101
  puts
102
102
 
103
- object_filename = (ARGV.size == 2) ? ARGV[0] : 'box.png'
104
- scene_filename = (ARGV.size == 2) ? ARGV[1] : 'box_in_scene.png'
103
+ object_filename = (ARGV.size == 2) ? ARGV[0] : 'images/box.png'
104
+ scene_filename = (ARGV.size == 2) ? ARGV[1] : 'images/box_in_scene.png'
105
105
 
106
106
  object, image = nil, nil
107
107
  begin
@@ -7,7 +7,7 @@ include OpenCV
7
7
  original_window = GUI::Window.new "original"
8
8
  hough_window = GUI::Window.new "hough circles"
9
9
 
10
- image = IplImage::load "stuff.jpg"
10
+ image = IplImage::load "images/stuff.jpg"
11
11
  gray = image.BGR2GRAY
12
12
 
13
13
  result = image.clone
File without changes
File without changes
Binary file
Binary file
File without changes
File without changes
File without changes
data/examples/inpaint.rb CHANGED
@@ -20,7 +20,7 @@ owindow = GUI::Window.new('original')
20
20
  mwindow = GUI::Window.new('mask')
21
21
  iwindow = GUI::Window.new('inpaint')
22
22
 
23
- image = IplImage::load('inpaint.png')
23
+ image = IplImage::load('images/inpaint.png')
24
24
  noimage = image.zero
25
25
  b, g, r = image.split
26
26
  original_mask = r.threshold(0x00, 0xFF, CV_THRESH_BINARY_INV) & b.threshold(0x00, 0xFF, CV_THRESH_BINARY_INV)
@@ -11,8 +11,8 @@ USE_EXTENDED_DESCRIPTOR = true
11
11
  THRESHOLD = 1500
12
12
  DESCRIPTOR_SIZE = USE_EXTENDED_DESCRIPTOR ? 128 : 64
13
13
 
14
- img1 = CvMat.load('lenna.jpg', CV_LOAD_IMAGE_GRAYSCALE)
15
- img2 = CvMat.load('lenna-rotated.jpg', CV_LOAD_IMAGE_GRAYSCALE)
14
+ img1 = CvMat.load('images/lenna.jpg', CV_LOAD_IMAGE_GRAYSCALE)
15
+ img2 = CvMat.load('images/lenna-rotated.jpg', CV_LOAD_IMAGE_GRAYSCALE)
16
16
 
17
17
  puts 'Extracting features from img1 using SURF...'
18
18
  param = CvSURFParams.new(THRESHOLD, USE_EXTENDED_DESCRIPTOR)
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # A demo of Ruby/OpenCV's match_template function
4
+
5
+ require 'opencv'
6
+ include OpenCV
7
+
8
+ puts 'This program demonstrates the match_template function'
9
+ puts 'Usage:'
10
+ puts "ruby #{__FILE__} <template_filename> <match_filename>"
11
+ puts
12
+
13
+ template_filename = (ARGV.size == 2) ? ARGV[0] : File.expand_path(File.dirname(__FILE__) + '/images/lena-eyes.jpg')
14
+ match_image_filename = (ARGV.size == 2) ? ARGV[1] : File.expand_path(File.dirname(__FILE__) + '/images/lena-256x256.jpg')
15
+
16
+ template = CvMat.load(template_filename)
17
+ match_image = CvMat.load(match_image_filename)
18
+ result = match_image.match_template(template, :sqdiff_normed)
19
+
20
+ pt1 = result.min_max_loc[2] # minimum location
21
+ pt2 = CvPoint.new(pt1.x + template.width, pt1.y + template.height)
22
+ match_image.rectangle!(pt1, pt2, :color => CvColor::Black, :thickness => 3)
23
+
24
+ window = GUI::Window.new('Display window') # Create a window for display.
25
+ window.show(match_image) # Show our image inside it.
26
+ GUI::wait_key # Wait for a keystroke in the window.
@@ -2,11 +2,11 @@ require 'opencv'
2
2
  require 'benchmark'
3
3
  include OpenCV
4
4
 
5
- data = File.join(File.dirname(__FILE__), 'matching_to_many_images')
5
+ data = File.dirname(__FILE__)
6
6
 
7
- query = IplImage.load File.join(data, 'query.png'), CV_LOAD_IMAGE_GRAYSCALE
7
+ query = CvMat.load File.join(data, 'query.png'), CV_LOAD_IMAGE_GRAYSCALE
8
8
  image_files = ['1.png', '2.png', '3.png'].map{|f| File.join(data, 'train', f)}
9
- images = image_files.map{|f| IplImage.load f, CV_LOAD_IMAGE_GRAYSCALE}
9
+ images = image_files.map{|f| CvMat.load f, CV_LOAD_IMAGE_GRAYSCALE}
10
10
 
11
11
 
12
12
  matchs = query.match_descriptors(images)
File without changes
File without changes
File without changes
File without changes
data/examples/paint.rb CHANGED
File without changes
data/examples/snake.rb CHANGED
File without changes
@@ -0,0 +1,286 @@
1
+ /************************************************************
2
+
3
+ algorithm.cpp -
4
+
5
+ $Author: ser1zw $
6
+
7
+ Copyright (C) 2013 ser1zw
8
+
9
+ ************************************************************/
10
+ #include <stdio.h>
11
+ #include "algorithm.h"
12
+ /*
13
+ * Document-class: OpenCV::Algorithm
14
+ *
15
+ */
16
+ __NAMESPACE_BEGIN_OPENCV
17
+ __NAMESPACE_BEGIN_ALGORITHM
18
+
19
+ VALUE rb_klass;
20
+
21
+ VALUE
22
+ rb_class()
23
+ {
24
+ return rb_klass;
25
+ }
26
+
27
+ VALUE
28
+ rb_set_int(VALUE self, VALUE parameter, VALUE value)
29
+ {
30
+ Check_Type(parameter, T_STRING);
31
+ try {
32
+ ALGORITHM(self)->setInt(StringValueCStr(parameter), NUM2INT(value));
33
+ }
34
+ catch (cv::Exception& e) {
35
+ raise_cverror(e);
36
+ }
37
+
38
+ return Qnil;
39
+ }
40
+
41
+ VALUE
42
+ rb_set_double(VALUE self, VALUE parameter, VALUE value)
43
+ {
44
+ Check_Type(parameter, T_STRING);
45
+ try {
46
+ ALGORITHM(self)->setDouble(StringValueCStr(parameter), NUM2DBL(value));
47
+ }
48
+ catch (cv::Exception& e) {
49
+ raise_cverror(e);
50
+ }
51
+
52
+ return Qnil;
53
+ }
54
+
55
+ VALUE
56
+ rb_set_bool(VALUE self, VALUE parameter, VALUE value)
57
+ {
58
+ Check_Type(parameter, T_STRING);
59
+ try {
60
+ bool val = TRUE_OR_FALSE(value) ? true : false;
61
+ ALGORITHM(self)->setBool(StringValueCStr(parameter), val);
62
+ }
63
+ catch (cv::Exception& e) {
64
+ raise_cverror(e);
65
+ }
66
+
67
+ return Qnil;
68
+ }
69
+
70
+ VALUE
71
+ rb_set_string(VALUE self, VALUE parameter, VALUE value)
72
+ {
73
+ Check_Type(parameter, T_STRING);
74
+ Check_Type(value, T_STRING);
75
+ try {
76
+ ALGORITHM(self)->setString(StringValueCStr(parameter), StringValueCStr(value));
77
+ }
78
+ catch (cv::Exception& e) {
79
+ raise_cverror(e);
80
+ }
81
+
82
+ return Qnil;
83
+ }
84
+
85
+ VALUE
86
+ rb_set_mat(VALUE self, VALUE parameter, VALUE value)
87
+ {
88
+ Check_Type(parameter, T_STRING);
89
+ try {
90
+ CvMat* val = CVMAT_WITH_CHECK(value);
91
+ cv::Mat mat(val);
92
+ ALGORITHM(self)->setMat(StringValueCStr(parameter), mat);
93
+ }
94
+ catch (cv::Exception& e) {
95
+ raise_cverror(e);
96
+ }
97
+
98
+ return Qnil;
99
+ }
100
+
101
+ VALUE
102
+ rb_set_matvector(VALUE self, VALUE parameter, VALUE value)
103
+ {
104
+ Check_Type(parameter, T_STRING);
105
+ Check_Type(value, T_ARRAY);
106
+ try {
107
+ long len = RARRAY_LEN(value);
108
+ VALUE* value_ptr = RARRAY_PTR(value);
109
+ std::vector<cv::Mat> mat_vector;
110
+ for (int i = 0; i < len; i++) {
111
+ CvMat* val = CVMAT_WITH_CHECK(value_ptr[i]);
112
+ cv::Mat mat(val);
113
+ mat_vector.push_back(mat);
114
+ }
115
+ ALGORITHM(self)->setMatVector(StringValueCStr(parameter), mat_vector);
116
+ }
117
+ catch (cv::Exception& e) {
118
+ raise_cverror(e);
119
+ }
120
+
121
+ return Qnil;
122
+ }
123
+
124
+ VALUE
125
+ rb_set_algorithm(VALUE self, VALUE parameter, VALUE value)
126
+ {
127
+ Check_Type(parameter, T_STRING);
128
+ try {
129
+ ALGORITHM(self)->setAlgorithm(StringValueCStr(parameter), ALGORITHM(value));
130
+ }
131
+ catch (cv::Exception& e) {
132
+ raise_cverror(e);
133
+ }
134
+
135
+ return Qnil;
136
+ }
137
+
138
+
139
+ VALUE
140
+ rb_get_int(VALUE self, VALUE parameter)
141
+ {
142
+ Check_Type(parameter, T_STRING);
143
+ int value = 0;
144
+ try {
145
+ value = ALGORITHM(self)->getInt(StringValueCStr(parameter));
146
+ }
147
+ catch (cv::Exception& e) {
148
+ raise_cverror(e);
149
+ }
150
+
151
+ return INT2NUM(value);
152
+ }
153
+
154
+ VALUE
155
+ rb_get_double(VALUE self, VALUE parameter)
156
+ {
157
+ Check_Type(parameter, T_STRING);
158
+ double value = 0.0;
159
+ try {
160
+ value = ALGORITHM(self)->getDouble(StringValueCStr(parameter));
161
+ }
162
+ catch (cv::Exception& e) {
163
+ raise_cverror(e);
164
+ }
165
+
166
+ return DBL2NUM(value);
167
+ }
168
+
169
+ VALUE
170
+ rb_get_bool(VALUE self, VALUE parameter)
171
+ {
172
+ Check_Type(parameter, T_STRING);
173
+ bool value = false;
174
+ try {
175
+ value = ALGORITHM(self)->getBool(StringValueCStr(parameter));
176
+ }
177
+ catch (cv::Exception& e) {
178
+ raise_cverror(e);
179
+ }
180
+
181
+ return value ? Qtrue : Qfalse;
182
+ }
183
+
184
+ VALUE
185
+ rb_get_string(VALUE self, VALUE parameter)
186
+ {
187
+ Check_Type(parameter, T_STRING);
188
+ std::string value = "";
189
+ try {
190
+ value = ALGORITHM(self)->getString(StringValueCStr(parameter));
191
+ }
192
+ catch (cv::Exception& e) {
193
+ raise_cverror(e);
194
+ }
195
+
196
+ return rb_str_new_cstr(value.c_str());
197
+ }
198
+
199
+ VALUE
200
+ rb_get_mat(VALUE self, VALUE parameter)
201
+ {
202
+ Check_Type(parameter, T_STRING);
203
+ VALUE mat = Qnil;
204
+ try {
205
+ cv::Mat value = ALGORITHM(self)->getMat(StringValueCStr(parameter));
206
+ cv::Size size = value.size();
207
+ mat = cCvMat::new_object(size.height, size.width, value.type());
208
+ cv::Mat dst(CVMAT(mat));
209
+ value.copyTo(dst);
210
+ }
211
+ catch (cv::Exception& e) {
212
+ raise_cverror(e);
213
+ }
214
+ return mat;
215
+ }
216
+
217
+ VALUE
218
+ rb_get_matvector(VALUE self, VALUE parameter)
219
+ {
220
+ Check_Type(parameter, T_STRING);
221
+ VALUE array = Qnil;
222
+ try {
223
+ std::vector<cv::Mat> value = ALGORITHM(self)->getMatVector(StringValueCStr(parameter));
224
+ int len = value.size();
225
+ array = rb_ary_new2(len);
226
+ for (int i = 0; i < len; i++) {
227
+ cv::Mat m = value[i];
228
+ cv::Size size = m.size();
229
+ VALUE mat = cCvMat::new_object(size.height, size.width, m.type());
230
+ cv::Mat dst(CVMAT(mat));
231
+ m.copyTo(dst);
232
+ rb_ary_store(array, i, mat);
233
+ }
234
+ }
235
+ catch (cv::Exception& e) {
236
+ raise_cverror(e);
237
+ }
238
+ return array;
239
+ }
240
+
241
+ VALUE
242
+ rb_name(VALUE self)
243
+ {
244
+ VALUE name = Qnil;
245
+ try {
246
+ name = rb_str_new_cstr(ALGORITHM(self)->name().c_str());
247
+ }
248
+ catch (cv::Exception& e) {
249
+ raise_cverror(e);
250
+ }
251
+ return name;
252
+ }
253
+
254
+ void
255
+ define_ruby_class()
256
+ {
257
+ if (rb_klass)
258
+ return;
259
+ /*
260
+ * opencv = rb_define_module("OpenCV");
261
+ *
262
+ * note: this comment is used by rdoc.
263
+ */
264
+ VALUE opencv = rb_module_opencv();
265
+ rb_klass = rb_define_class_under(opencv, "Algorithm", rb_cObject);
266
+ rb_define_method(rb_klass, "set_int", RUBY_METHOD_FUNC(rb_set_int), 2);
267
+ rb_define_method(rb_klass, "set_double", RUBY_METHOD_FUNC(rb_set_double), 2);
268
+ rb_define_method(rb_klass, "set_bool", RUBY_METHOD_FUNC(rb_set_bool), 2);
269
+ rb_define_method(rb_klass, "set_string", RUBY_METHOD_FUNC(rb_set_string), 2);
270
+ rb_define_method(rb_klass, "set_mat", RUBY_METHOD_FUNC(rb_set_mat), 2);
271
+ rb_define_method(rb_klass, "set_matvector", RUBY_METHOD_FUNC(rb_set_matvector), 2);
272
+ rb_define_method(rb_klass, "set_algorithm", RUBY_METHOD_FUNC(rb_set_algorithm), 2);
273
+
274
+ rb_define_method(rb_klass, "get_int", RUBY_METHOD_FUNC(rb_get_int), 1);
275
+ rb_define_method(rb_klass, "get_double", RUBY_METHOD_FUNC(rb_get_double), 1);
276
+ rb_define_method(rb_klass, "get_bool", RUBY_METHOD_FUNC(rb_get_bool), 1);
277
+ rb_define_method(rb_klass, "get_string", RUBY_METHOD_FUNC(rb_get_string), 1);
278
+ rb_define_method(rb_klass, "get_mat", RUBY_METHOD_FUNC(rb_get_mat), 1);
279
+ rb_define_method(rb_klass, "get_matvector", RUBY_METHOD_FUNC(rb_get_matvector), 1);
280
+
281
+ rb_define_method(rb_klass, "name", RUBY_METHOD_FUNC(rb_name), 0);
282
+ }
283
+
284
+ __NAMESPACE_END_ALGORITM
285
+ __NAMESPACE_END_OPENCV
286
+