【OpenCV】——b站达尔闻
达尔闻OpenCV学习
计算机视觉导论
一、计算机视觉解决的基本问题
分类,检测,分隔
二、计算机视觉的前沿进展
深度学习 海量数据
边缘计算
图像分类领域:
目标检测领域
CS231n 李飞飞教授 公开课
原始论文
安装配置Opencv-Python
https://github.com/opencv/opencv
Resources
-
Homepage:
- Courses: https://opencv.org/courses
-
Q&A forum: http://answers.opencv.org
-
Issue tracking: https://github.com/opencv/opencv/issues
pip换源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip install opencv-python
#当前查看版本
import cv2
cv2.__version__
案例
边缘计算
import cv2
cap = cv2.VideoCapture(0)
cap.open(0)
while cap.isOpened():
flag, frame = cap.read()
cv2.imshow('my_window',frame)
key_pressed = cv2.waitKey(60)
print('键盘被按下的键是:',key_pressed)
if key_pressed == 27:
break
cap.release()
cv2.destroyAllWindows()
#边缘检测
import cv2
#cap = cv2.VideoCapture(0)
import numpy as npcap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cap.open(0)
while cap.isOpened():
flag, frame = cap.read()
if not flag:
break
key_pressed = cv2.waitKey(60)
print('键盘被按下的键是:',key_pressed)
frame = cv2.Canny(frame,100,200)
frame = np.dstack(frame,frame,frame)
cv2.imshow('my_window2',frame)
if key_pressed == 27:
break
cap.release()
cv2.destroyAllWindows()
#CannyScanV1.py
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
#import numpy as npcap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cap.open(0)
while cap.isOpened():
flag, frame = cap.read()
if not flag:
break
key_pressed = cv2.waitKey(60)
print('键盘被按下的键是:',key_pressed)
frame = cv2.Canny(frame,100,200)
frame = np.dstack((frame,frame,frame))
cv2.imshow('my_window2',frame)
if key_pressed == 27:
break
cap.release()
cv2.destroyAllWindows()
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
# get a frame
ret, frame = cap.read()
# show a frame
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
#CannyScanV1.py
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
#import numpy as npcap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cap.open(0)
while cap.isOpened():
flag, frame = cap.read()
if not flag:
break
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray = cv2.blur(gray,ksize = (5, 5))
grayframe = cv2.Canny(gray,100,200)
frame = cv2.Canny(frame,100,200)
##frame = np.dstack(frame,frame,frame)
cv2.imshow('my_window',frame)
cv2.imshow('my_window2',grayframe)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
笑脸检测
#SmileCaptureV1.py
import cv2
#人脸检测 眼睛检测,微笑检测
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_smile.xml')
cap = cv2.VideoCapture(0)
while(True):
ret,frame = cap.read()
faces = face_cascade.detectMultiScale(frame,1.3,2)
img = frame
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
face_area = img[y:y+h,x:x+w]
eyes = eye_cascade.detectMultiScale(face_area,1.3,10)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
smiles = smile_cascade.detectMultiScale(face_area,scaleFactor = 1.16,minNeighbors=65,minSize=(25,25),flags = cv2.CASCADE_SCALE_IMAGE)
for (ex,ey,ew,eh) in smiles:
cv2.rectangle(face_area,(ex,ey),(ew+ex,ey+eh),(0,0,255),1)
cv2.putText(img,'Smile',(x,y-7),3,1.2,(0,0,255),2,cv2.LINE_AA)
cv2.imshow("frame2",img)
if cv2.waitKey(5) & 0xff == ord('q'):
break
cap.release()
cv2.destoryAllWindows()
视频
调用摄像头:
#
import cv2
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("无法打开摄像头")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("无法打开摄像头")
break
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow("frame_window",gray)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
本地视频
# VedioCapv1.py
import cv2
cap = cv2.VideoCapture("test.mp4")
if not cap.isOpened():
print("无法打开视频")
exit()
print('WIDTH',cap.get(3))
print('HIGTH',cap.get(4))
while True:
ret, frame = cap.read()
if not ret:
print("无法打开摄像头")
break
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow("frame_window",gray)
if cv2.waitKey(25) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
保存处理后的视频
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
fourcc = cv.VideoWriter_fourcc(*'DIVX')
# Define the codec and create VideoWriter object
# 指定输出文件,fourCC视频编码,FPS
out = cv.VideoWriter('output.avi',fourcc,20.0,(640,480))
while cap.isOpened():
ret,frame = cap.read()
if not ret:
print("无法打开摄像头")
break
frame = cv.flip(frame,0) ## 翻转视频
out.write(frame)
cv.imshow('frame',frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
out.release()# 若注释掉,没有预览图片
cv.destoryAllWindows()