python-2.7 - 在 OpenCV 中画一条线不起作用

标签 python-2.7 opencv drawing 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
        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/

相关文章:

python - 在 Python 中查找 Safari 的高效用户代理正则表达式

将函数作为可选参数的 Python 函数定义

c# - 以编程方式创建流程图

android - 如何实现 DrawLine 函数?

javascript - Openlayers 绘图的某些部分是不可见的

python - pygame连续和同时按键输入

python - 如何使用 Flask WTF FileField 实际上传文件

java - Android 中使用边缘检测进行图像透视裁剪

image - 使用 findContours 时如何避免检测图像帧

c# - 不使用Aforge或OpenCv进行圆检测