简体   繁体   English

试图在 OpenCV 读取的图像上画线,但没有画线

[英]Trying to draw line on the image which is read by OpenCV, but no line is drawn

I build a simple code that showing image through openCV window and using setmousecallback function, I want to draw a horizontal line on the open cv image.我构建了一个简单的代码,通过 openCV 窗口显示图像并使用 setmousecallback 函数,我想在打开的 cv 图像上绘制一条水平线。 However even though there is no error alarm, no line was shown on the image.然而,即使没有错误警报,图像上也没有显示任何线条。 I think I made mistake, but as I know I can't find.我想我犯了错​​误,但我知道我找不到。 please let me know why a line can't be drawn on the "src" image.请让我知道为什么不能在“src”图像上画一条线。

import cv2 
import numpy as np 

x_start, y_start, x_end, y_end = -1,-1,-1,-1
drawing = False

def line_draw(event, x,y, flags, param):

    global x_start, y_start, x_end, y_end, drawing  

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        x_start, y_start = x,y
        print("up")
    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            cv2.line(src,(10,y),(500,y),(255,0,0),1)
            print("move")

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        cv2.line(src,(10,y),(500,y),(255,0,0),1)
        print("up")


src = cv2.imread("d:/sample.jpg")
cv2.namedWindow("image_window")
cv2.imshow("image_window", src)
cv2.setMouseCallback("image_window", line_draw)

cv2.waitKey(0)
cv2.destroyAllWindows()

Try this:尝试这个:

...
...
    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            src = cv2.line(src,(10,y),(500,y),(255,0,0),1)
            print("move")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM