File: find_similar_region.rb

package info (click to toggle)
ruby-rmagick 2.13.1-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,524 kB
  • sloc: ansic: 16,755; ruby: 9,730; sh: 14; makefile: 12
file content (34 lines) | stat: -rw-r--r-- 1,077 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'RMagick'

#   The Image#find_similar_region searches for a region in the image
#   similar to the target. This example uses a rectangle from the image
#   as the target, assuring that find_similar_region will succeed.

#   Draw a red rectangle over the image that shows where the target matched.

img = Magick::Image.read(File.join(File.dirname(__FILE__), '../doc/ex/images/Flower_Hat.jpg')).first
target = img.crop(21, 94, 118, 126)

begin
    res = img.find_similar_region(target)
    if res
        gc = Magick::Draw.new
        gc.stroke('red')
        gc.stroke_width(2)
        gc.fill('none')
        gc.rectangle(res[0], res[1], res[0]+target.columns, res[1]+target.rows)
        gc.draw(img)
        img.matte = false
        puts "Found similar region. Writing `find_similar_region.gif'..."
        img.write('find_similar_region.gif')
    else
        puts "No match!"
    end
rescue NotImplementedError
    $stderr.puts <<-END_MSG
    The find_similar_region method is not supported by this version of
    ImageMagick/GraphicsMagick.
    END_MSG
end

exit