ImageMatch
An simple image match library for view test.
- Ruby 2.3.3 and OpenCV 2.4.10 are tested.
- Ruby 1.9.3 and OpenCV 2.4.10 are tested.
Requirement
Installation of this library
Add this line to your application's Gemfile:
gem 'image_match'
And then execute:
$ bundle
Or install it yourself as:
$ gem install image_match
Usage
Flow
Basic flow is following.
- Get comparison source and destination image file.
- Compare them with this library.
Interfaces
Following 3 functions are prepared.
- perfect_match function : Calc match score between image1 and image2. This function requires same size image as image1 and image2 This returns true if match score is higher than limit_similarity. When you set true to is_output, you can check matching result with image. The image will be created at your current directory.
perfect_match(image1_filename, image2_filename, limit_similarity=0.9, is_output=false)
- perfect_match_template function : Try to find template image in scene image. This function requires that template image's size is smaller than scene image. This returns true if match score is higher than limit_similarity. When you set true to is_output, you can check matching result with image. The output image will be created at your current directory.
perfect_match_template(scene_filename, template_filename, limit_similarity=0.9, is_output=false)
- fuzzy_match_template function : Try to find template image in scene image. This function requires that template image's size is smaller(or equal) than scene image. This function ignore image size, color and image detail. When you set true to is_output, you can check matching result with image. The output image will be created at your current directory. Note that some times this is useful I think, but accuracy is not so high currently.
fuzzy_match_template(scene_filename, template_filename, is_output=false)
- match_template_ignore_size function : Try to find template image in scene image. This function requires that template image's size is smaller(or equal) than scene image. This function ignore image size. It means you can match when your template image's size is not same compared with one in scene image. When you set true to is_output, you can check matching result with image. The output image will be created at your current directory. Note that some times this is useful I think, but accuracy is not so high currently.
match_template_ignore_size(scene_filename, template_filename, limit_similarity=0.9, is_output=false)
Sample Code
A sample to take screen shot on http://google.com/ and compare screen shot and prepared logo image. You can check all files related this sample on samples directory(currently sample is only one).
Gemfile
source 'https://rubygems.org'
gem 'capybara'
gem 'poltergeist'
gem 'image_match'
sample.rb
require 'capybara'
require 'capybara/poltergeist'
require 'image_match'
include ImageMatch
# Get current google web page image
url = 'http://google.com/'
Capybara.javascript_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new app, js_errors: false
end
Capybara.default_selector = :xpath
session = Capybara::Session.new(:poltergeist)
session.driver.headers = {'User-Agent' => "Mozilla/5.0 (Macintosh; Intel Mac OS X)"}
session.visit(url)
session.save_screenshot('screenshot.png', full: true)
# Compare logo (output match result image)
# Main source code related this library is following only 1 line
if perfect_match_template('./screenshot.png', './google-logo.jpg', 0.9, true)
puts "Exists!"
else
puts "Nothing..."
end
Result(*_match_result.png)
Contributing
- Fork it ( https://github.com/[my-github-username]/image_match/fork )
- Create your feature branch (
git checkout -b feature/my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin feature/my-new-feature
) - Create a new Pull Request to develop branch Note that develop branch is newest version(not release version).
LICENSE:
The BSD Liscense
see LICENSE.txt