Ask Your Question
0

How to merge rectangles of matchTemplate?

asked 2020-05-06 08:48:55 -0500

updated 2020-05-06 10:02:08 -0500

supra56 gravatar image

Good morning. I'm with some trouble with this code. I want to check if the PCB is ok with manual insertions, i found some codes to check this with a template, ok. But when the componentes are found, the code create some rectangles, but sometimes, create overlap rectangles, here is the code.

C:\fakepath\azul.png

C:\fakepath\PlacaBoa.png

import cv2
import numpy as np

img_rgb = cv2.imread('placaboa.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

#Azul
azul = cv2.imread('azul.png',0)
w, h = azul.shape[::-1]

res = cv2.matchTemplate(img_gray, azul, cv2.TM_CCOEFF_NORMED)
threshold = 0.75
loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)

cv2.imshow('Detected',img_rgb)

cv2.waitKey(0)
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-05-06 11:43:31 -0500

supra56 gravatar image

Change this:

threshold = 0.75

to:

threshold = 0.99

Change thickness::

cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)

to:

cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 1)

Output:

image description

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-05-06 08:48:55 -0500

Seen: 52 times

Last updated: May 06