Gumse90210
Gumse90210

Reputation: 11

Using OpenCV Templatematch with multiple occurences

I'm playing around with OpenCV and Templatematch. I have found some code which I basically understand, but is having some problems 'adjusting' to my simple needs.

The code does template match on a picture of an HMI. The HMI consists of both 7 segments and other 'icons' lit up by LEDs. The code perfectly finds both 7 segments numbers and, icons - great !

However, as there is more that one 7 segment LED (2) I would like the code to be able to find lets say, two zeros or two ones. My challenge is how to 'adjust' the code to make this happen.

Currently the code finds only the most matching one, and draws a box around that. How do I make it draw a box around all the occurrences if will find of a certain template ?

Any help would be highly appreciated.

template_list = ['./opencvtmpl/template_0.png', './opencvtmpl/app_icon.jpg']

# load the image, convert it to grayscale, and initialize the
image = cv2.imread("./opencvtmpl/img/input_scaled.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

for template in template_list:
    # load the template image, convert it to grayscale, and detect edges
    template = cv2.imread(template)
    template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
    template = cv2.Canny(template, 50, 200)
    (tH, tW) = template.shape[:2]
    cv2.imshow("Template", template)

    # bookkeeping variable to keep track of the matched region
    found = None
    
    # loop over the scales of the image
    for scale in np.linspace(0.2, 1.0, 20)[::-1]:
        # 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)
        # check to see if the iteration should be visualized
        if (True):
            # 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)
            print(maxVal)
            #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))

    # draw a bounding box around the detected result and display the image
    cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)

cv2.imshow("Image", image)
cv2.waitKey(0)

Upvotes: 1

Views: 217

Answers (0)

Related Questions