Ask Your Question
0

Error color.cpp

asked 2019-03-21 06:03:35 -0500

goebbi gravatar image

i using Python 2.7 and opencv 2.4.13.6

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
template = cv2.imread("bildklein.jpg")                                           #Zugriff auf Bild 2
gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
w, h = gray_template.shape[::-1]

while True:
    _, frame = cap.read()
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

    res = cv2.matchTemplate(gray_frame, gray_template, cv2.TM_CCOEFF_NORMED)
    loc = np.where(res >= 0.6)

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

    cv2.imshow("Frame", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):                   #Schliessen der Videofenster (Taste q)
        break

cap.release()
cv2.destroyAllWindows()

It doesn't work. I have three problems and i do not find a solution.

UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 18: invalid start byte

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\build\2_4_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\color.cpp, line 3783 Traceback (most recent call last):

UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 18: ordinal not in range(128)

edit retag flag offensive close merge delete

Comments

  • as a typical python noob, you never check the results from imread() or cap.open() or cap.read(), so your program gets invalid input, and you're confused

  • please check, if bildklein.jpeg is really a valid jpeg image (opencv does not think so, it tried to open it & extract some "magic image header", but found invalid unicode garbage)

berak gravatar imageberak ( 2019-03-22 02:17:42 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
-1

answered 2019-03-21 21:08:27 -0500

supra56 gravatar image
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
template = cv2.imread("bildklein.jpg", cv2.IMREAD_GRAYSCALE)
w, h = template.shape[::-1]

while True:
    _, frame = cap.read()
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    res = cv2.matchTemplate(gray_frame, template, cv2.TM_CCOEFF_NORMED)
    loc = np.where(res >= 0.7)

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

    cv2.imshow("Frame", frame)

    key = cv2.waitKey(1)

    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-21 06:03:35 -0500

Seen: 31 times

Last updated: Mar 21