ruby-opencv 0.0.10-i386-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +26 -0
- data/DEVELOPERS_NOTE.md +137 -0
- data/Gemfile +8 -0
- data/History.txt +5 -0
- data/License.txt +30 -0
- data/Manifest.txt +227 -0
- data/README.md +98 -0
- data/Rakefile +90 -0
- data/config.yml +7 -0
- data/examples/alpha_blend.rb +21 -0
- data/examples/box.png +0 -0
- data/examples/box_in_scene.png +0 -0
- data/examples/contours/bitmap-contours-with-labels.png +0 -0
- data/examples/contours/bitmap-contours.png +0 -0
- data/examples/contours/bounding-box-detect-canny.rb +62 -0
- data/examples/contours/contour_retrieval_modes.rb +139 -0
- data/examples/contours/rotated-boxes.jpg +0 -0
- data/examples/convexhull.rb +47 -0
- data/examples/face_detect.rb +20 -0
- data/examples/find_obj.rb +169 -0
- data/examples/houghcircle.rb +22 -0
- data/examples/inpaint.png +0 -0
- data/examples/inpaint.rb +57 -0
- data/examples/lenna-rotated.jpg +0 -0
- data/examples/lenna.jpg +0 -0
- data/examples/match_kdtree.rb +88 -0
- data/examples/matching_to_many_images.rb +16 -0
- data/examples/matching_to_many_images/query.png +0 -0
- data/examples/matching_to_many_images/train/1.png +0 -0
- data/examples/matching_to_many_images/train/2.png +0 -0
- data/examples/matching_to_many_images/train/3.png +0 -0
- data/examples/matching_to_many_images/train/trainImages.txt +3 -0
- data/examples/paint.rb +70 -0
- data/examples/snake.rb +43 -0
- data/examples/stuff.jpg +0 -0
- data/examples/tiffany.jpg +0 -0
- data/ext/opencv/curve.cpp +112 -0
- data/ext/opencv/curve.h +34 -0
- data/ext/opencv/cvavgcomp.cpp +67 -0
- data/ext/opencv/cvavgcomp.h +39 -0
- data/ext/opencv/cvbox2d.cpp +197 -0
- data/ext/opencv/cvbox2d.h +61 -0
- data/ext/opencv/cvcapture.cpp +506 -0
- data/ext/opencv/cvcapture.h +72 -0
- data/ext/opencv/cvchain.cpp +233 -0
- data/ext/opencv/cvchain.h +46 -0
- data/ext/opencv/cvcircle32f.cpp +116 -0
- data/ext/opencv/cvcircle32f.h +52 -0
- data/ext/opencv/cvcondensation.cpp +282 -0
- data/ext/opencv/cvcondensation.h +49 -0
- data/ext/opencv/cvconnectedcomp.cpp +143 -0
- data/ext/opencv/cvconnectedcomp.h +49 -0
- data/ext/opencv/cvcontour.cpp +296 -0
- data/ext/opencv/cvcontour.h +48 -0
- data/ext/opencv/cvcontourtree.cpp +91 -0
- data/ext/opencv/cvcontourtree.h +41 -0
- data/ext/opencv/cvconvexitydefect.cpp +103 -0
- data/ext/opencv/cvconvexitydefect.h +42 -0
- data/ext/opencv/cverror.cpp +159 -0
- data/ext/opencv/cverror.h +28 -0
- data/ext/opencv/cvfeaturetree.cpp +125 -0
- data/ext/opencv/cvfeaturetree.h +55 -0
- data/ext/opencv/cvfont.cpp +208 -0
- data/ext/opencv/cvfont.h +64 -0
- data/ext/opencv/cvhaarclassifiercascade.cpp +168 -0
- data/ext/opencv/cvhaarclassifiercascade.h +39 -0
- data/ext/opencv/cvhistogram.cpp +546 -0
- data/ext/opencv/cvhistogram.h +73 -0
- data/ext/opencv/cvhumoments.cpp +139 -0
- data/ext/opencv/cvhumoments.h +51 -0
- data/ext/opencv/cvline.cpp +154 -0
- data/ext/opencv/cvline.h +54 -0
- data/ext/opencv/cvmat.cpp +5848 -0
- data/ext/opencv/cvmat.h +284 -0
- data/ext/opencv/cvmatnd.cpp +44 -0
- data/ext/opencv/cvmatnd.h +28 -0
- data/ext/opencv/cvmemstorage.cpp +68 -0
- data/ext/opencv/cvmemstorage.h +53 -0
- data/ext/opencv/cvmoments.cpp +287 -0
- data/ext/opencv/cvmoments.h +75 -0
- data/ext/opencv/cvpoint.cpp +228 -0
- data/ext/opencv/cvpoint.h +64 -0
- data/ext/opencv/cvpoint2d32f.cpp +211 -0
- data/ext/opencv/cvpoint2d32f.h +63 -0
- data/ext/opencv/cvpoint3d32f.cpp +245 -0
- data/ext/opencv/cvpoint3d32f.h +66 -0
- data/ext/opencv/cvrect.cpp +333 -0
- data/ext/opencv/cvrect.h +79 -0
- data/ext/opencv/cvscalar.cpp +236 -0
- data/ext/opencv/cvscalar.h +71 -0
- data/ext/opencv/cvseq.cpp +599 -0
- data/ext/opencv/cvseq.h +74 -0
- data/ext/opencv/cvsize.cpp +221 -0
- data/ext/opencv/cvsize.h +65 -0
- data/ext/opencv/cvsize2d32f.cpp +209 -0
- data/ext/opencv/cvsize2d32f.h +64 -0
- data/ext/opencv/cvslice.cpp +120 -0
- data/ext/opencv/cvslice.h +61 -0
- data/ext/opencv/cvsparsemat.cpp +44 -0
- data/ext/opencv/cvsparsemat.h +28 -0
- data/ext/opencv/cvsurfparams.cpp +199 -0
- data/ext/opencv/cvsurfparams.h +58 -0
- data/ext/opencv/cvsurfpoint.cpp +223 -0
- data/ext/opencv/cvsurfpoint.h +52 -0
- data/ext/opencv/cvtermcriteria.cpp +192 -0
- data/ext/opencv/cvtermcriteria.h +71 -0
- data/ext/opencv/cvtwopoints.cpp +116 -0
- data/ext/opencv/cvtwopoints.h +51 -0
- data/ext/opencv/cvutils.cpp +192 -0
- data/ext/opencv/cvutils.h +30 -0
- data/ext/opencv/cvvideowriter.cpp +137 -0
- data/ext/opencv/cvvideowriter.h +43 -0
- data/ext/opencv/extconf.rb +83 -0
- data/ext/opencv/gui.cpp +68 -0
- data/ext/opencv/gui.h +30 -0
- data/ext/opencv/iplconvkernel.cpp +192 -0
- data/ext/opencv/iplconvkernel.h +71 -0
- data/ext/opencv/iplimage.cpp +644 -0
- data/ext/opencv/iplimage.h +73 -0
- data/ext/opencv/mouseevent.cpp +181 -0
- data/ext/opencv/mouseevent.h +56 -0
- data/ext/opencv/opencv.cpp +727 -0
- data/ext/opencv/opencv.h +400 -0
- data/ext/opencv/pointset.cpp +274 -0
- data/ext/opencv/pointset.h +68 -0
- data/ext/opencv/trackbar.cpp +121 -0
- data/ext/opencv/trackbar.h +69 -0
- data/ext/opencv/window.cpp +357 -0
- data/ext/opencv/window.h +66 -0
- data/images/CvMat_sobel.png +0 -0
- data/images/CvMat_sub_rect.png +0 -0
- data/images/CvSeq_relationmap.png +0 -0
- data/images/face_detect_from_lena.jpg +0 -0
- data/lib/1.9/opencv.so +0 -0
- data/lib/2.0/opencv.so +0 -0
- data/lib/opencv.rb +12 -0
- data/lib/opencv/psyched_yaml.rb +22 -0
- data/lib/opencv/version.rb +3 -0
- data/ruby-opencv.gemspec +44 -0
- data/test/helper.rb +166 -0
- data/test/runner.rb +30 -0
- data/test/samples/airplane.jpg +0 -0
- data/test/samples/baboon.jpg +0 -0
- data/test/samples/baboon200.jpg +0 -0
- data/test/samples/baboon200_rotated.jpg +0 -0
- data/test/samples/blank0.jpg +0 -0
- data/test/samples/blank1.jpg +0 -0
- data/test/samples/blank2.jpg +0 -0
- data/test/samples/blank3.jpg +0 -0
- data/test/samples/blank4.jpg +0 -0
- data/test/samples/blank5.jpg +0 -0
- data/test/samples/blank6.jpg +0 -0
- data/test/samples/blank7.jpg +0 -0
- data/test/samples/blank8.jpg +0 -0
- data/test/samples/blank9.jpg +0 -0
- data/test/samples/cat.jpg +0 -0
- data/test/samples/chessboard.jpg +0 -0
- data/test/samples/contours.jpg +0 -0
- data/test/samples/fruits.jpg +0 -0
- data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
- data/test/samples/inpaint-mask.bmp +0 -0
- data/test/samples/lena-256x256.jpg +0 -0
- data/test/samples/lena-32x32.jpg +0 -0
- data/test/samples/lena-eyes.jpg +0 -0
- data/test/samples/lena-inpaint.jpg +0 -0
- data/test/samples/lena.jpg +0 -0
- data/test/samples/lines.jpg +0 -0
- data/test/samples/messy0.jpg +0 -0
- data/test/samples/messy1.jpg +0 -0
- data/test/samples/movie_sample.avi +0 -0
- data/test/samples/one_way_train_0000.jpg +0 -0
- data/test/samples/one_way_train_0001.jpg +0 -0
- data/test/samples/partially_blank0.jpg +0 -0
- data/test/samples/partially_blank1.jpg +0 -0
- data/test/samples/smooth0.jpg +0 -0
- data/test/samples/smooth1.jpg +0 -0
- data/test/samples/smooth2.jpg +0 -0
- data/test/samples/smooth3.jpg +0 -0
- data/test/samples/smooth4.jpg +0 -0
- data/test/samples/smooth5.jpg +0 -0
- data/test/samples/smooth6.jpg +0 -0
- data/test/samples/str-cv-rotated.jpg +0 -0
- data/test/samples/str-cv.jpg +0 -0
- data/test/samples/str-ov.jpg +0 -0
- data/test/samples/stuff.jpg +0 -0
- data/test/test_curve.rb +43 -0
- data/test/test_cvavgcomp.rb +24 -0
- data/test/test_cvbox2d.rb +76 -0
- data/test/test_cvcapture.rb +183 -0
- data/test/test_cvchain.rb +108 -0
- data/test/test_cvcircle32f.rb +41 -0
- data/test/test_cvconnectedcomp.rb +61 -0
- data/test/test_cvcontour.rb +150 -0
- data/test/test_cvcontourtree.rb +43 -0
- data/test/test_cverror.rb +50 -0
- data/test/test_cvfeaturetree.rb +65 -0
- data/test/test_cvfont.rb +58 -0
- data/test/test_cvhaarclassifiercascade.rb +63 -0
- data/test/test_cvhistogram.rb +271 -0
- data/test/test_cvhumoments.rb +83 -0
- data/test/test_cvline.rb +50 -0
- data/test/test_cvmat.rb +2947 -0
- data/test/test_cvmat_drawing.rb +349 -0
- data/test/test_cvmat_dxt.rb +150 -0
- data/test/test_cvmat_imageprocessing.rb +2015 -0
- data/test/test_cvmat_matching.rb +57 -0
- data/test/test_cvmoments.rb +180 -0
- data/test/test_cvpoint.rb +75 -0
- data/test/test_cvpoint2d32f.rb +75 -0
- data/test/test_cvpoint3d32f.rb +93 -0
- data/test/test_cvrect.rb +144 -0
- data/test/test_cvscalar.rb +113 -0
- data/test/test_cvseq.rb +295 -0
- data/test/test_cvsize.rb +75 -0
- data/test/test_cvsize2d32f.rb +75 -0
- data/test/test_cvslice.rb +31 -0
- data/test/test_cvsurfparams.rb +57 -0
- data/test/test_cvsurfpoint.rb +66 -0
- data/test/test_cvtermcriteria.rb +56 -0
- data/test/test_cvtwopoints.rb +40 -0
- data/test/test_cvvideowriter.rb +58 -0
- data/test/test_iplconvkernel.rb +54 -0
- data/test/test_iplimage.rb +236 -0
- data/test/test_mouseevent.rb +17 -0
- data/test/test_opencv.rb +329 -0
- data/test/test_pointset.rb +126 -0
- data/test/test_preliminary.rb +130 -0
- data/test/test_trackbar.rb +47 -0
- data/test/test_window.rb +115 -0
- metadata +386 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# face_detect.rb
|
3
|
+
require "rubygems"
|
4
|
+
require "opencv"
|
5
|
+
|
6
|
+
include OpenCV
|
7
|
+
|
8
|
+
window = GUI::Window.new("face detect")
|
9
|
+
capture = CvCapture.open
|
10
|
+
detector = CvHaarClassifierCascade::load("./data/haarcascades/haarcascade_frontalface_alt.xml")
|
11
|
+
|
12
|
+
loop {
|
13
|
+
image = capture.query
|
14
|
+
detector.detect_objects(image).each { |rect|
|
15
|
+
image.rectangle! rect.top_left, rect.bottom_right, :color => CvColor::Red
|
16
|
+
}
|
17
|
+
window.show image
|
18
|
+
break if GUI::wait_key(100)
|
19
|
+
}
|
20
|
+
|
@@ -0,0 +1,169 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
3
|
+
|
4
|
+
# A Demo Ruby/OpenCV Implementation of SURF
|
5
|
+
# See https://code.ros.org/trac/opencv/browser/tags/2.3.1/opencv/samples/c/find_obj.cpp
|
6
|
+
require 'opencv'
|
7
|
+
require 'benchmark'
|
8
|
+
include OpenCV
|
9
|
+
|
10
|
+
def compare_surf_descriptors(d1, d2, best, length)
|
11
|
+
raise ArgumentError unless (length % 4) == 0
|
12
|
+
total_cost = 0
|
13
|
+
0.step(length - 1, 4) { |i|
|
14
|
+
t0 = d1[i] - d2[i]
|
15
|
+
t1 = d1[i + 1] - d2[i + 1]
|
16
|
+
t2 = d1[i + 2] - d2[i + 2]
|
17
|
+
t3 = d1[i + 3] - d2[i + 3]
|
18
|
+
total_cost += t0 * t0 + t1 * t1 + t2 * t2 + t3 * t3
|
19
|
+
break if total_cost > best
|
20
|
+
}
|
21
|
+
total_cost
|
22
|
+
end
|
23
|
+
|
24
|
+
def naive_nearest_neighbor(vec, laplacian, model_keypoints, model_descriptors)
|
25
|
+
length = model_descriptors[0].size
|
26
|
+
neighbor = nil
|
27
|
+
dist1 = 1e6
|
28
|
+
dist2 = 1e6
|
29
|
+
|
30
|
+
model_descriptors.size.times { |i|
|
31
|
+
kp = model_keypoints[i]
|
32
|
+
mvec = model_descriptors[i]
|
33
|
+
next if laplacian != kp.laplacian
|
34
|
+
|
35
|
+
d = compare_surf_descriptors(vec, mvec, dist2, length)
|
36
|
+
if d < dist1
|
37
|
+
dist2 = dist1
|
38
|
+
dist1 = d
|
39
|
+
neighbor = i
|
40
|
+
elsif d < dist2
|
41
|
+
dist2 = d
|
42
|
+
end
|
43
|
+
}
|
44
|
+
|
45
|
+
return (dist1 < 0.6 * dist2) ? neighbor : nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def find_pairs(object_keypoints, object_descriptors,
|
49
|
+
image_keypoints, image_descriptors)
|
50
|
+
ptpairs = []
|
51
|
+
object_descriptors.size.times { |i|
|
52
|
+
kp = object_keypoints[i]
|
53
|
+
descriptor = object_descriptors[i]
|
54
|
+
nearest_neighbor = naive_nearest_neighbor(descriptor, kp.laplacian, image_keypoints, image_descriptors)
|
55
|
+
unless nearest_neighbor.nil?
|
56
|
+
ptpairs << i
|
57
|
+
ptpairs << nearest_neighbor
|
58
|
+
end
|
59
|
+
}
|
60
|
+
ptpairs
|
61
|
+
end
|
62
|
+
|
63
|
+
def locate_planar_object(object_keypoints, object_descriptors,
|
64
|
+
image_keypoints, image_descriptors, src_corners)
|
65
|
+
ptpairs = find_pairs(object_keypoints, object_descriptors, image_keypoints, image_descriptors)
|
66
|
+
n = ptpairs.size / 2
|
67
|
+
return nil if n < 4
|
68
|
+
|
69
|
+
pt1 = []
|
70
|
+
pt2 = []
|
71
|
+
n.times { |i|
|
72
|
+
pt1 << object_keypoints[ptpairs[i * 2]].pt
|
73
|
+
pt2 << image_keypoints[ptpairs[i * 2 + 1]].pt
|
74
|
+
}
|
75
|
+
|
76
|
+
_pt1 = CvMat.new(1, n, CV_32F, 2)
|
77
|
+
_pt2 = CvMat.new(1, n, CV_32F, 2)
|
78
|
+
_pt1.set_data(pt1)
|
79
|
+
_pt2.set_data(pt2)
|
80
|
+
h = CvMat.find_homography(_pt1, _pt2, :ransac, 5)
|
81
|
+
|
82
|
+
dst_corners = []
|
83
|
+
4.times { |i|
|
84
|
+
x = src_corners[i].x
|
85
|
+
y = src_corners[i].y
|
86
|
+
z = 1.0 / (h[6][0] * x + h[7][0] * y + h[8][0])
|
87
|
+
x = (h[0][0] * x + h[1][0] * y + h[2][0]) * z
|
88
|
+
y = (h[3][0] * x + h[4][0] * y + h[5][0]) * z
|
89
|
+
dst_corners << CvPoint.new(x.to_i, y.to_i)
|
90
|
+
}
|
91
|
+
|
92
|
+
dst_corners
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
##### Main #####
|
97
|
+
puts 'This program demonstrated the use of the SURF Detector and Descriptor using'
|
98
|
+
puts 'brute force matching on planar objects.'
|
99
|
+
puts 'Usage:'
|
100
|
+
puts "ruby #{__FILE__} <object_filename> <scene_filename>, default is box.png and box_in_scene.png"
|
101
|
+
puts
|
102
|
+
|
103
|
+
object_filename = (ARGV.size == 2) ? ARGV[0] : 'box.png'
|
104
|
+
scene_filename = (ARGV.size == 2) ? ARGV[1] : 'box_in_scene.png'
|
105
|
+
|
106
|
+
object, image = nil, nil
|
107
|
+
begin
|
108
|
+
object = IplImage.load(object_filename, CV_LOAD_IMAGE_GRAYSCALE)
|
109
|
+
image = IplImage.load(scene_filename, CV_LOAD_IMAGE_GRAYSCALE)
|
110
|
+
rescue
|
111
|
+
puts "Can not load #{object_filename} and/or #{scene_filename}"
|
112
|
+
puts "Usage: ruby #{__FILE__} [<object_filename> <scene_filename>]"
|
113
|
+
exit
|
114
|
+
end
|
115
|
+
object_color = object.GRAY2BGR
|
116
|
+
|
117
|
+
param = CvSURFParams.new(1500)
|
118
|
+
|
119
|
+
object_keypoints, object_descriptors = nil, nil
|
120
|
+
image_keypoints, image_descriptors = nil, nil
|
121
|
+
tms = Benchmark.measure {
|
122
|
+
object_keypoints, object_descriptors = object.extract_surf(param)
|
123
|
+
puts "Object Descriptors: #{object_descriptors.size}"
|
124
|
+
|
125
|
+
image_keypoints, image_descriptors = image.extract_surf(param)
|
126
|
+
puts "Image Descriptors: #{image_descriptors.size}"
|
127
|
+
}
|
128
|
+
puts "Extraction time = #{tms.real * 1000} ms"
|
129
|
+
|
130
|
+
correspond = IplImage.new(image.width, object.height + image.height, CV_8U, 1);
|
131
|
+
correspond.set_roi(CvRect.new(0, 0, object.width, object.height))
|
132
|
+
object.copy(correspond)
|
133
|
+
correspond.set_roi(CvRect.new(0, object.height, image.width, image.height))
|
134
|
+
image.copy(correspond)
|
135
|
+
correspond.reset_roi
|
136
|
+
|
137
|
+
src_corners = [CvPoint.new(0, 0), CvPoint.new(object.width, 0),
|
138
|
+
CvPoint.new(object.width, object.height), CvPoint.new(0, object.height)]
|
139
|
+
dst_corners = locate_planar_object(object_keypoints, object_descriptors,
|
140
|
+
image_keypoints, image_descriptors, src_corners)
|
141
|
+
|
142
|
+
correspond = correspond.GRAY2BGR
|
143
|
+
if dst_corners
|
144
|
+
4.times { |i|
|
145
|
+
r1 = dst_corners[i % 4]
|
146
|
+
r2 = dst_corners[(i + 1) % 4]
|
147
|
+
correspond.line!(CvPoint.new(r1.x, r1.y + object.height), CvPoint.new(r2.x, r2.y + object.height),
|
148
|
+
:color => CvColor::Red, :thickness => 2, :line_type => :aa)
|
149
|
+
}
|
150
|
+
end
|
151
|
+
|
152
|
+
ptpairs = find_pairs(object_keypoints, object_descriptors, image_keypoints, image_descriptors)
|
153
|
+
|
154
|
+
0.step(ptpairs.size - 1, 2) { |i|
|
155
|
+
r1 = object_keypoints[ptpairs[i]]
|
156
|
+
r2 = image_keypoints[ptpairs[i + 1]]
|
157
|
+
correspond.line!(r1.pt, CvPoint.new(r2.pt.x, r2.pt.y + object.height),
|
158
|
+
:color => CvColor::Red, :line_type => :aa)
|
159
|
+
}
|
160
|
+
|
161
|
+
object_keypoints.each { |r|
|
162
|
+
radius = (r.size * 1.2 / 9.0 * 2).to_i
|
163
|
+
object_color.circle!(r.pt, radius, :color => CvColor::Red, :line_type => :aa)
|
164
|
+
}
|
165
|
+
|
166
|
+
GUI::Window.new('Object Correspond').show correspond
|
167
|
+
GUI::Window.new('Object').show object_color
|
168
|
+
GUI::wait_key
|
169
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# houghcircle.rb
|
3
|
+
require "rubygems"
|
4
|
+
require "opencv"
|
5
|
+
include OpenCV
|
6
|
+
|
7
|
+
original_window = GUI::Window.new "original"
|
8
|
+
hough_window = GUI::Window.new "hough circles"
|
9
|
+
|
10
|
+
image = IplImage::load "stuff.jpg"
|
11
|
+
gray = image.BGR2GRAY
|
12
|
+
|
13
|
+
result = image.clone
|
14
|
+
original_window.show image
|
15
|
+
detect = gray.hough_circles(CV_HOUGH_GRADIENT, 2.0, 10, 200, 50)
|
16
|
+
puts detect.size
|
17
|
+
detect.each{|circle|
|
18
|
+
puts "#{circle.center.x},#{circle.center.y} - #{circle.radius}"
|
19
|
+
result.circle! circle.center, circle.radius, :color => CvColor::Red, :thickness => 3
|
20
|
+
}
|
21
|
+
hough_window.show result
|
22
|
+
GUI::wait_key
|
Binary file
|
data/examples/inpaint.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# inpaint.rb
|
3
|
+
require "rubygems"
|
4
|
+
require "opencv"
|
5
|
+
include OpenCV
|
6
|
+
|
7
|
+
puts <<EOS
|
8
|
+
Inpainting sample
|
9
|
+
|
10
|
+
Usage:
|
11
|
+
'dilate' bar - Adjust mask to inpaint (Non-black areas indicate the area that needs to be inpainted).
|
12
|
+
'radius' bar - Adjust radius of a circular neighborhood of each point inpainted.
|
13
|
+
'n' key - Inpaint using Navier-Stokes based method
|
14
|
+
't' key - Inpaint using Alexandru Telea's method
|
15
|
+
'c' key - Clear the inpaint window
|
16
|
+
'ESC' key - exit"
|
17
|
+
EOS
|
18
|
+
|
19
|
+
owindow = GUI::Window.new('original')
|
20
|
+
mwindow = GUI::Window.new('mask')
|
21
|
+
iwindow = GUI::Window.new('inpaint')
|
22
|
+
|
23
|
+
image = IplImage::load('inpaint.png')
|
24
|
+
noimage = image.zero
|
25
|
+
b, g, r = image.split
|
26
|
+
original_mask = r.threshold(0x00, 0xFF, CV_THRESH_BINARY_INV) & b.threshold(0x00, 0xFF, CV_THRESH_BINARY_INV)
|
27
|
+
mask = original_mask.copy
|
28
|
+
|
29
|
+
num_dilate = 3
|
30
|
+
mwindow.set_trackbar("dilate", 10, num_dilate) { |v|
|
31
|
+
num_dilate = v
|
32
|
+
mask = original_mask.dilate(nil, num_dilate)
|
33
|
+
mwindow.show mask
|
34
|
+
}
|
35
|
+
|
36
|
+
radius = 5
|
37
|
+
mwindow.set_trackbar("radius", 10, radius) { |v|
|
38
|
+
radius = v
|
39
|
+
}
|
40
|
+
|
41
|
+
owindow.show image
|
42
|
+
mwindow.show mask
|
43
|
+
iwindow.show noimage
|
44
|
+
|
45
|
+
while key = GUI::wait_key
|
46
|
+
case key.chr
|
47
|
+
when "\e" # esc
|
48
|
+
exit
|
49
|
+
when "n"
|
50
|
+
iwindow.show image.inpaint(:ns, mask, radius)
|
51
|
+
when "t"
|
52
|
+
iwindow.show image.inpaint(:telea, mask, radius)
|
53
|
+
when "c"
|
54
|
+
iwindow.show noimage
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
Binary file
|
data/examples/lenna.jpg
ADDED
Binary file
|
@@ -0,0 +1,88 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
3
|
+
|
4
|
+
# A sample of matching SURF feature points using kd-tree
|
5
|
+
# See http://tech.groups.yahoo.com/group/OpenCV/message/62318
|
6
|
+
|
7
|
+
require 'opencv'
|
8
|
+
include OpenCV
|
9
|
+
|
10
|
+
USE_EXTENDED_DESCRIPTOR = true
|
11
|
+
THRESHOLD = 1500
|
12
|
+
DESCRIPTOR_SIZE = USE_EXTENDED_DESCRIPTOR ? 128 : 64
|
13
|
+
|
14
|
+
img1 = CvMat.load('lenna.jpg', CV_LOAD_IMAGE_GRAYSCALE)
|
15
|
+
img2 = CvMat.load('lenna-rotated.jpg', CV_LOAD_IMAGE_GRAYSCALE)
|
16
|
+
|
17
|
+
puts 'Extracting features from img1 using SURF...'
|
18
|
+
param = CvSURFParams.new(THRESHOLD, USE_EXTENDED_DESCRIPTOR)
|
19
|
+
kp1, desc1 = img1.extract_surf(param)
|
20
|
+
puts "found #{kp1.size} keypoints from img1"
|
21
|
+
|
22
|
+
puts 'Extracting features from img2 using SURF...'
|
23
|
+
kp2, desc2 = img2.extract_surf(param)
|
24
|
+
puts "found #{kp2.size} keypoints from img2"
|
25
|
+
|
26
|
+
puts 'Matching keypoints...'
|
27
|
+
desc1mat = CvMat.new(kp1.size, DESCRIPTOR_SIZE, :cv32f, 1)
|
28
|
+
desc2mat = CvMat.new(kp2.size, DESCRIPTOR_SIZE, :cv32f, 1)
|
29
|
+
desc1.each_with_index { |desc, i|
|
30
|
+
desc.each_with_index { |d, j|
|
31
|
+
desc1mat[i, j] = CvScalar.new(d)
|
32
|
+
}
|
33
|
+
}
|
34
|
+
desc2.each_with_index { |desc, i|
|
35
|
+
desc.each_with_index { |d, j|
|
36
|
+
desc2mat[i, j] = CvScalar.new(d)
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
feature_tree = CvFeatureTree.new(desc1mat)
|
41
|
+
results, distances = feature_tree.find_features(desc2mat, 1, 250)
|
42
|
+
|
43
|
+
reverse_lookup = []
|
44
|
+
reverse_lookup_dist = []
|
45
|
+
kp1.size.times { |i|
|
46
|
+
reverse_lookup << -1
|
47
|
+
reverse_lookup_dist << Float::MAX
|
48
|
+
}
|
49
|
+
|
50
|
+
match_count = 0
|
51
|
+
kp2.size.times { |j|
|
52
|
+
i = results[j][0].to_i
|
53
|
+
d = distances[j][0]
|
54
|
+
if (d < reverse_lookup_dist[i])
|
55
|
+
match_count += 1 if reverse_lookup_dist[i] == Float::MAX
|
56
|
+
reverse_lookup[i] = j
|
57
|
+
reverse_lookup_dist[i] = d
|
58
|
+
end
|
59
|
+
}
|
60
|
+
puts "found #{match_count} putative correspondences"
|
61
|
+
|
62
|
+
points1 = []
|
63
|
+
points2 = []
|
64
|
+
kp2.size.times { |j|
|
65
|
+
i = results[j][0].to_i
|
66
|
+
if (j == reverse_lookup[i])
|
67
|
+
points1 << kp1[i].pt
|
68
|
+
points2 << kp2[j].pt
|
69
|
+
end
|
70
|
+
}
|
71
|
+
|
72
|
+
width = img1.cols + img2.cols
|
73
|
+
height = (img1.rows > img2.rows) ? img1.rows : img2.rows
|
74
|
+
correspond = IplImage.new(width, height, :cv8u, 1);
|
75
|
+
correspond.set_roi(CvRect.new(0, 0, img1.cols, img1.rows))
|
76
|
+
img1.copy(correspond)
|
77
|
+
correspond.set_roi(CvRect.new(img1.cols, 0, img1.cols + img2.cols, img2.rows))
|
78
|
+
img2.copy(correspond)
|
79
|
+
correspond.reset_roi
|
80
|
+
|
81
|
+
points1.zip(points2) { |pt1, pt2|
|
82
|
+
pt2.x += img1.cols
|
83
|
+
correspond.line!(pt1, pt2, :color => CvColor::White)
|
84
|
+
}
|
85
|
+
|
86
|
+
GUI::Window.new('Object Correspond').show correspond
|
87
|
+
GUI::wait_key
|
88
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'opencv'
|
2
|
+
require 'benchmark'
|
3
|
+
include OpenCV
|
4
|
+
|
5
|
+
data = File.join(File.dirname(__FILE__), 'matching_to_many_images')
|
6
|
+
|
7
|
+
query = IplImage.load File.join(data, 'query.png'), CV_LOAD_IMAGE_GRAYSCALE
|
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}
|
10
|
+
|
11
|
+
|
12
|
+
matchs = query.match_descriptors(images)
|
13
|
+
|
14
|
+
match_index, count = matchs.max_by {|image_index, count| count}
|
15
|
+
|
16
|
+
puts "max match: #{image_files[match_index]}"
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/examples/paint.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# paint.rb
|
3
|
+
require "rubygems"
|
4
|
+
require "opencv"
|
5
|
+
|
6
|
+
include OpenCV
|
7
|
+
|
8
|
+
window = GUI::Window.new("free canvas")
|
9
|
+
canvas = CvMat.new(500, 500, CV_8U, 3).fill!(CvColor::White) # create white canvas
|
10
|
+
window.show canvas
|
11
|
+
|
12
|
+
colors = CvColor::constants.collect{ |i| i.to_s }
|
13
|
+
|
14
|
+
usage =<<USAGE
|
15
|
+
[mouse]
|
16
|
+
drag - draw
|
17
|
+
right button - fill by color
|
18
|
+
[keyborad]
|
19
|
+
1 to 9 - change thickness of line
|
20
|
+
type color name - change color
|
21
|
+
esc - exit
|
22
|
+
USAGE
|
23
|
+
puts usage
|
24
|
+
|
25
|
+
# drawing option
|
26
|
+
opt = {
|
27
|
+
:color => CvColor::Black,
|
28
|
+
:tickness => 1
|
29
|
+
}
|
30
|
+
|
31
|
+
point = nil
|
32
|
+
window.on_mouse{ |m|
|
33
|
+
case m.event
|
34
|
+
when :move
|
35
|
+
if m.left_button?
|
36
|
+
canvas.line!(point, m, opt) if point
|
37
|
+
point = m
|
38
|
+
end
|
39
|
+
when :left_button_down
|
40
|
+
canvas.line!(m, m, opt)
|
41
|
+
point = m
|
42
|
+
when :left_button_up
|
43
|
+
point = nil
|
44
|
+
when :right_button_down
|
45
|
+
mask = canvas.flood_fill!(m, opt[:color])
|
46
|
+
end
|
47
|
+
window.show canvas
|
48
|
+
}
|
49
|
+
|
50
|
+
color_name = ''
|
51
|
+
while key = GUI.wait_key
|
52
|
+
next if key < 0 or key > 255
|
53
|
+
case key.chr
|
54
|
+
when "\e" # [esc] - exit
|
55
|
+
exit
|
56
|
+
when '1'..'9'
|
57
|
+
puts "change thickness to #{key.chr.to_i}."
|
58
|
+
opt[:thickness] = key.chr.to_i
|
59
|
+
when /[A-Za-z]/
|
60
|
+
color_name << key.chr
|
61
|
+
choice = colors.find_all{ |i| i =~ /\A#{color_name}/i }
|
62
|
+
if choice.size == 1
|
63
|
+
color,= choice
|
64
|
+
puts "change color to #{color}."
|
65
|
+
opt[:color] = CvColor::const_get(color)
|
66
|
+
end
|
67
|
+
color_name = '' if choice.size < 2
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|