Skip to content

Commit 60bc61f

Browse files
authored
Update findPhotoWithinPhoto.py
1 parent 6a14831 commit 60bc61f

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

findPhotoWithinPhoto.py

+12-21
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,48 @@
33
Yellow Circle = Center of the location of small_image.png within large_image.png. The spot to click.
44
'''
55

6-
76
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
167
method = cv2.TM_SQDIFF_NORMED
178

18-
# Read the images from the file
9+
# get the images
1910
small_image = cv2.imread('small_image.png')
2011
large_image = cv2.imread('large_image.png')
2112

13+
# look for small_image inside large_image
2214
result = cv2.matchTemplate(small_image, large_image, method)
2315

24-
# We want the minimum squared difference
16+
# minimum squared difference
2517
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
2618

27-
# Draw the rectangle:
28-
# Extract the coordinates of our best match
19+
# Coordinates of our best match
2920
MPx,MPy = mnLoc
3021

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.
3223
trows,tcols = small_image.shape[:2]
3324

34-
# TOP LEFT circle
25+
# draw TOP LEFT circle
3526
center = (MPx,MPy)
3627
radius = 5
3728
color = (0,255,0)
3829
cv2.circle(large_image, center, radius, color, thickness=1, lineType=8, shift=0)
3930

40-
# BOTTOM RIGHT circle
31+
# draw BOTTOM RIGHT circle
4132
center = (MPx+tcols,MPy+trows)
4233
radius = 5
4334
color = (0,255,0)
4435
cv2.circle(large_image, center, radius, color, thickness=1, lineType=8, shift=0)
4536

46-
# CENTER circle
47-
center = (MPx+tcols/2,MPy+trows/2)
37+
# draw CENTER circle
38+
center = (MPx+tcols//2,MPy+trows//2)
4839
radius = 5
4940
color = (0,255,255)
5041
cv2.circle(large_image, center, radius, color, thickness=10, lineType=8, shift=0)
5142

52-
# Step 3: Draw the rectangle on large_image
43+
# draw red rectangle
5344
cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)
5445

55-
# Display the original image with the rectangle around the match.
46+
# display the results
5647
cv2.imshow('output',large_image)
5748

58-
# The image is only displayed if we call this
49+
# wait for key press with 0 delay
5950
cv2.waitKey(0)

0 commit comments

Comments
 (0)