视频:1.OpenCV图像读取与显示
import cv2
img=cv2.imread("img/bike.jpg")
cv2.imshow("img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
视频:1.2 OpenCV实现图像保存
import cv2
img=cv2.imread("img/bike.jpg")
cv2.imshow("img",img)
cv2.imwrite("test.jpg",img) #保存图像
cv2.waitKey(0)
cv2.destroyAllWindows()
视频:1.3 OpenCV打开本地摄像头并显示视频画面
import cv2
cap=cv2.VideoCapture(0)
while cap.isOpened():
ret,frame=cap.read()
cv2.imshow("video",frame)
if cv2.waitKey(1)==ord("q"):
break
cap.release()
cv2.destroyAllWindows()
视频:1.4 OpenCV摄像头采集图像数据
import cv2
cap=cv2.VideoCapture(0)
k=0
while cap.isOpened():
ret,frame=cap.read()
cv2.imshow("video",frame)
k=k 1
if k<=100:
cv2.imwrite("cap/" str(k) ".jpg",frame)
if cv2.waitKey(1)==ord("q"):
break
cap.release()
cv2.destroyAllWindows()
视频:1.5 OpenCV录制视频
import cv2
cap=cv2.VideoCapture(0)
fourcc=cv2.VideoWriter_fourcc(*"mp4v")
out=cv2.VideoWriter("out.mp4",fourcc,20,(640,480))
while cap.isOpened():
ret,frame=cap.read()
cv2.imshow("video",frame)
out.write(frame)
if cv2.waitKey(1)==ord("q"):
break
cap.release()
cv2.destroyAllWindows()
视频:1.6 OpenCV实现人脸检测
import cv2
face_detector=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
img=cv2.imread("img/t5.jpg")
faces=face_detector.detectMultiScale(frame,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x w,y h),(255,0,0),1)
cv2.imshow("result",frame)
if cv2.waitKey(10)==ord("q") :
break
cap.release()
cv2.destroyAllWindows()
视频:1.7 OpenCV实时人脸检测
import cv2
face_detector=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
#img=cv2.imread("img/t5.jpg")
#cv2.imshow("img",img)
cap=cv2.VideoCapture(0)
k=0
while cap.isOpened():
ret,frame=cap.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=face_detector.detectMultiScale(gray,1.3,5)
#print(faces)
k=k 1
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x w,y h),(255,0,0),1)
faceimg=frame[y:y w,x:x w] #人脸数据
# if k ==0:
# cv2.imwrite("cap/" str(k) ".jpg",faceimg)
cv2.imshow("result",frame)
if cv2.waitKey(10)==ord("q") or k==2000:
break
cap.release()
cv2.destroyAllWindows()