Ask Your Question

Revision history [back]

How to merge rectangles of matchTemplate?

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()

How to merge rectangles of matchTemplate?

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.pngC:\fakepath\azul.png

C:\fakepath\PlacaBoa.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()

How to merge rectangles of matchTemplate?

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. 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()