Python-OpenCV-Vorlagenabgleich (2+ verschiedene Vorlagen stimmen in einem Bild überein)Python

Python-Programme
Guest
 Python-OpenCV-Vorlagenabgleich (2+ verschiedene Vorlagen stimmen in einem Bild überein)

Post by Guest »

Ich bin Anfänger mit Grundkenntnissen in Python (einige Projekte in der Vergangenheit) und folge jetzt
Tutorial für den OpenCV-Vorlagenabgleich.
Es funktioniert gut, ich habe die Bilder dafür geändert meinen Bedürfnissen und es funktioniert.
Ich möchte mehr Vorlagenbilder auf einmal abgleichen und sie mit Rechtecken auf dem Originalbild markieren.
Auf dem Bild sind zum Beispiel ein Apfel und eine Birne zu sehen. und ich möchte sowohl Apfel als auch Birne gleichzeitig markieren.
Das Tutorial ist für den Abgleich einzelner Vorlagen, und ich habe Schleifen ausprobiert, um mehr Vorlagen abzugleichen, aber
es ergibt immer nur eine Objektidentifizierung.
Dies ist der Originalcode aus dem Tutorial:

Code: Select all

import cv2
import numpy as np
from imutils.object_detection import non_max_suppression

# Reading the image and the template
img = cv2.imread('Assets/img3.png')
temp = cv2.imread('Assets/logo_2.png')

# save the image dimensions
W, H = temp.shape[:2]

# Define a minimum threshold
thresh = 0.4

# Converting them to grayscale
img_gray = cv2.cvtColor(img,
cv2.COLOR_BGR2GRAY)
temp_gray = cv2.cvtColor(temp,
cv2.COLOR_BGR2GRAY)

# Passing the image to matchTemplate method
match = cv2.matchTemplate(
image=img_gray, templ=temp_gray,
method=cv2.TM_CCOEFF_NORMED)

# Select rectangles with
# confidence greater than threshold
(y_points, x_points) = np.where(match >= thresh)

# initialize our list of rectangles
boxes = list()

# loop over the starting (x, y)-coordinates again
for (x, y) in zip(x_points, y_points):

# update our list of rectangles
boxes.append((x, y, x + W, y + H))

# apply non-maxima suppression to the rectangles
# this will create a single bounding box
boxes = non_max_suppression(np.array(boxes))

# loop over the final bounding boxes
for (x1, y1, x2, y2) in boxes:

# draw the bounding box on the image
cv2.rectangle(img, (x1, y1), (x2, y2),
(255, 0, 0), 3)

# Show the template and the final output
cv2.imshow("Template", temp)
cv2.imshow("After NMS", img)
cv2.waitKey(0)

# destroy all the windows
# manually to be on the safe side
cv2.destroyAllWindows()

Ich habe versucht, eine Schleife und eine Funktion hinzuzufügen, aber es funktioniert nicht

Code: Select all

def doTemplateMatch():
for entry in temp_series:
match = cv2.matchTemplate(image=img_gray, templ=entry, method=cv2.TM_CCOEFF_NORMED)
print("in dotamplematch loop")
return match

# Select rectangles with
# confidence greater than threshold
(y_points, x_points) = np.where(doTemplateMatch() >= thresh)
Können Sie mir Ratschläge oder eine funktionierende Lösung geben, damit ich analysieren und lernen kann?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post