|
@@ -1,6 +1,7 @@
|
|
|
import cv2
|
|
import cv2
|
|
|
import numpy as np
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
+
|
|
|
def non_max_suppression(boxes, overlapThresh):
|
|
def non_max_suppression(boxes, overlapThresh):
|
|
|
if len(boxes) == 0:
|
|
if len(boxes) == 0:
|
|
|
return []
|
|
return []
|
|
@@ -12,10 +13,10 @@ def non_max_suppression(boxes, overlapThresh):
|
|
|
pick = []
|
|
pick = []
|
|
|
|
|
|
|
|
# Grab the coordinates of the bounding boxes
|
|
# Grab the coordinates of the bounding boxes
|
|
|
- x1 = boxes[:,0]
|
|
|
|
|
- y1 = boxes[:,1]
|
|
|
|
|
- x2 = boxes[:,2]
|
|
|
|
|
- y2 = boxes[:,3]
|
|
|
|
|
|
|
+ x1 = boxes[:, 0]
|
|
|
|
|
+ y1 = boxes[:, 1]
|
|
|
|
|
+ x2 = boxes[:, 2]
|
|
|
|
|
+ y2 = boxes[:, 3]
|
|
|
|
|
|
|
|
# Compute the area of the bounding boxes and sort by bottom-right y-coordinate
|
|
# Compute the area of the bounding boxes and sort by bottom-right y-coordinate
|
|
|
area = (x2 - x1 + 1) * (y2 - y1 + 1)
|
|
area = (x2 - x1 + 1) * (y2 - y1 + 1)
|
|
@@ -43,15 +44,18 @@ def non_max_suppression(boxes, overlapThresh):
|
|
|
overlap = (w * h) / area[idxs[:last]]
|
|
overlap = (w * h) / area[idxs[:last]]
|
|
|
|
|
|
|
|
# Delete all indexes from the index list that have overlap greater than the threshold
|
|
# Delete all indexes from the index list that have overlap greater than the threshold
|
|
|
- idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0])))
|
|
|
|
|
|
|
+ idxs = np.delete(
|
|
|
|
|
+ idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0]))
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
# Return only the bounding boxes that were picked
|
|
# Return only the bounding boxes that were picked
|
|
|
return boxes[pick].astype("int")
|
|
return boxes[pick].astype("int")
|
|
|
|
|
|
|
|
|
|
+
|
|
|
# Load images
|
|
# Load images
|
|
|
-target_image = cv2.imread('test/20240108_021855.png')
|
|
|
|
|
-template_image = cv2.imread('templates/end_of_log.jpg')
|
|
|
|
|
-w, h = template_image.shape[:-1]
|
|
|
|
|
|
|
+target_image = cv2.imread("test/20240108_224218.jpg")
|
|
|
|
|
+template_image = cv2.imread("templates/dungeon/attack.png")
|
|
|
|
|
+h, w = template_image.shape[:-1]
|
|
|
|
|
|
|
|
# Template matching
|
|
# Template matching
|
|
|
result = cv2.matchTemplate(target_image, template_image, cv2.TM_CCOEFF_NORMED)
|
|
result = cv2.matchTemplate(target_image, template_image, cv2.TM_CCOEFF_NORMED)
|
|
@@ -70,15 +74,15 @@ rectangles = [(*loc, loc[0] + w, loc[1] + h) for loc in locations]
|
|
|
rectangles = non_max_suppression(rectangles, 0.3)
|
|
rectangles = non_max_suppression(rectangles, 0.3)
|
|
|
|
|
|
|
|
# Draw rectangles around matches
|
|
# Draw rectangles around matches
|
|
|
-for (startX, startY, endX, endY) in rectangles:
|
|
|
|
|
|
|
+for startX, startY, endX, endY in rectangles:
|
|
|
cv2.rectangle(target_image, (startX, startY), (endX, endY), (0, 255, 0), 2)
|
|
cv2.rectangle(target_image, (startX, startY), (endX, endY), (0, 255, 0), 2)
|
|
|
|
|
|
|
|
# Print the coordinates of the rectangle
|
|
# Print the coordinates of the rectangle
|
|
|
- centerX = round(startX + (endX-startX)/2)
|
|
|
|
|
- centerY = round(startY + (endY-startY)/2)
|
|
|
|
|
|
|
+ centerX = round(startX + (endX - startX) / 2)
|
|
|
|
|
+ centerY = round(startY + (endY - startY) / 2)
|
|
|
print(f"center coordinates: {centerX}/{centerY}")
|
|
print(f"center coordinates: {centerX}/{centerY}")
|
|
|
|
|
|
|
|
# Display the result
|
|
# Display the result
|
|
|
-cv2.imshow('Matched Results', target_image)
|
|
|
|
|
|
|
+cv2.imshow("Matched Results", target_image)
|
|
|
cv2.waitKey(0)
|
|
cv2.waitKey(0)
|
|
|
cv2.destroyAllWindows()
|
|
cv2.destroyAllWindows()
|