一、图像处理函数
? ? ? ? 1.将图像转化为灰度图-用convert('L')
from PIL import Image
import matplotlib.pyplot as plt
im=Image.open('IMG_8793.JPG').convert('L')
print(im.format,im.size,im.mode)
#im.show()
plt.imshow(im)
plt.title('Image Processing@wyb')
plt.axis('off')
plt.show()
? ? ? ? 2.调节图像的模糊程度
from PIL import Image
from numpy import *
from scipy.ndimage import filters
import matplotlib.pyplot as plt
im=array(Image.open('IMG_8793.JPG').convert('L'))
im2=filters.gaussian_filter(im,20) #第二个参数调整模糊程度
plt.figure()
plt.imshow(im)
plt.figure()
plt.imshow(im2)
plt.show()
? ? ? ? 3.sobel算子
from PIL import Image
from numpy import *
from scipy.ndimage import filters
import matplotlib.pyplot as plt
im=array(Image.open('IMG_8793.JPG').convert('L'))
imx=zeros(im.shape)
filters.sobel(im,1,imx)
imy=zeros(im.shape)
filters.sobel(im,0,imy)
magnitude=sqrt(imx**2+imy**2)
plt.figure()
plt.imshow(imx)
plt.figure()
plt.imshow(imy)
plt.figure()
plt.imshow(magnitude)
plt.show()
?
? ? ? ? ?4.图片截取、旋转、粘贴
from PIL import Image
import matplotlib.pyplot as plt
image=Image.open('IMG_8793.JPG')
print(image)
box=(350,400,800,800)
image_crop=image.crop(box)
image_crop=image_crop.transpose(Image.ROTATE_180)
image.paste(image_crop1,box)
plt.imshow(image)
plt.axis('off')
plt.show()
?
二、ffmpeg命令
? ? ? ? 1.ffmpeg安装
? ? ? ? 1)下载brew文件,在终端进入下载的brew文件所在的位置?
? ? ? ? ? ? cd /Users/chenxinning/Downloads
? ? ? ? 2)在终端输入?
ruby brew_install.rb
? ? ? 3)在终端输入
brew install ffmpeg
? ? ?
? ? ? ? 2.ffmpeg实现视频格式转化
ffmpeg -i /Users/chenxinning/Desktop/ghz.mp4 -b:v 640k ghz.flv
? ? ?
三、图片处理和人脸识别——将图片中的人脸高斯模糊
???????
?
用dib进行人脸识别并标出人脸坐标
import cv2
import dlib
import numpy as np
import matplotlib.pyplot as plt
path="girl.png"
img=cv2.imread(path)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
detector=dlib.get_frontal_face_detector()
predictor=dlib.shape_predictor('/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/face_recognition_models/models/shape_predictor_68_face_landmarks.dat')
# 人脸数rects
rects = detector(img, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(path, rects[i]).parts()])
for idx, point in enumerate(landmarks):
# 81点的坐标
pos = (point[0, 0], point[0, 1])
# 利用cv2.circle给每个特征点画一个圈,共81个
cv2.circle(path, pos, 2, color=(0, 255, 0))
# 利用cv2.putText输出1-81
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(path, str(idx + 1), pos, font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)
print("index=" + str(idx+1) + " x=" + str(pos[0]) + " y=" + str(pos[1]))
plt.imshow(path)
plt.show()
if cv2.waitKey(1) & 0xFF == ord('q'):
print("q pressed")
cap.release()
cv2.destroyAllWindows()
输出结果:
Invoked with: <_dlib_pybind11.shape_predictor object at 0x103fdd5f0>, 'girl.png', rectangle(228,228,377,377)
然后用PIL进行人脸高斯模糊
from PIL import Image, ImageFilter
class MyGaussianBlur(ImageFilter.Filter):
name = "GaussianBlur"
def __init__(self, radius=2, bounds=None):
self.radius = radius
self.bounds = bounds
def filter(self, image):
if self.bounds:
clips = image.crop(self.bounds).gaussian_blur(self.radius)
image.paste(clips, self.bounds)
return image
else:
return image.gaussian_blur(self.radius)
bounds = (228,228,377,377)
image = Image.open('girl.png')
image = image.filter(MyGaussianBlur(radius=29, bounds=bounds))
plt.imshow(image)# 绘制图像image
plt.axis('off')
plt.show()
?????????????????????
四、图像处理与人脸识别——换背景
import cv2
import numpy as np
import inspect
img=cv2.imread('cxn.jpg')
img_back=cv2.imread('back.jpg')
#日常缩放
rows,cols,channels = img_back.shape
img_back=cv2.resize(img_back,None,fx=0.7,fy=0.7)
#cv2.imshow('img_back',img_back)
rows,cols,channels = img.shape
img=cv2.resize(img,None,fx=0.4,fy=0.4)
#cv2.imshow('img',img)
rows,cols,channels = img.shape#rows,cols最后一定要是前景图片的,后面遍历图片需要用到
#转换hsv
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
#获取mask
lower_blue=np.array([78,43,46])
upper_blue=np.array([110,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
#cv2.imshow('Mask', mask)
#腐蚀膨胀
erode=cv2.erode(mask,None,iterations=1)
cv2.imshow('erode',erode)
dilate=cv2.dilate(erode,None,iterations=1)
#cv2.imshow('dilate',dilate)
#遍历替换
center=[50,50]#在新背景图片中的位置
for i in range(rows):
for j in range(cols):
if dilate[i,j]==0:#0代表黑色的点
img_back[center[0]+i,center[1]+j]=img[i,j]#此处替换颜色,为BGR通道
#cv2.imshow('res',img_back)
#cv2.waitKey(0)
cv2.imwrite('rec.png',img_back)
???????
|