Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 35 lines (29 sloc) 1.042 kb
c4912fd New
rmagick authored
1 require 'RMagick'
2
3 # The Image#find_similar_region searches for a region in the image
4 # similar to the target. This example uses a rectangle from the image
5 # as the target, assuring that find_similar_region will succeed.
6
7 # Draw a red rectangle over the image that shows where the target matched.
8
9 img = Magick::Image.read('../doc/ex/images/Flower_Hat.jpg').first
10 target = img.crop(21, 94, 118, 126)
11
12 begin
13 res = img.find_similar_region(target)
14 if res
15 gc = Magick::Draw.new
16 gc.stroke('red')
17 gc.stroke_width(2)
18 gc.fill('none')
19 gc.rectangle(res[0], res[1], res[0]+target.columns, res[1]+target.rows)
20 gc.draw(img)
21 img.matte = false
22 puts "Found similar region. Writing `find_similar_region.gif'..."
23 img.write('find_similar_region.gif')
24 else
25 puts "No match!"
26 end
27 rescue NotImplementedError
28 $stderr.puts <<-END_MSG
29 The find_similar_region method is not supported by this version of
30 ImageMagick/GraphicsMagick.
31 END_MSG
32 end
33
34 exit
Something went wrong with that request. Please try again.