re-implement equipment with masks

This commit is contained in:
2022-05-16 14:57:15 +02:00
parent 7feb318a99
commit d362b9e2c8
2 changed files with 128 additions and 113 deletions

View File

@@ -72,12 +72,18 @@ class Vision:
return keep_rects
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)
@@ -91,6 +97,15 @@ class Vision:
# concatenate together results without causing an error
if not locations:
return np.array([], dtype=np.int32).reshape(0, 4)
#while len(locations) > 1000:
# threshold = threshold + 0.01
# locations = np.where(result >= threshold)
# locations = list(zip(*locations[::-1]))
# print("modified treshhold to:" + str(threshold))
# print("actual locations:" + str(len(locations)))
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().