You are on page 1of 7

1.

StudyNotes0322-Week5-01
1.1 study materials
https://www.javatpoint.com/opencv

OpenCV Tutorial

opencv24-python-tutorials-readthedocs-io-en-stable.pdf

1.2 Mouse Programming


1.2.1 start jupyter nootbook

1.2.2 right click to erase a circle


add some function to be continued

1.2.3 Mouse Event Programming


1.2.3.1 code explaination

opencv24-python-tutorials-readthedocs-io-en-stable.pdf Page 31

k = cv2.waitKey(1) & 0xFF if k == ord('m'):

0xFF = 11111111, 0xFF is a Hexadecimal number

remian 8bits of cv2.waitKey(1), 00001111 & 11111111 = 00001111

if k == ord('m'):
mode = not mode

elif k == 27:

break

The ord () function is mainly used to return the ASCII code of the corresponding character.
Python() function returns the Unicode code from a given character.

k == 27, 27 represent Esc key


1.2.3.2 Keyboard key value table

https://theasciicode.com.ar/

1.2.3.3 source code

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

ix,iy = -1,-1

#mouse callback function

def draw_circle(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.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)

else:

cv2.circle(img,(x,y),5,(0,0,255),-1)

elif event == cv2.EVENT_LBUTTONUP:

drawing = False

if mode == True:

cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)

else:

cv2.circle(img,(x,y),50,(0,0,255),-1)

img = np.zeros((512,512,3), np.uint8)

cv2.namedWindow('image')

cv2.setMouseCallback('image',draw_circle)

while(1):

cv2.imshow('image',img)

k = cv2.waitKey(1) & 0xFF

if k == ord('m'):

mode = not mode

elif k == 27:

break

cv2.destroyAllWindows()

1.2.3.4 running result

1.2.3.5 choose color

import cv2

import numpy as np

def nothing(x):

pass

#Create a black image, a window

img = np.zeros((300,512,3), np.uint8)

cv2.namedWindow('image')

#create trackbars for color change

cv2.createTrackbar('R','image',0,255,nothing)

cv2.createTrackbar('G','image',0,255,nothing)

cv2.createTrackbar('B','image',0,255,nothing)

#create switch for ON/OFF functionality

switch = '0 : OFF \n1 : ON'

cv2.createTrackbar(switch, 'image',0,1,nothing)

while(1):

cv2.imshow('image',img)

k = cv2.waitKey(1) & 0xFF

if k == 27:

break

#get current positions of four trackbars

r = cv2.getTrackbarPos('R','image')

g = cv2.getTrackbarPos('G','image')

b = cv2.getTrackbarPos('B','image')

s = cv2.getTrackbarPos(switch,'image')

if s == 0:

img[:] = 0

else:

img[:] = [b,g,r]

cv2.destroyAllWindows()
1.2.4 keyboard Event with OpenCV-Python
import cv2

import numpy as np

img = np.zeros((300,512,3), np.uint8)

cv2.namedWindow('image')#[b,g,r]

while(1):

cv2.imshow('image',img)

k = cv2.waitKey(1) & 0xFF

if k == ord('r'): #press r key to change color red


img[:] = [0,0,255]

if k == ord('g'): #press g key to change color grenn

img[:] = [0,255,0]

elif k == 27: #perss Esc to quit

break

cv2.destroyAllWindows()

1.2.5 Snake Games with Python and OpenCV

1.2.6 puzzle games


continued!

1.2.7 drwa a circle on a image


https://techtutorialsx.com/2020/12/08/python-opencv-mouse-events/

import cv2

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

if event == cv2.EVENT_MOUSEMOVE:

print('({}, {})'.format(x, y))

imgCopy = img.copy()

cv2.circle(imgCopy, (x, y), 10, (255, 0, 0), -1)

cv2.imshow('image', imgCopy)

img = cv2.imread('hero.jpg')

cv2.imshow('image', img)

cv2.setMouseCallback('image', drawCircle)

cv2.waitKey(0)#Press Enter Key to quit

cv2.destroyAllWindows()

1.3 changed background color


Press Enter key to change color

img[:]=[255,0,0] #red color

1.4 No Homework
 

You might also like