0

Sorry for the noob question. My problem is that multiscale finds only one of several templates in the image and I need all of them. How can i fix this? I am only beginner, I will be grateful for any help or criticism.

for image in ims:

    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]:

        resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
        r = gray.shape[1] / float(resized.shape[1])

        if resized.shape[0] < tH or resized.shape[1] < tW:
            break

        edged = cv2.Canny(resized, 150, 350)
        result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
        (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)


        clone = np.dstack([edged, edged, edged])
        cv2.rectangle(clone, (maxLoc[0], maxLoc[1]),
                      (maxLoc[0] + tW, maxLoc[1] + tH), (0, 0, 255), 2)

        if found is None or maxVal > found[0]:
            found = (maxVal, maxLoc, r)

    (_, 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))
    if maxVal > 700000:
        cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
        cv2.imshow('img',image)
        print('good')
    elif maxVal < 700000:
        print('bad')
print(maxVal)
cv2.waitKey(0)
cv2.destroyAllWindows()
knowname
  • 29
  • 7
  • If you haven't already figured it out right now, multiscale template matching is a hard problem where it is quite difficult to find all templates in an image. It is helpful if you knew how many templates you're searching for. However failing this, the only recourse you have is to make your list of scales more granular as well as changing the max scale for your template. – rayryeng May 05 '20 at 06:59
  • @rayryeng, Thank you a lot! But if it’s not difficult for you, could you explain what you mean by "make your list of scales more granular"? And how should this help me? Or maybe if more convenient ways to collect information from the screen / image? Type of information: 2d interface, which, unfortunately, resize sometimes – knowname May 05 '20 at 09:34

0 Answers0