使用opencv绘制形状

问题描述 投票:-1回答:1

我正在尝试使用Opencv练习绘制形状。

import numpy as np
import cv2 as cv

img = np.zeros((512,512,3), np.uint8)
#draw a red line
img = cv.line(img, (100,100), (300,300), (0,0,255),4)
#img = cv.circle(img,(447,63), 63, (0,0,255), -1)
cv.imshow('image',img)
cv.waitKey(0)
cv2.destroyAllWindows()

当我跑步时,我得到错误

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp, line 269
Traceback (most recent call last):
  File "shapes.py", line 10, in <module>
    cv.imshow('image',img)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow

我搜索了各种谷歌链接,但没有人帮助形状。我感谢任何帮助。

opencv
1个回答
1
投票

错误消息表示您将不正确的变量作为输入图像传递。首先,确保你的numpy数组成功创建,然后不要尝试使用行函数的返回值,改为:

img = cv.line(img, (100,100), (300,300), (0,0,255),4)  

至:

cv.line(img, (100,100), (300,300), (0,0,255),4)
© www.soinside.com 2019 - 2024. All rights reserved.