OPENCV Display Image:
1 #-*-coding:utf-8-*-2 ImportNumPy as NP3 ImportCv24 fromMatplotlibImportPyplot as Plt5 6Img=cv2.imread ("Cat.png", 5)#Gta5-In7Cv2.imshow ('Image', IMG)#Show8K=cv2.waitkey (0) &0xff#read keyboard input, parameter is read in x milliseconds, x=0 is infinite wait read9 while(k!=27 andK!=ord ('s')):TenK=cv2.waitkey (0) &0xff One ifK==27: A cv2.destroyallwindows () - elifK==ord ('s'): -Cv2.imwrite ('Mess.png', IMG) theCv2.destroyallwindows ()#Close window, parameter can be window name
View Code
OpenCV load image, Matplotlib display:
1 # 2 import Span style= "color: #000000;" > NumPy as NP 3 import Cv2 4 from matplotlib import Pyplot as plt 5 6 img=cv2.imread ( cat.png Span style= "color: #800000;" " " 7 8 plt.show ()
View Code
Note: Color images are BGR mode when loaded with OPENCV. But Matplotib is RGB mode. So if the color image has been read by OPENCV, it will not be displayed correctly by Matplotib.
Simple drawing:
1 #-*-coding:utf-8-*-2 ImportNumPy as NP3 ImportCv24 fromMatplotlibImportPyplot as Plt5Cv2.namedwindow ('Hello')6Img=np.zeros ((512,512,3), np.uint8)#Create an empty picture7Cv2.line (IMG, (60,90), (511,511), (255,0,0), 6);8Cv2.rectangle (IMG, (100,70), (300,300), (100,100,100), 5)9Cv2.circle (IMG, (200,200), 60, (0,250,0), 2)TenFont=Cv2. Font_hershey_simplex OneCv2.puttext (IMG,'OpenCV', (60,400), font,4, (25,25,90), 2) ACv2.imshow ('Hello', IMG) - - cv2.waitkey (0) theCv2.destroyallwindows ()
View Code
Mouse events:
1 #-*-coding:utf-8-*-2 ImportNumPy as NP3 ImportCv24 fromMatplotlibImportPyplot as Plt5 #Mouse Event callback function function parameters Unified so6 defDraw_rectangle (event,x,y,flags,param):7 ifevent==Cv2. EVENT_LBUTTONDBLCLK:8Cv2.rectangle (IMG, (x, y), (x+30,y+30), (250,20,20), 5)9 TenImg=np.zeros ((512,512,3), np.uint8) OneCv2.namedwindow ('Hello') ACv2.setmousecallback ('Hello', Draw_rectangle) - while1: -Cv2.imshow ('Hello', IMG) the ifCv2.waitkey (&0xff==27): - Break; -Cv2.destroyallwindows ()
View Code
Slide Bar:
1 #-*-coding:utf-8-*-2 """3 Created on Sun Jan 5 13:51:344 @author: Duan5 """6 ImportCv27 ImportNumPy as NP8 defNothing (x):#the position of the slider, usually the default is pass9 PassTen #create a pair of black images OneImg=np.zeros ((250,512,3), np.uint8) ACv2.namedwindow ('Image') -Cv2.createtrackbar ('R','Image', 0,255,nothing)#Create slider bar, slide bar name, display window name, range, callback function -Cv2.createtrackbar ('G','Image', 0,255, nothing) theCv2.createtrackbar ('B','Image', 0,255, nothing) -Switch='0:off\n1:on' -Cv2.createtrackbar (Switch,'Image', 0,1, nothing) - while(1): +Cv2.imshow ('Image', IMG) -K=cv2.waitkey (&0XFF) + ifK==27: A Break atR=cv2.gettrackbarpos ('R','Image') -G=cv2.gettrackbarpos ('G','Image') -B=cv2.gettrackbarpos ('B','Image') -S=cv2.gettrackbarpos (Switch,'Image') - ifs==0: -img[:]=100 in Else: -img[:]=[B,g,r] toCv2.destroyallwindows ()
View Code
Use the slider to change the color of the circle:
1 #-*-coding:utf-8-*-2 ImportCv23 ImportNumPy as NP4r,b,g=0,0,05 defNothing (x):#the position of the slider, usually the default is pass6 Pass7 8 defdraw_circle (event,x,y,flags,param):9 ifevent==Cv2. EVENT_LBUTTONDBLCLK:TenCv2.circle (IMG1, (x, y), ten, (B,g,r), 1) One A #create a pair of black images -Img=np.zeros ((250,512,3), np.uint8) -Img1=np.zeros ((250,512,3), np.uint8) the -Cv2.namedwindow ('Image') -Cv2.namedwindow ('win') - +Cv2.createtrackbar ('R','Image', 0,255,nothing)#Create slider bar, slide bar name, display window name, range, callback function -Cv2.createtrackbar ('G','Image', 0,255, nothing) +Cv2.createtrackbar ('B','Image', 0,255, nothing) A atCv2.setmousecallback ('win', Draw_circle) - while(1): -Cv2.imshow ('win', IMG1) -Cv2.imshow ('Image', IMG) - ifCv2.waitkey (&0xff==27): - Break inR=cv2.gettrackbarpos ('R','Image') -G=cv2.gettrackbarpos ('G','Image') toB=cv2.gettrackbarpos ('B','Image') +img[:]=[B,g,r] -Cv2.destroyallwindows ()
View Code
OpenCV for Python