CV基础入门
1、读取图片
import cv2 as cv
img=cv.imread('lena.jpg')
cv.imshow('read_img',img)
cv.waitKey(3000)
cv.destroyAllWindows()
2、灰度转化
import cv2 as cv
img=cv.imread('lena.jpg')
cv.imshow('BGR_img',img)
gray_img=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.imshow('gray_img',gray_img)
cv.imwrite('gray_lena.jpg',gray_img)
cv.waitKey(0)
cv.destroyAllWindows()
3、修改图片尺寸
pyimport cv2 as cv
img=cv.imread('lena.jpg')
cv.imshow('img',img)
print('原来图片的形状',img.shape)
resize_img=cv.resize(img,dsize=(600,560))
print('修改后图片的形状:',resize_img.shape)
cv.imshow('resize_img',resize_img)
while True:
if ord('q')==cv.waitKey(0):
break
cv.destroyAllWindows()
4、绘制选取框——矩形、圆
import cv2 as cv
img=cv.imread('lena.jpg')
x,y,w,h=100,100,100,100
cv.rectangle(img,(x,y,x+w,y+h),color=(0,255,255),thickness=3)
x,y,r=200,200,100
cv.circle(img,center=(x,y),radius=r,color=(0,0,255),thickness=2)
cv.imshow('rectangle_img',img)
cv.waitKey(0)
cv.destroyAllWindows()
5、检测图片中的人脸
import cv2 as cv
def face_detect_demo():
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detector = cv.CascadeClassifier(
'E:/code_set/face_demo/facecode/haarcascade_frontalface_default.xml')
faces = face_detector.detectMultiScale(gray)
for x,y,w,h in faces:
print(x,y,w,h)
cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
cv.circle(img,center=(x+w//2,y+h//2),radius=w//2,color=(0,255,0),thickness=2)
cv.imshow('result',img)
img=cv.imread('face3.jpg')
face_detect_demo()
cv.waitKey(0)
cv.destroyAllWindows()
6、检测视频中的人脸
pyimport cv2 as cv
def face_detect_demo(img):
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detector = cv.CascadeClassifier(
'E:/soft/opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
faces = face_detector.detectMultiScale(gray)
for x,y,w,h in faces:
cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
cv.circle(img,center=(x+w//2,y+h//2),radius=(w//2),color=(0,255,0),thickness=2)
cv.imshow('result',img)
cap = cv.VideoCapture('video.mp4')
while True:
flag,frame=cap.read()
print('flag:',flag,'frame.shape:',frame.shape)
if not flag:
break
face_detect_demo(frame)
if ord('q') == cv.waitKey(10):
break
cv.destroyAllWindows()
cap.release()
7、训练数据
import os
import cv2
import sys
from PIL import Image
import numpy as np
def getImageAndLabels(path):
facesSamples=[]
ids=[]
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
face_detector = cv2.CascadeClassifier(
'E:/code_set/face_demo/facecode/haarcascade_frontalface_default.xml')
for imagePath in imagePaths:
PIL_img=Image.open(imagePath).convert('L')
img_numpy=np.array(PIL_img,'uint8')
faces = face_detector.detectMultiScale(img_numpy)
id=int(os.path.split(imagePath)[1].split('.')[0])
for x,y,w,h in faces:
facesSamples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return facesSamples,ids
if __name__ == '__main__':
path='./data/jm/'
faces,ids = getImageAndLabels(path)
recognizer=cv2.face.LBPHFaceRecognizer_create()
recognizer.train(faces,np.array(ids))
recognizer.write('trainer/trainer.yml')
8、人脸识别
import cv2
import numpy as np
import os
recogizer=cv2.face.LBPHFaceRecognizer_create()
recogizer.read('trainer/trainer.yml')
img=cv2.imread('30.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face_detector = cv2.CascadeClassifier(
'E:/code_set/face_demo/facecode/haarcascade_frontalface_default.xml')
faces = face_detector.detectMultiScale(gray)
for x,y,w,h in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
id,confidence=recogizer.predict(gray[y:y+h,x:x+w])
print('标签id:',id,'置信评分:',confidence)
cv2.imshow('result',img)
cv2.waitKey(0)
cv2.destroyAllWindows()