openCV study
打包exe应用程序
- pyinstaller yourprogram.py(go to your program's directory and run)
openCV 安装
1. 安装指令 'pip install opencv-python'
2. 安装extra modules 'pip install opencv-contrib-python'
openCV 调用
1.'import cv2'
using openCV
read img && write img && key process
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import cv2 as cv
# Load an color image in grayscale
img = cv.imread('fam.jpg',cv.IMREAD_GRAYSCALE)
# cv.imshow('fam',img)
# cv.waitKey(0)
# cv.destroyAllWindows()
# cv.imwrite('fam_deal.png', img)
# cv.namedWindow('fam', cv.WINDOW_NORMAL)
# cv.imshow('fam', img)
# cv.waitKey(0)
# cv.destroyAllWindows()
cv.imshow('fam', img)
k = cv.waitKey(0) & 0xFF # using a 64bit machine must be modified
if k == 27:
print("即将退出")
cv.destroyAllWindows()
elif k == ord('s'):
print("生成key.png图片,生成中......")
cv.imwrite('key.png',img)
cv.destroyAllWindows()
Capture Video from Camera
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import cv2 as cv
# Capture Video from Camera
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()
play video
cap = cv.VideoCapture('test.avi')
while(cap.isOpened()):
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()
saving video
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
#define the codec and creat VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi',fourcc,30.0,(640,480))
while(cap.isOpened()):
ret, frame =cap.read()
if ret == True:
frame = cv.flip(frame,180)
#write the flipped frame
out.write(frame)
cv.imshow('frame',frame)
if cv.waitKey(1)&0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()
- 视频旋转cv2.flip(frame, 1)第一个参数表示要旋转的视频,第二个参数表示旋转的方向,0表示绕x轴旋转,大于0的数表示绕y轴旋转,小于0的负数表示绕x和y轴旋转
- cv2.flip(frame, 0)会出现视频输出倒置
draw functions in OpenCV
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import cv2 as cv
#create c black image
img = np.zeros((512,512,3),np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.line(img,(0,0),(511,511),(0,255,0),5)
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv.circle(img,(447,63), 63, (0,0,255), -1)
cv.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv.polylines(img,[pts],True,(0,255,255))
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'董占峰',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
cv.imshow('fam', img)
k = cv.waitKey(0) & 0xFF # using a 64bit machine must be modified
if k == 27:
print("即将退出")
cv.destroyAllWindows()
elif k == ord('s'):
print("生成key.png图片,生成中......")
cv.imwrite('drawing_test.png',img)
cv.destroyAllWindows()
1.支持中文需要有中文字体,使用PIL模块来支持,使用openCV无法支持
Trackbar as the Color Palette
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import cv2 as cv
def nothing(x):
pass
# create trackbars for color change
img = np.zeros((300,512,3),np.uint8)
cv.namedWindow("image")
# create trackbars for color change
cv.createTrackbar('R','image',0,255,nothing)
cv.createTrackbar('G','image',0,255,nothing)
cv.createTrackbar('B','image',0,255,nothing)
# create switch for ON/OFF functionality
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
# get current positions of four trackbars
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()