当我运行应该在黑色表面上画一条线的代码时,我没有收到任何错误消息,但也未显示任何内容。怎么了 ?
import numpy as np
import cv2
class DessinerLigne:
def dessinerLigne(self):
# Create a black image
self.img=np.zeros((512,512,3),np.uint8)
# Draw a diagonal blue line with thickness of 5 px
self.img=cv2.line(self.img,(0,0),(511,511),(255,0,0),5)
# If q is pressed then exit program
self.k=cv2.waitKey(0)
if self.k==ord('q'):
cv2.destroyAllWindows()
if __name__=="__main__":
DL=DessinerLigne()
DL.dessinerLigne()
最佳答案
在OpenCV doc中,您可以看到cv2.line()不返回任何内容,但可以就地运行。
所以你的代码可以是
import numpy as np
import cv2
class DessinerLigne:
def dessinerLigne(self):
# Create a black image
self.img=np.zeros((512,512,3),np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv2.line(self.img,(0,0),(511,511),(255,0,0),5)
cv2.imshow("Image", self.img)
# If q is pressed then exit program
self.k=cv2.waitKey(0)
if self.k==ord('q'):
cv2.destroyAllWindows()
if __name__=="__main__":
DL=DessinerLigne()
DL.dessinerLigne()
关于python-2.7 - 在OpenCV中画线不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28295002/