标签:
opencv显示图像:
1 # -*- coding: UTF-8 -*- 2 import numpy as np 3 import cv2 4 from matplotlib import pyplot as plt 5 6 img=cv2.imread("cat.png",5) #载入 7 cv2.imshow(‘image‘,img) #显示 8 k=cv2.waitKey(0)&0xFF #读取键盘输入,参数为在x毫秒内读取,x=0是无限等待读取 9 while(k!=27 and k!=ord(‘s‘)): 10 k=cv2.waitKey(0)&0xFF 11 if k==27: 12 cv2.destroyAllWindows() 13 elif k==ord(‘s‘): 14 cv2.imwrite(‘mess.png‘,img) 15 cv2.destroyAllWindows() #关闭窗口,参数可以为窗口名
opencv载入图像,matplotlib显示:
1 # -*- coding: UTF-8 -*- 2 import numpy as np 3 import cv2 4 from matplotlib import pyplot as plt 5 6 img=cv2.imread(‘cat.png‘) 7 plt.imshow(img) 8 plt.show()
注:彩色图像使用OpenCV 加载时是BGR 模式。但是Matplotib 是RGB模式。所以彩色图像如果已经被OpenCV 读取,那它将不会被Matplotib 正确显示。
简单画图:
1 # -*- coding: UTF-8 -*- 2 import numpy as np 3 import cv2 4 from matplotlib import pyplot as plt 5 cv2.namedWindow(‘hello‘) 6 img=np.zeros((512,512,3),np.uint8) #创建一张空图片 7 cv2.line(img,(60,90),(511,511),(255,0,0),6); 8 cv2.rectangle(img,(100,70),(300,300),(100,100,100),5) 9 cv2.circle(img,(200,200),60,(0,250,0),2) 10 font=cv2.FONT_HERSHEY_SIMPLEX 11 cv2.putText(img,‘opencv‘,(60,400),font,4,(25,25,90),2) 12 cv2.imshow(‘hello‘,img) 13 14 cv2.waitKey(0) 15 cv2.destroyAllWindows()
鼠标事件:
1 # -*- coding: UTF-8 -*- 2 import numpy as np 3 import cv2 4 from matplotlib import pyplot as plt 5 #鼠标事件回调函数 函数参数统一如此 6 def draw_rectangle(event,x,y,flags,param): 7 if event==cv2.EVENT_LBUTTONDBLCLK: 8 cv2.rectangle(img,(x,y),(x+30,y+30),(250,20,20),5) 9 10 img=np.zeros((512,512,3),np.uint8) 11 cv2.namedWindow(‘hello‘) 12 cv2.setMouseCallback(‘hello‘,draw_rectangle) 13 while 1: 14 cv2.imshow(‘hello‘,img) 15 if cv2.waitKey(20)&0xFF==27: 16 break; 17 cv2.destroyAllWindows()
滑动条:
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sun Jan 5 13:51:34 2014 4 @author: duan 5 """ 6 import cv2 7 import numpy as np 8 def nothing(x): #滑动条的位置,通常默认为pass 9 pass 10 # 创建一副黑色图像 11 img=np.zeros((250,512,3),np.uint8) 12 cv2.namedWindow(‘image‘) 13 cv2.createTrackbar(‘R‘,‘image‘,0,255,nothing) #创建滑动条 ,滑动条名称,显示窗口名称,范围,回调函数 14 cv2.createTrackbar(‘G‘,‘image‘,0,255,nothing) 15 cv2.createTrackbar(‘B‘,‘image‘,0,255,nothing) 16 switch=‘0:OFF\n1:ON‘ 17 cv2.createTrackbar(switch,‘image‘,0,1,nothing) 18 while(1): 19 cv2.imshow(‘image‘,img) 20 k=cv2.waitKey(20)&0xFF 21 if k==27: 22 break 23 r=cv2.getTrackbarPos(‘R‘,‘image‘) 24 g=cv2.getTrackbarPos(‘G‘,‘image‘) 25 b=cv2.getTrackbarPos(‘B‘,‘image‘) 26 s=cv2.getTrackbarPos(switch,‘image‘) 27 if s==0: 28 img[:]=100 29 else: 30 img[:]=[b,g,r] 31 cv2.destroyAllWindows()
利用滑动条改变颜色画圆:
1 # -*- coding: utf-8 -*- 2 import cv2 3 import numpy as np 4 r,b,g=0,0,0 5 def nothing(x): #滑动条的位置,通常默认为pass 6 pass 7 8 def draw_circle(event,x,y,flags,param): 9 if event==cv2.EVENT_LBUTTONDBLCLK: 10 cv2.circle(img1,(x,y),10,(b,g,r),-1) 11 12 # 创建一副黑色图像 13 img=np.zeros((250,512,3),np.uint8) 14 img1=np.zeros((250,512,3),np.uint8) 15 16 cv2.namedWindow(‘image‘) 17 cv2.namedWindow(‘win‘) 18 19 cv2.createTrackbar(‘R‘,‘image‘,0,255,nothing) #创建滑动条 ,滑动条名称,显示窗口名称,范围,回调函数 20 cv2.createTrackbar(‘G‘,‘image‘,0,255,nothing) 21 cv2.createTrackbar(‘B‘,‘image‘,0,255,nothing) 22 23 cv2.setMouseCallback(‘win‘,draw_circle) 24 while(1): 25 cv2.imshow(‘win‘,img1) 26 cv2.imshow(‘image‘,img) 27 if cv2.waitKey(20)&0xFF==27: 28 break 29 r=cv2.getTrackbarPos(‘R‘,‘image‘) 30 g=cv2.getTrackbarPos(‘G‘,‘image‘) 31 b=cv2.getTrackbarPos(‘B‘,‘image‘) 32 img[:]=[b,g,r] 33 cv2.destroyAllWindows()
标签:
原文地址:http://www.cnblogs.com/a1225234/p/4975618.html