Computer Vision OpenCV

chart

Our mouse or touch interaction with the opencv image triggers events. Let's output EVENT to see what events opencv provides for us to call.

events = [i for i in dir(cv2) if 'EVENT' in i]
print(events)

Output events. Look at how many events opencv provides us. Today we only focus on EVENT_LBUTTONDOWN and EVENT_RBUTTONDOWN, two left and right mouse events.

['EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON','EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP']

We can get the location information of the left-click event through the event, and display the location of the left-click event through the location information.

def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x,',',y)
        font = cv2.FONT_HERSHEY_SIMPLEX
        strXY = str(x) + "," + str(y)
        cv2.putText(img, strXY, (x,y), font, 1, (0,255,255),2)
        cv2.imshow('image',img)

Mouse events are captured by setMouseCallback in the form of callback functions.

img = np.zeros((512,512,3),np.uint8)
cv2.imshow('image',img)
cv2.setMouseCallback('image',click_event)
cv2.waitKey(0)
cv2.destroyAllWindows()
chart

There is no difference between right-click events and left click analogs, so you can have a try, and there is nothing to explain today.

    if event == cv2.EVENT_RBUTTONDOWN:
        blue = img[y,x, 0]
        green = img[y,x, 1]
        red = img[y,x, 2]
        font = cv2.FONT_HERSHEY_SIMPLEX
        strBGR = str(blue) + "," + str(green) +","+ str(red)
        cv2.putText(img, strBGR, (x,y), font, .5, (255,255,0),2)
        cv2.imshow('image',img)

chart

opencv.jpeg

Tags: OpenCV

Posted on Tue, 08 Oct 2019 02:46:24 -0700 by danwguy