image_match 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/image_match.svg)](http://badge.fury.io/rb/image_match)
2
+
1
3
  # ImageMatch
2
4
 
3
5
  An simple image match library for view test.
@@ -52,7 +54,7 @@ Following 3 functions are prepared.
52
54
 
53
55
  2. perfect_match_template function :
54
56
  Try to find template image in scene image.
55
- This function requires that template image's size is smaller than image2.
57
+ This function requires that template image's size is smaller than scene image.
56
58
  This returns true if match score is higher than limit_similarity.
57
59
  When you set true to is_output, you can check matching result with image.
58
60
  The output image will be created at your current directory.
@@ -63,7 +65,7 @@ Following 3 functions are prepared.
63
65
 
64
66
  3. fuzzy_match_template function :
65
67
  Try to find template image in scene image.
66
- This function requires that template image's size is smaller(or equal) than image2.
68
+ This function requires that template image's size is smaller(or equal) than scene image.
67
69
  This function ignore image size, color and image detail.
68
70
  When you set true to is_output, you can check matching result with image.
69
71
  The output image will be created at your current directory.
@@ -73,6 +75,18 @@ Following 3 functions are prepared.
73
75
  fuzzy_match_template(scene_filename, template_filename, is_output=false)
74
76
  ```
75
77
 
78
+ 4. match_template_ignore_size function :
79
+ Try to find template image in scene image.
80
+ This function requires that template image's size is smaller(or equal) than scene image.
81
+ This function ignore image size.
82
+ When you set true to is_output, you can check matching result with image.
83
+ The output image will be created at your current directory.
84
+ Note that some times this is useful I think, but accuracy is not so high currently.
85
+
86
+ ```ruby
87
+ match_template_ignore_size(scene_filename, template_filename, limit_similarity=0.9, is_output=false)
88
+ ```
89
+
76
90
  ### Sample Code
77
91
 
78
92
  A sample to take screen shot on http://google.com/ and compare screen shot and prepared logo image.
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Hidetomo Suzuki"]
10
10
  spec.email = ["zuqqhi2@gmail.com"]
11
11
  spec.summary = %q{Check web page or widget design with image file automatically}
12
- spec.description = %q{1.Make page or widget image file. 2.Get current page or widget image on the web. 3.Compare them with this gem.}
12
+ spec.description = %q{This image maching utility allows us to, for example, test web page design automatically.}
13
13
  spec.homepage = "https://github.com/zuqqhi2/image_match"
14
14
  spec.license = "The BSD Liscense"
15
15
 
@@ -91,6 +91,35 @@ module ImageMatch
91
91
 
92
92
  dst_corners
93
93
  end
94
+
95
+
96
+ def get_object_location(scene_filename, template_filename)
97
+ scene, template = nil, nil
98
+ begin
99
+ scene = IplImage.load(scene_filename, CV_LOAD_IMAGE_GRAYSCALE)
100
+ template = IplImage.load(template_filename, CV_LOAD_IMAGE_GRAYSCALE)
101
+ rescue
102
+ raise RuntimeError, 'Couldn\'t read image files correctly'
103
+ return false
104
+ end
105
+
106
+ return nil unless scene.width >= template.width and scene.height >= template.height
107
+
108
+ param = CvSURFParams.new(1500)
109
+ template_keypoints, template_descriptors = template.extract_surf(param)
110
+ scene_keypoints, scene_descriptors = scene.extract_surf(param)
111
+
112
+ src_corners = [CvPoint.new(0, 0),
113
+ CvPoint.new(template.width, 0),
114
+ CvPoint.new(template.width, template.height),
115
+ CvPoint.new(0, template.height)]
116
+ return locate_planar_template(template_keypoints,
117
+ template_descriptors,
118
+ scene_keypoints,
119
+ scene_descriptors,
120
+ src_corners)
121
+ end
122
+
94
123
 
95
124
  #====================================================================
96
125
  # Public Interface
@@ -248,5 +277,60 @@ module ImageMatch
248
277
 
249
278
  return (dst_corners ? true : false)
250
279
  end
280
+
281
+ ##
282
+ #
283
+ # Calculate matching score of 1st input image and 2nd input image.
284
+ # The 2nd input image size must be smaller than 1st input image.
285
+ # This function is robust for brightness and size.
286
+ #
287
+ # @param [String] scene_filename Scene image file path
288
+ # @param [String] template_filename template image file path which you want find in scene image
289
+ # @param [Int] limit_similarity Accepting similarity (default is 90% matching)
290
+ # @param [Boolean] is_output if you set true, you can get match result with image (default is false)
291
+ # @return [Boolean] true if matching score is higher than limit_similarity
292
+ # false otherwise
293
+ #
294
+ def match_template_ignore_size(scene_filename, template_filename, limit_similarity=0.9, is_output=false)
295
+ raise ArgumentError, 'File does not exists.' unless File.exist?(scene_filename) and File.exist?(template_filename)
296
+ raise ArgumentError, 'is_output must be true or false.' unless is_output == false or is_output == true
297
+
298
+ dst_corners = get_object_location(scene_filename, template_filename)
299
+
300
+ scene, template = nil, nil
301
+ begin
302
+ scene = IplImage.load(scene_filename)
303
+ template = IplImage.load(template_filename)
304
+ rescue
305
+ raise RuntimeError, 'Couldn\'t read image files correctly'
306
+ return false
307
+ end
308
+
309
+ return false unless scene.width >= template.width and scene.height >= template.height
310
+
311
+ if dst_corners
312
+ src_corners = [CvPoint.new(0, 0),
313
+ CvPoint.new(template.width, 0),
314
+ CvPoint.new(template.width, template.height),
315
+ CvPoint.new(0, template.height)]
316
+
317
+ resize_width = (dst_corners[1].x - dst_corners[0].x) - src_corners[1].x
318
+ resize_height = (dst_corners[3].y - dst_corners[0].y) - src_corners[3].y
319
+
320
+ template = template.resize(CvSize.new(template.width + resize_width, template.height + resize_height))
321
+ end
322
+
323
+ result = scene.match_template(template, :ccoeff_normed)
324
+ min_score, max_score, min_point, max_point = result.min_max_loc
325
+
326
+ if is_output
327
+ from = max_point
328
+ to = CvPoint.new(from.x + template.width, from.y + template.height)
329
+ scene.rectangle!(from, to, :color => CvColor::Red, :thickness => 3)
330
+ scene.save_image(Time.now.to_i.to_s + "_match_result.png")
331
+ end
332
+
333
+ return (max_score >= limit_similarity ? true : false)
334
+ end
251
335
 
252
336
  end
@@ -1,3 +1,3 @@
1
1
  module ImageMatch
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -80,10 +80,10 @@ describe ImageMatch do
80
80
  expect(result).to eq true
81
81
 
82
82
  # Set is_output
83
- result = fuzzy_match_template(@image_dir + '/box_in_scene.jpg', @image_dir + '/box.jpg')
83
+ result = fuzzy_match_template(@image_dir + '/box_in_scene.jpg', @image_dir + '/box.jpg', true)
84
84
  path = File.expand_path(File.dirname(__FILE__))
85
85
  result = `ls #{path}/../*_match_result.png | wc -l`
86
- expect(result.chomp).to eq '1'
86
+ expect(result.chomp).to eq '2'
87
87
  end
88
88
 
89
89
  it 'finds that 1st input image doesn\'t contains 2nd input image' do
@@ -99,6 +99,39 @@ describe ImageMatch do
99
99
  expect{ fuzzy_match_template(@image_dir + '/lena.jpg', @image_dir + '/lena-eyes-2nd.jpg') }.to raise_error
100
100
  end
101
101
  end
102
+
103
+ describe 'match_template_ignore_size' do
104
+ it 'finds that 2nd input image is a part of 1st input image' do
105
+ # Same
106
+ result = match_template_ignore_size(@image_dir + '/stuff.jpg', @image_dir + '/stuff.jpg')
107
+ expect(result).to eq true
108
+
109
+ # Template
110
+ result = match_template_ignore_size(@image_dir + '/stuff.jpg', @image_dir + '/stuff-lighter.jpg')
111
+ expect(result).to eq true
112
+ result = match_template_ignore_size(@image_dir + '/stuff.jpg', @image_dir + '/stuff-lighter-small.jpg')
113
+ expect(result).to eq true
114
+
115
+ # Set is_output
116
+ result = match_template_ignore_size(@image_dir + '/stuff.jpg', @image_dir + '/stuff-lighter-small.jpg', 0.9, true)
117
+ path = File.expand_path(File.dirname(__FILE__))
118
+ result = `ls #{path}/../*_match_result.png | wc -l`
119
+ expect(result.chomp).to eq '3'
120
+ end
121
+
122
+ it 'finds that 1st input image doesn\'t contains 2nd input image' do
123
+ # No correlation images
124
+ result = match_template_ignore_size(@image_dir + '/box.jpg', @image_dir + '/lena-eyes.jpg', 0.9, true)
125
+ expect(result).to eq false
126
+
127
+ # Size not match
128
+ result = match_template_ignore_size(@image_dir + '/lena.jpg', @image_dir + '/box_in_scene.jpg')
129
+ expect(result).to eq false
130
+
131
+ # File does not exists
132
+ expect{ match_template_ignore_size(@image_dir + '/lena.jpg', @image_dir + '/lena-eyes-2nd.jpg') }.to raise_error
133
+ end
134
+ end
102
135
 
103
136
  after :all do
104
137
  # Delete created files
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_match
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-16 00:00:00.000000000 Z
12
+ date: 2014-11-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -75,8 +75,8 @@ dependencies:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
77
  version: 0.0.13
78
- description: 1.Make page or widget image file. 2.Get current page or widget image
79
- on the web. 3.Compare them with this gem.
78
+ description: This image maching utility allows us to, for example, test web page design
79
+ automatically.
80
80
  email:
81
81
  - zuqqhi2@gmail.com
82
82
  executables: []
@@ -100,9 +100,13 @@ files:
100
100
  - spec/image_match_spec.rb
101
101
  - spec/images/box.jpg
102
102
  - spec/images/box_in_scene.jpg
103
+ - spec/images/lena-eyes-small.jpg
103
104
  - spec/images/lena-eyes.jpg
104
105
  - spec/images/lena-noise.jpg
105
106
  - spec/images/lena.jpg
107
+ - spec/images/stuff-lighter-small.jpg
108
+ - spec/images/stuff-lighter.jpg
109
+ - spec/images/stuff.jpg
106
110
  - spec/spec_helper.rb
107
111
  homepage: https://github.com/zuqqhi2/image_match
108
112
  licenses:
@@ -119,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
123
  version: '0'
120
124
  segments:
121
125
  - 0
122
- hash: -1656355716927277807
126
+ hash: -2888227142270713127
123
127
  required_rubygems_version: !ruby/object:Gem::Requirement
124
128
  none: false
125
129
  requirements:
@@ -128,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
132
  version: '0'
129
133
  segments:
130
134
  - 0
131
- hash: -1656355716927277807
135
+ hash: -2888227142270713127
132
136
  requirements: []
133
137
  rubyforge_project:
134
138
  rubygems_version: 1.8.23.2
@@ -139,7 +143,11 @@ test_files:
139
143
  - spec/image_match_spec.rb
140
144
  - spec/images/box.jpg
141
145
  - spec/images/box_in_scene.jpg
146
+ - spec/images/lena-eyes-small.jpg
142
147
  - spec/images/lena-eyes.jpg
143
148
  - spec/images/lena-noise.jpg
144
149
  - spec/images/lena.jpg
150
+ - spec/images/stuff-lighter-small.jpg
151
+ - spec/images/stuff-lighter.jpg
152
+ - spec/images/stuff.jpg
145
153
  - spec/spec_helper.rb