How to draw multiple lines on a video frame using python by mouse click events

冷暖自知 提交于 2020-04-18 05:41:30

问题


I have a code which draws a line on a video frame using opencv python. But in my case, actually I need to draw 3 lines on the same frame one by one. Which means drawing one line followed by another like that. This is the code I have

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
class getPoints:
    def __init__(self,frame,points):
        self.frame = frame
        self.points = points

    def interactive_drawing(self,event,x,y,flags,param):
        global ix,iy,drawing, mode
        if event==cv2.EVENT_LBUTTONDOWN:
            self.points.clear()
            drawing=True
            ix,iy=x,y
            print(ix,it)
            self.points.append((x,y))
        elif event==cv2.EVENT_MOUSEMOVE:
            if drawing==True:
                if mode==True:
                    #cv2.circle(self.frame,(x,y),1,(0,0,255),-1)
                    print("move:",x,y)
                    #self.points.append([x,y])
        elif event==cv2.EVENT_LBUTTONUP:
            drawing=False
            if mode==True:
                cv2.circle(self.frame,(x,y),1,(0,0,255),-1)
                self.points.append((x,y))
                #print(self.points)
        return self.points


class ROIselecter:
    def drawROI(self,line1):
        self.line1 =line1
        global cap
        ret, frame = cap.read()
        frame_resized = imutils.resize(frame, width=500)
        self.line1 = []
        gp = getPoints(frame_resized, self.line1)
        cv2.namedWindow('Window')
        cv2.setMouseCallback('Window', gp.interactive_drawing)

        while (1):
            cv2.imshow('Window', frame_resized)
            k = cv2.waitKey(1) & 0xFF
            if k == ord('q'):
                break
        cv2.destroyAllWindows()

What I have is one line and I am performing an algorithm which takes initial point and final point of the line as the inputs and gives the result. Now I want to perform the same algorithm with the three lines. Please help to solve this problem!!!

来源:https://stackoverflow.com/questions/60753876/how-to-draw-multiple-lines-on-a-video-frame-using-python-by-mouse-click-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!