import cv2 as cv
import copy
import numpy as np#添加模块和矩阵模块
# 脸
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load('E://opencv/build/etc/haarcascades/haarcascade_frontalface_default.xml')
# 眼睛
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
eye_cascade.load('E://opencv/build/etc/haarcascades/haarcascade_eye.xml')
# 嘴巴
mouth_cascade = cv.CascadeClassifier('haarcascade_mcs_mouth.xml')
mouth_cascade.load('E://opencv/build/etc/haarcascades/haarcascade_mcs_mouth.xml')
# 鼻子
nose_cascade = cv.CascadeClassifier('haarcascade_mcs_nose.xml')
nose_cascade.load('E://opencv/build/etc/haarcascades/haarcascade_mcs_nose.xml')
# 耳朵
leftear_cascade = cv.CascadeClassifier('haarcascade_mcs_leftear.xml')
leftear_cascade.load('E://opencv/build/etc/haarcascades/haarcascade_mcs_leftear.xml')
rightear_cascade = cv.CascadeClassifier('haarcascade_mcs_rightear.xml')
rightear_cascade.load('E://opencv/build/etc/haarcascades/haarcascade_mcs_rightear.xml')
def edge_demo(image):
blurred = cv.GaussianBlur(image, (3, 3), 0)
gray = cv.cvtColor(blurred, cv.COLOR_RGB2GRAY)
# xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0) #x方向梯度
# ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1) #y方向梯度
# edge_output = cv.Canny(xgrad, ygrad, 50, 150)
edge_output = cv.Canny(gray, 50, 150)
cv.imshow("Canny Edge", edge_output)
dst = cv.bitwise_and(image, image, mask= edge_output)
cv.imshow("Color Edge", dst)
def edge_demo1(image):
# 先将图像高斯模糊去噪
blurred = cv.GaussianBlur(image, (3, 3), 0)
# 图像灰度化
gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
# 图像边缘提取
edge_output = cv.Canny(gray, 50, 150)
cv.imshow("CannyEdge", edge_output)
# 彩色边线
# dst = cv.bitwise_and(image,gray,mask=edge_output)
# cv.imshow("Color Edge",dst)
return edge_output
def edge_demo2(image):
# 先将图像高斯模糊去噪
blurred = cv.GaussianBlur(image, (3, 3), 0)
# 图像灰度化
gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
# 图像边缘提取
edge_output = cv.Canny(gray, 50, 150)
# 彩色边线
# dst = cv.bitwise_and(image,gray,mask=edge_output)
# cv.imshow("Color Edge",dst)
return edge_output
#画轮廓
def contours_demo(image):
"""
dst = cv.GaussianBlur(image,(3,3),0) #用高斯模糊去噪
gray = cv.cvtColor(dst,cv.COLOR_BGR2GRAY)
#图像二值化---返回阈值和二值图像
ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
cv.imshow("binary_image",binary)
"""
image1=copy.copy(image)
binary = edge_demo1(image1)
contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
cv.drawContours(image1, contours, i, (255, 255, 255), -2)
print(i)
cv.imshow("detect contours", image1)
cap=cv.VideoCapture(3)#开摄像头参数为第几个摄像头
while(1): # get a frame
ret, frame = cap.read() # show a frame
frame1=copy.copy(frame)
imgsmall=copy.copy(frame)
#人脸识别
gray = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY)
# 脸
faces = face_cascade.detectMultiScale(gray, 1.2, 3)
for (x, y, w, h) in faces:
img = cv.rectangle(frame1, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
# 眼睛
eyes = eye_cascade.detectMultiScale(roi_gray, 1.2, 3)
for (ex, ey, ew, eh) in eyes:
cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
# 嘴巴
mouth = mouth_cascade.detectMultiScale(roi_gray, 1.5, 5)
for (mx, my, mw, mh) in mouth:
cv.rectangle(roi_color, (mx, my), (mx + mw, my + mh), (0, 0, 255), 2)
# 鼻子
nose = nose_cascade.detectMultiScale(roi_gray, 1.2, 5)
for (nx, ny, nw, nh) in nose:
cv.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 255), 2)
# 耳朵
leftear = leftear_cascade.detectMultiScale(roi_gray, 1.01, 2)
for (lx, ly, lw, lh) in leftear:
cv.rectangle(roi_color, (lx, ly), (lx + lw, ly + lh), (0, 0, 0), 2)
rightear = rightear_cascade.detectMultiScale(roi_gray, 1.01, 2)
for (rx, ry, rw, rh) in rightear:
cv.rectangle(roi_color, (rx, ry), (rx + rw, ry + rh), (0, 0, 0), 2)
cv.imshow('img', img)
contours_demo(frame)#原图画轮廓
#cv.namedWindow('input_image', cv.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
#cv.imshow('input_image', frame)
edge_demo(frame)#灰度边缘
cv.imshow("capture", frame)
#cv.imshow("small",imgsmall)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
#释放并销毁窗口