openCV Variable umleiten -wie?

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Short
User
Beiträge: 2
Registriert: Montag 23. November 2015, 15:23

Hallo Gemeinschaft,
ich bin neu hier und hoffe, dass ich die richtige Robrik gewählt habe.

Ich möchte die Variable top_left in eine Datei umleiten.
Leider bekomme ich eine Fehlermeldung.
Der Befehl print schreibt den Inhalt korrekt in die Konsole dann kommt die Fehlermeldung /zB

(127 , 60)
Traceback (most recent call last)
File* /template_matching py* , line 48, in <module>
datei write(wert
TypeError must be string or buffer, not tuple

Was mich iritiert,dass print damit umgehen kann.

Vielleicht hat ja Jemand eine Info für mich


Code: Alles auswählen

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('messi5.ppm',0)
img2 = img.copy()
template = cv2.imread('template.ppm',0)
w, h = template.shape[::-1]

methods = ['cv2.TM_SQDIFF']
for meth in methods:
    img = img2.copy()
    method = eval(meth)
    # Apply template Matching
    res = cv2.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    
#    top_left = 160 , 100 
    cv2.rectangle(img,top_left, bottom_right ,255,2)
    cv2.imwrite('gesucht.ppm',img)

print top_left

wert = top_left

datei = open("positiontxt","wb")
datei.write(wert)
datei.close()
Gruß
Short
Benutzeravatar
cofi
Python-Forum Veteran
Beiträge: 4432
Registriert: Sonntag 30. März 2008, 04:16
Wohnort: RGFybXN0YWR0

Willkommen im Forum und zu Python!

`print` kann damit umgehen, weil Tupel eine `__repr__` bzw `__str__` Methode haben und ruft diese implizit auf, bei Dateien passiert das aber nicht.

Was du hier machen musst ist selbst den String zu erzeugen, z.B. mit

Code: Alles auswählen

wert = str(top_left)
wenn dir die Darstellung reicht, die man bei `print` sieht.
Short
User
Beiträge: 2
Registriert: Montag 23. November 2015, 15:23

Hallo Cofi,
danke für die schnelle Antwort.

Es ist perfekt.

Gruß
Short
Antworten