我收到此错误:
OpenCV错误:未知函数中不支持的格式或格式组合(),文件C:\ slave \ WinInstallerMegaPack \ src \ opencv \ modules \ imgproc \ src \ canny.cpp,第67行 Traceback(最近一次调用最后一次): edges = cv2.Canny(frame,100,100) cv2.error:C:\ slave \ WinInstallerMegaPack \ srx \ opencv \ modules \ imgproc \ src \ canny.cpp:67:错误:( - 210)
当我运行此代码时:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
_, frame = cap.read()
cv2.imshow('Original',frame)
edges = cv2.Canny(frame,100,100)
cv2.imshow('Edges',edges)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
答案 0 :(得分:1)
Canny需要灰度图像作为输入,但您的frame
是3通道(BGR)图像。在将其传递给Canny之前,您需要将其转换为灰度:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 100)
作为旁注,请记住Canny阈值用于滞后,因此您可能希望将first_threshold
设置为[0.25 - 0.5] * second_threshold
edges = cv2.Canny(gray, 100, 200)