Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

draw line from 2 mouse event in image python

import cv2

class DrawLineWidget(object):
    def __init__(self):
        self.original_image = cv2.imread('1.jpg')
        self.clone = self.original_image.copy()

        cv2.namedWindow('image')
        cv2.setMouseCallback('image', self.extract_coordinates)

        # List to store start/end points
        self.image_coordinates = []

    def extract_coordinates(self, event, x, y, flags, parameters):
        # Record starting (x,y) coordinates on left mouse button click
        if event == cv2.EVENT_LBUTTONDOWN:
            self.image_coordinates = [(x,y)]

        # Record ending (x,y) coordintes on left mouse bottom release
        elif event == cv2.EVENT_LBUTTONUP:
            self.image_coordinates.append((x,y))
            print('Starting: {}, Ending: {}'.format(self.image_coordinates[0], self.image_coordinates[1]))

            # Draw line
            cv2.line(self.clone, self.image_coordinates[0], self.image_coordinates[1], (36,255,12), 2)
            cv2.imshow("image", self.clone) 

        # Clear drawing boxes on right mouse button click
        elif event == cv2.EVENT_RBUTTONDOWN:
            self.clone = self.original_image.copy()

    def show_image(self):
        return self.clone

if __name__ == '__main__':
    draw_line_widget = DrawLineWidget()
    while True:
        cv2.imshow('image', draw_line_widget.show_image())
        key = cv2.waitKey(1)

        # Close program with keyboard 'q'
        if key == ord('q'):
            cv2.destroyAllWindows()
            exit(1)
Comment

PREVIOUS NEXT
Code Example
Python :: selenium close browser 
Python :: sns lineplot title 
Python :: pandas to csv encoding 
Python :: img read 
Python :: pandas groupby count unique rows 
Python :: clear console in python 
Python :: number of times a value occurs in dataframne 
Python :: python make temp file 
Python :: get datatype of all columns pandas 
Python :: pandas read csv parse_dates 
Python :: python matplotlib inline 
Python :: sort list of dictionaries by key python 
Python :: open csv from google drive using python 
Python :: add rows to dataframe pandas 
Python :: from csv to pandas dataframe 
Python :: python Pandas pivot on bin 
Python :: py random list integers 
Python :: increase pie chart size python 
Python :: python get the elements between quotes in string 
Python :: python write a list to a file line by line 
Python :: scipy stats arithmetic mean 
Python :: corona shape in python 
Python :: function python to get the minimu and its position 
Python :: how to add subtitle matplotlib 
Python :: pandas groupby sum 
Python :: to_csv drop index 
Python :: wonsan 
Python :: python list comprehension index, value 
Python :: string list into list pandas 
Python :: create a df with column names 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =