forked from peddivarshith/3dPrintedMicroscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_matching.py
80 lines (69 loc) · 3.16 KB
/
template_matching.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# import the necessary packages
import numpy as np
import argparse
import imutils
import glob
import cv2
import datetime
import time
def match_common_region(stage_image,wsi_image,visualize,name):
# load the image image, convert it to grayscale, and detect edges
template = cv2.imread(stage_image)
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(tH, tW) = template.shape[:2]
#cv2.imshow("Template", template)
# load the image, convert it to grayscale, and initialize the
# bookkeeping variable to keep track of the matched region
image = cv2.imread(wsi_image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
found = None
# loop over the scales of the image
for scale in np.linspace(0.2, 1.0, 20)[::-1]:
print('scale',scale)
# resize the image according to the scale, and keep track
# of the ratio of the resizing
resized = imutils.resize(gray, width = int(gray.shape[1] * scale))
r = gray.shape[1] / float(resized.shape[1])
# if the resized image is smaller than the template, then break
# from the loop
if resized.shape[0] < tH or resized.shape[1] < tW:
break
# detect edges in the resized, grayscale image and apply template
# matching to find the template in the image
edged = cv2.Canny(resized, 50, 200)
result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
print(maxVal)
# check to see if the iteration should be visualized
if visualize==1:
# draw a bounding box around the detected region
clone = np.dstack([edged, edged, edged])
cv2.rectangle(clone, (maxLoc[0], maxLoc[1]),
(maxLoc[0] + tW, maxLoc[1] + tH), (0, 0, 255), 2)
cv2.imshow("Visualize", clone)
cv2.waitKey(0)
# if we have found a new maximum correlation value, then update
# the bookkeeping variable
if found is None or maxVal > found[0]:
found = (maxVal, maxLoc, r)
# unpack the bookkeeping variable and compute the (x, y) coordinates
# of the bounding box based on the resized ratio
(_, maxLoc, r) = found
(startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
(endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))
ndarray=np.asarray(image)
cropped=ndarray[startY:endY,startX:endX]
# draw a bounding box around the detected result and display the image
timestamp=time.time()
timestamp=datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d-%H:%M:%S')
filename=name+timestamp+".png"
cv2.imwrite("./static/matching_region/"+filename,cropped)
print(cropped.shape)
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
compute={"startX":startX,"endX":endX,"startY":startY,"endY":endY,"filename":filename}
print("computed values")
print(compute)
return compute
#cv2.imshow("Image", image)
#cv2.waitKey(0)