re-implement equipment with masks

This commit is contained in:
2022-05-15 00:50:42 +02:00
parent e7b15cc48a
commit 883a6f77aa
292 changed files with 454 additions and 173 deletions

View File

@@ -27,22 +27,32 @@ class Vision:
# TM_CCOEFF, TM_CCOEFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_SQDIFF, TM_SQDIFF_NORMED
self.method = method
def find(self, haystack_img, needle_img, threshold=0.5, max_results=10):
def find(self, haystack_img, needle_img, threshold=0.5, max_results=10, normalize=False, mask=None):
# run the OpenCV algorithm
needle_w = needle_img.shape[1]
needle_h = needle_img.shape[0]
result = cv.matchTemplate(haystack_img, needle_img, self.method)
if normalize:
result = cv.matchTemplate(haystack_img, needle_img, cv.TM_CCORR_NORMED, None, mask)
_minVal, _maxVal, minLoc, maxLoc = cv.minMaxLoc(result, None)
cv.normalize(result, result, 0, 1, cv.NORM_MINMAX, -1)
else:
result = cv.matchTemplate(haystack_img, needle_img, self.method)
# Get the all the positions from the match result that exceed our threshold
locations = np.where(result >= threshold)
locations = list(zip(*locations[::-1]))
# print(locations)
_minVal, _maxVal, minLoc, maxLoc = cv.minMaxLoc(result, None)
# if we found no results, return now. this reshape of the empty array allows us to
# concatenate together results without causing an error
if not locations:
return np.array([], dtype=np.int32).reshape(0, 4)
if len(locations) > 5000:
return np.array([], dtype=np.int32).reshape(0, 4)
# You'll notice a lot of overlapping rectangles get drawn. We can eliminate those redundant
# locations by using groupRectangles().
# First we need to create the list of [x, y, w, h] rectangles