小编use*_*127的帖子

OpenCV:如何用鼠标连续绘图?

我修改了一个取自 OpenCV 文档的简单程序。

我只是想连续使用鼠标指针进行绘图。目前我成功地绘制了,但不是连续的,除非我移动鼠标光标太慢。

代码:

import cv2
import numpy as np 

drawing=False # true if mouse is pressed
mode=True # if True, draw rectangle. Press 'm' to toggle to curve

# mouse callback function
def interactive_drawing(event,x,y,flags,param):
    global ix,iy,drawing, mode

    if event==cv2.EVENT_LBUTTONDOWN:
        drawing=True
        ix,iy=x,y

    elif event==cv2.EVENT_MOUSEMOVE:
        if drawing==True:
            if mode==True:
                cv2.circle(img,(x,y),1,(0,0,255),-1)
    elif event==cv2.EVENT_LBUTTONUP:
        drawing=False
        if mode==True:
            cv2.circle(img,(x,y),1,(0,0,255),-1)        


img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('Window')
cv2.setMouseCallback('Window',interactive_drawing)
while(1):
    cv2.imshow('Window',img)
    k=cv2.waitKey(1)&0xFF
    if k==27:
        break
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

希望这个屏幕截图可以解释我的问题:小线看起来是连续的,因为我必须太慢地移动鼠标光标。较长的线不连续,因为我必须以正常速度移动光标:

在此输入图像描述

我希望能够像这样连续绘制:

在此输入图像描述

任何人都可以展示如何解决这个问题?先感谢您。

python mouse drawing opencv python-2.7

4
推荐指数
1
解决办法
9680
查看次数

OpenCV:检测鼠标点击图片的位置

我有以下代码,其中仅使用OpenCV显示图像:

    import numpy as np 
    import cv2

    class LoadImage:
        def loadImage(self):
            self.img=cv2.imread('photo.png')
            cv2.imshow('Test',self.img)

            self.pressedkey=cv2.waitKey(0)

            # Wait for ESC key to exit
            if self.pressedkey==27:
                cv2.destroyAllWindows()

    # Start of the main program here        
    if __name__=="__main__":
        LI=LoadImage()
        LI.loadImage()
Run Code Online (Sandbox Code Playgroud)

当窗口中显示照片后,我想在控制台(终端)上单击图片时显示鼠标的位置。我不知道如何执行此操作。有什么帮助吗?

mouse opencv python-2.7

3
推荐指数
3
解决办法
2万
查看次数

标签 统计

mouse ×2

opencv ×2

python-2.7 ×2

drawing ×1

python ×1