OpenCV入门教程(含人脸检测与常用图像处理示例等)
在OpenCV中混合图像


# Two images
img1 = cv2.imread('target.jpg')
img2 = cv2.imread('filter.png')
# OpenCV expects to get BGR images, so we will convert from BGR to RGB
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
# Resize the Images. In order to blend them, the two images
# must be of the same shape
img1 =cv2.resize(img1,(620,350))
img2 =cv2.resize(img2,(620,350))
# Now, we can blend them, we need to define the weight (alpha) of the target image
# as well as the weight of the filter image
# in our case we choose 80% target and 20% filter
blended = cv2.addWeighted(src1=img1,alpha=0.8,src2=img2,beta=0.2,gamma=0)
# finally we can save the image. Now we need to convert it from RGB to BGR
cv2.imwrite('Blending.png',cv2.cvtColor(blended, cv2.COLOR_RGB2BGR))

在OpenCV中处理图像

如何模糊影像
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
img = cv2.imread('panda.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
blurred_img = cv2.blur(img,ksize=(20,20))
cv2.imwrite('blurredpanda.jpg', blurred_img)

如何申请Sobel Operator
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
cv2.imwrite('sobelx_panda.jpg', sobelx)
cv2.imwrite('sobely_panda.jpg', sobely)


如何对图像应用阈值
ret,th1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
fig = plt.figure(figsize=(12,10))
plt.imshow(th1,cmap='gray')

OpenCV中的人脸检测
numpy array
,然后转换为灰度。然后通过应用适当的**CascadeClassifier,**我们获得了人脸的边界框。最后,使用PIllow(甚至是OpenCV),我们可以在初始图像上绘制框。import numpy as np
import PIL
from PIL import Image
import requests
from io import BytesIO
from PIL import ImageDraw
# I have commented out the cat and eye cascade. Notice that the xml files are in the opencv folder that you have downloaded and installed
# so it is good a idea to write the whole path
face_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
#cat_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalcatface.xml')
#eye_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_eye.xml')
URL = 'https://images.unsplash.com/photo-1525267219888-bb077b8792cc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80'
response = requests.get(URL)
img = Image.open(BytesIO(response.content))
img_initial = img.copy()
# convert it to np array
img = np.asarray(img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
# And lets just print those faces out to the screen
#print(faces)
drawing=ImageDraw.Draw(img_initial)
# For each item in faces, lets surround it with a red box
for x,y,w,h in faces:
# That might be new syntax for you! Recall that faces is a list of rectangles in (x,y,w,h)
# format, that is, a list of lists. Instead of having to do an iteration and then manually
# pull out each item, we can use tuple unpacking to pull out individual items in the sublist
# directly to variables. A really nice python feature
#
# Now we just need to draw our box
drawing.rectangle((x,y,x+w,y+h), outline='red')
display(img_initial)


裁剪脸部以分离图像
img_initial.crop((x,y,x+w,y+h))
display(img_initial.crop((x,y,x+w,y+h)))

initial_img=Image.open('my_image.jpg')
img = cv.imread('my_image.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
OpenCV中的人脸检测视频
如何录制人脸检测视频
.mp4
文件。# change your path to the one where the haarcascades/haarcascade_frontalface_default.xml is
face_cascade = cv2.CascadeClassifier('../DATA/haarcascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# MACOS AND LINUX: *'XVID' (MacOS users may want to try VIDX as well just in case)
# WINDOWS *'VIDX'
writer = cv2.VideoWriter('myface.mp4', cv2.VideoWriter_fourcc(*'XVID'),25, (width, height))
while True:
ret, frame = cap.read(0)
frame = detect_face(frame)
writer.write(frame)
cv2.imshow('Video Face Detection', frame)
# escape button to close it
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
writer.release()
cv2.destroyAllWindows()
计算机视觉代码的输出
https:// vimeo.com/364588657
赞 (0)