【发布时间】:2025-11-25 15:45:01
【问题描述】:
我正在尝试使用 openCV 模块制作实时草图,但 cv2.imshow 函数显示错误。我已附上错误的屏幕截图。 请帮忙..
import cv2
def sketch(img):
img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gray_blur=cv2.GaussianBlur(img_gray,(5,5),0)
canny_edges=cv2.Canny(img_gray_blur,10,70)
ret,mask=cv2.threshold(canny_edges,70,255,cv2.THRESH_BINARY)
return mask
cap=cv2.VideoCapture(0)
while True:
ret,frame=cap.read()
cv2.imshow('live sketcher',sketch(frame))
if cv2.waitKey():
break
cap.release()
cv2.destroyAllWindows()
【问题讨论】:
-
我附上了错误截图。请看。
-
在 imshow 之前你应该检查 img 不是 null 并且 len(img.shape)==3 或 ==4
-
是的,它显示图像的长度为3..我该怎么办?
-
'def sketch(img): if img is not None: print("image is present") print(len(img.shape))'
-
代码在我重新启动 jupyter notebook 时运行,但在第二次运行时显示错误..谁能解释这个原因..?
标签: python python-3.x opencv opencv3.0