安裝Opencvhtml
pip install --upgrade setuptools pip install numpy Matplotlib pip install opencv-python
opencv環境已經整好,就是這麼簡單。只須要numpy、Matplotlib、opencv-python三個包,都不大很快就能夠下好,若是下載中間出現error或wrong,從新輸入命令便可。python
參考 OpenCv官方教材文檔數組
一 Dome函數
##01打開顯示一張圖片,顯示灰度圖片動畫
import numpy as npui import cv2 as cv編碼 ##01打開顯示一張圖片,顯示灰度圖片spa img = cv.imread('E:\\fruits.jpg',0);#讀取一張圖片,注意路徑須要轉義,第二個參數是模式code cv.imwrite('E:\\fruits_copy.jpg',img);#寫一張圖片 cv.namedWindow('IMG_NAME',cv.WINDOW_NORMAL);#定義一個窗口 cv.imshow('IMG_NAME',img);#顯示窗口圖片 cv.waitKey(0);#等待響應 cv.destroyAllWindows();#銷燬窗體 |
##02使用matplotlib顯示座標畫圖
import numpy as np import cv2 as cv from matplotlib import pyplot as plt ##02使用matplotlib顯示座標畫圖 img = cv.imread('E:\\fruits.jpg',0) plt.imshow(img,cmap = 'gray',interpolation = 'bicubic')# ##plt.xticks([]),plt.yticks([])#x/y軸座標隱藏,去掉就顯示 plt.show()
|
##攝像頭顯示一幀幀圖片
import numpy as np import cv2 as cv #01內置攝像頭打開 cap = cv.VideoCapture(0) while(True): ret,frame = cap.read()# gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY) cv.imshow('frame',gray) if cv.waitKey(1) & 0xff == ord('q'): break cap.release() cv.destroyAllWindows() |
##處理後保存攝像頭視頻
##flip水平或者垂直翻轉數組
import numpy as np import cv2 #打開攝像頭 cap = cv2.VideoCapture(0)
#定義編碼器和寫對象 fourcc = cv.cv.FOURCC(*'XVID') out = cv.VideoWriter('E:\\output.avi',fourcc,20.0,(640,480)) #讀取攝像頭的一幀 while cap.isOpened(): ret,frame = cap.read() if ret == True: #翻轉圖片 frame = cv.flip(frame,0) #寫出 out.write(frame) cv.imshow('frmae',frame) #中斷判斷 if cv.waitKey(1) & 0xff == ord('q'): break else: break #釋放內存/資源 cap.release() out.release() cv.destroyAllWindows()
|
主要函數:線:cv2.line() 圓 cv2.circle() 矩陣 cv2.rectangle() 橢圓 cv2.ellipse() 文字 cv2.putText()
函數的參數:img,color,thickness,linetype
·##簡單繪畫
import numpy as np import cv2 as cv #設置一張背景圖片 img = np.zeros((512,512,3),np.uint8) ##img = cv.imread('E:\\fruits.jpg',0);
cv.line(img,(0,0),(511,511),(255,0,0),5)#畫線 cv.circle(img,(477,263),63,(0,0,255),8,8)#畫圓 cv.rectangle(img,(384,0),(510,128),(0,255,0),3)#矩陣 cv.ellipse(img,(256,256),(100,50),0,0,360,255,-1)#橢圓 font = cv.FONT_HERSHEY_SIMPLEX cv.putText(img,'helloworld',(10,500),font,4,(255,255,255),2)
##pts = np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)#連點多邊形,能夠經過多條line鏈接造成 ##pts = pts.reshape(-1,1,2)
#顯示 cv.namedWindow('IMG_NAME',cv.WINDOW_NORMAL);#定義一個窗口 cv.imshow('IMG_NAME',img);#顯示窗口圖片 cv.waitKey(0);#等待響應 cv.destroyAllWindows();#銷燬窗體 |
·##增強繪畫:左擊畫圓
import cv2 as cv import numpy as np #鼠標響應函數
def draw_circle(event,x,y,flags,param): if event == cv.EVENT_LBUTTONDBLCLK:#左擊 cv.circle(img,(x,y),63,(0,0,255),8,8)#畫圓 #窗體及顯示 img = np.zeros((512,512,3),np.uint8) cv.namedWindow('IMG_NAME');#定義一個窗口 cv.setMouseCallback('IMG_NAME',draw_circle) #重繪刷新 while(1): cv.imshow('IMG_NAME',img) if cv.waitKey(20) & 0xff == 27:#ESC退出 break
#顯示
cv.waitKey(0);#等待響應 cv.destroyAllWindows();#銷燬窗體 |
·##增強繪畫:拖動畫圖
import numpy as np import cv2 as cv drawing = False mode = 'a' ix,iy =-1,-1 #建立回調函數 def draw_circle(event,x,y,flags,params): global ix,iy,drawing,mode #左擊,返回起始位置 if event == cv.EVENT_LBUTTONDOWN: drawing = True ix,iy = x,y elif event == cv.EVENT_MOUSEMOVE and flags == cv.EVENT_FLAG_LBUTTON: if drawing == True: if mode == 'a':#畫線 cv.rectangle(img,(x,y),(x,y),(0,255,0),-1) elif mode == 'b':#畫圓 cv.circle(img,(x,y),6,(0,255,0),-1) elif event == cv.EVENT_LBUTTONUP: drawing =False ## img=np.zeros((512,512,3),np.uint8) cv.namedWindow('image') cv.setMouseCallback('image',draw_circle) ##刷新重繪 while(1): cv.imshow('image',img) k=cv.waitKey(1)&0xFF if k==ord('a'): mode='a' elif k==ord('b'): mode='b' elif k==27: break |
·##增強畫圖:調色板
import numpy as np import cv2 as cv # def nothing(x): pass # img = np.zeros((300,512,3),np.uint8) cv.namedWindow('image') cv.createTrackbar('R','image',0,255,nothing) cv.createTrackbar('G','image',0,255,nothing) cv.createTrackbar('B','image',0,255,nothing)
switch = '0:OFF\n1:ON' cv.createTrackbar(switch,'image',0,1,nothing) while(1): cv.imshow('image',img) k = cv.waitKey(1)&0xff if k == 27 : break r = cv.getTrackbarPos('R','image') g = cv.getTrackbarPos('G','image') b = cv.getTrackbarPos('B','image') s = cv.getTrackbarPos(switch,'image') if s == 0: img[:]=0 else: img[:]=[b,g,r] cv.destroyAllWindows()
|
·##上述兩種功能結合在一塊兒,可調色的畫板
import numpy as np import cv2 as cv drawing = False mode = 'a' ix,iy =-1,-1 #建立回調函數 def draw_circle(event,x,y,flags,params): global ix,iy,drawing,mode #獲取顏色值 r = cv.getTrackbarPos('R','image') g = cv.getTrackbarPos('G','image') b = cv.getTrackbarPos('B','image') color = [r,g,b]
#左擊,返回起始位置 if event == cv.EVENT_LBUTTONDOWN: drawing = True ix,iy = x,y elif event == cv.EVENT_MOUSEMOVE and flags == cv.EVENT_FLAG_LBUTTON: if drawing == True: if mode == 'a':#畫線 cv.rectangle(img,(x,y),(x,y),color,-1) elif mode == 'b':#畫圓 cv.circle(img,(x,y),6,color,-1) elif event == cv.EVENT_LBUTTONUP: drawing =False ## def nothing(x):pass
img=np.zeros((512,512,3),np.uint8) cv.namedWindow('image')
#選擇顏色 cv.createTrackbar('R','image',0,255,nothing) cv.createTrackbar('G','image',0,255,nothing) cv.createTrackbar('B','image',0,255,nothing)
cv.setMouseCallback('image',draw_circle) ##刷新重繪 while(1): cv.imshow('image',img) k=cv.waitKey(1)&0xFF if k==ord('a'): mode='a' elif k==ord('b'): mode='b' elif k==27: break |
二 API 使用說明