|
3 | 3 | Yellow Circle = Center of the location of small_image.png within large_image.png. The spot to click.
|
4 | 4 | '''
|
5 | 5 |
|
6 |
| - |
7 | 6 | import cv2
|
8 |
| -''' |
9 |
| -if cv2.__version__[:1] == '2': |
10 |
| - import cv2 as cv |
11 |
| - method = cv2.TM_SQDIFF_NORMED |
12 |
| -elif cv2.__version__[:1] == '3': |
13 |
| -''' |
14 |
| - |
15 |
| -#import cv2 as cv |
16 | 7 | method = cv2.TM_SQDIFF_NORMED
|
17 | 8 |
|
18 |
| -# Read the images from the file |
| 9 | +# get the images |
19 | 10 | small_image = cv2.imread('small_image.png')
|
20 | 11 | large_image = cv2.imread('large_image.png')
|
21 | 12 |
|
| 13 | +# look for small_image inside large_image |
22 | 14 | result = cv2.matchTemplate(small_image, large_image, method)
|
23 | 15 |
|
24 |
| -# We want the minimum squared difference |
| 16 | +# minimum squared difference |
25 | 17 | mn,_,mnLoc,_ = cv2.minMaxLoc(result)
|
26 | 18 |
|
27 |
| -# Draw the rectangle: |
28 |
| -# Extract the coordinates of our best match |
| 19 | +# Coordinates of our best match |
29 | 20 | MPx,MPy = mnLoc
|
30 | 21 |
|
31 |
| -# Step 2: Get the size of the template. This is the same size as the match. |
| 22 | +# Get the size of the template. Same size as the match. |
32 | 23 | trows,tcols = small_image.shape[:2]
|
33 | 24 |
|
34 |
| -# TOP LEFT circle |
| 25 | +# draw TOP LEFT circle |
35 | 26 | center = (MPx,MPy)
|
36 | 27 | radius = 5
|
37 | 28 | color = (0,255,0)
|
38 | 29 | cv2.circle(large_image, center, radius, color, thickness=1, lineType=8, shift=0)
|
39 | 30 |
|
40 |
| -# BOTTOM RIGHT circle |
| 31 | +# draw BOTTOM RIGHT circle |
41 | 32 | center = (MPx+tcols,MPy+trows)
|
42 | 33 | radius = 5
|
43 | 34 | color = (0,255,0)
|
44 | 35 | cv2.circle(large_image, center, radius, color, thickness=1, lineType=8, shift=0)
|
45 | 36 |
|
46 |
| -# CENTER circle |
47 |
| -center = (MPx+tcols/2,MPy+trows/2) |
| 37 | +# draw CENTER circle |
| 38 | +center = (MPx+tcols//2,MPy+trows//2) |
48 | 39 | radius = 5
|
49 | 40 | color = (0,255,255)
|
50 | 41 | cv2.circle(large_image, center, radius, color, thickness=10, lineType=8, shift=0)
|
51 | 42 |
|
52 |
| -# Step 3: Draw the rectangle on large_image |
| 43 | +# draw red rectangle |
53 | 44 | cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)
|
54 | 45 |
|
55 |
| -# Display the original image with the rectangle around the match. |
| 46 | +# display the results |
56 | 47 | cv2.imshow('output',large_image)
|
57 | 48 |
|
58 |
| -# The image is only displayed if we call this |
| 49 | +# wait for key press with 0 delay |
59 | 50 | cv2.waitKey(0)
|
0 commit comments