上一篇:图像处理入门(一):linux(ubuntu)配置Openface+测试
一、需要了解的基础知识:
(1)使用Opencv对图片进行读写和显示操作:
1、使用cv2.cv的LoadImage、ShowImage和SaveImage函数:
1. import cv2.cv as cv 2. 3. # 读图片 4. image=cv.LoadImage('img/image.png', cv.CV_LOAD_IMAGE_COLOR)#Load the image 5. #Or just: image=cv.LoadImage('img/image.png') 6. 7. cv.NamedWindow('a_window', cv.CV_WINDOW_AUTOSIZE) #Facultative 8. cv.ShowImage('a_window', image) #Show the image 9. 10. # 写图片 11. cv.SaveImage("thumb.png", thumb) 12. cv.WaitKey(0) #Wait for user input and quit
2、也可以直接使用cv2的imread、imwrite和imshow函数
1. import numpy as np 2. import cv2 3. 4. img = cv2.imread('messi5.jpg',0) 5. cv2.imshow('image',img) 6. k = cv2.waitKey(0) 7. if k == 27: # wait for ESC key to exit 8. cv2.destroyAllWindows() 9. elif k == ord('s'): # wait for 's' key to save and exit 10. cv2.imwrite('messigray.png',img) 11. cv2.destroyAllWindows() 12. imread函数还可以定义加载的mode,默认是以RGB模式处理图片: 13. 14. import cv2 15. grayImage = cv2.imread('MyPic.png', cv2.CV_LOAD_IMAGE_GRAYSCALE) 16. # 可选参数CV_LOAD_IMAGE_COLOR (BGR), CV_LOAD_IMAGE_GRAYSCALE (grayscale), CV_LOAD_IMAGE_UNCHANGED(neither) 17. cv2.imwrite('MyPicGray.png', grayImage)
(2)图像仿射:(引用:https://blog.csdn.net/on2way/article/details/46801063)
图像的旋转加上拉升就是图像仿射变换,仿射变化也是需要一个M矩阵就可以,但是由于仿射变换比较复杂,一般直接找很难找到这个矩阵,opencv提供了根据变换前后三个点的对应关系来自动求解M。这个函数是
M=cv2.getAffineTransform(pos1,pos2),其中两个位置就是变换前后的对应位置关系。输 出的就是仿射矩阵M。然后在使用函数cv2.warpAffine()。形象化的图如下(引用参考的)
一个例子比如:
1. import cv2 2. import numpy as np 3. import matplotlib.pyplot as plt 4. img = cv2.imread('flower.jpg') 5. pts1 = np.float32([[50,50],[200,50],[50,200]]) 6. rows,cols = img.shape[:2] 7. M = cv2.getAffineTransform(pts1,pts2) 8. pts2 = np.float32([[10,100],[200,50],[100,250]]) 9. #第三个参数:变换后的图像大小 10. plt.subplot(122) 11. res = cv2.warpAffine(img,M,(rows,cols)) 12. plt.subplot(121) plt.imshow(img) 13. plt.imshow(res)
二、人脸检测和处理:
(一)、识别最大的人脸边界框:
(1)、使用人脸检测器:dlib自带的frontal_face_detector特征提取器:
使用方法:
1. detector=dlib.get_frontal_face_detector() 2. faces = detector(Img, 1) 3. 或者: 4. dets = dlib.get_frontal_face_detector(img)
具体的原理:
【dlib代码解读】人脸检测器的训练【转】
(二)、人脸对齐:
(1)、利用dlib的特征预测器(和dlib的shape_predictor_68_face_landmarks.dat(68点特征预测器)),进行人脸 68点特征提取:
使用方法:
1. predictor = dlib.shape_predictor("../models/dlib/shape_predictor_68_face_landmarks.dat")#此处填的是路径 2. shape = predictor(img, dets[0])
三、代码和效果:
1. import dlib 2. 3. import cv2 4. 5. 6. def GetFace(imgpath='1.jpg'): 7. #读取图片 8. Img=cv2.imread(imgpath) 9. if Img is None: 10. raise Exception("Unable to load image: {}".format(imgpath)) 11. # cv2.imshow('face image',Img); 12. # cv2.waitKey() 13. detector = dlib.get_frontal_face_detector() 14. facebox = detector(Img, 1) 15. face=facebox[0]#注意facebox是一个array 16. predictor = dlib.shape_predictor("/home/angelo/openface/demos/../models/dlib/shape_predictor_68_face_landmarks.dat") # 此处填的是路径 17. shape = predictor(Img, face) 18. # 生成dlib的图像窗口 19. win = dlib.image_window() 20. win.clear_overlay() 21. win.set_image(Img) 22. # 绘制面部轮廓 23. win.add_overlay(shape) 24. # 绘制矩阵轮廓 25. win.add_overlay(facebox) 26. # 保持图像 27. dlib.hit_enter_to_continue() 28. 29. if __name__=="__main__": 30. GetFace('1.jpg')
图像的仿射:
1. def GetFace(imgpath='1.jpg'): 2. #读取图片 3. Img=cv2.imread(imgpath) 4. if Img is None: 5. raise Exception("Unable to load image: {}".format(imgpath)) 6. # cv2.imshow('face image',Img); 7. # cv2.waitKey() 8. detector = dlib.get_frontal_face_detector() 9. facebox = detector(Img, 1) 10. face=facebox[0]#注意facebox是一个array 11. predictor = dlib.shape_predictor("/home/angelo/openface/demos/../models/dlib/shape_predictor_68_face_landmarks.dat") # 此处填的是路径 12. shape = predictor(Img, face) 13. # 生成dlib的图像窗口 14. win = dlib.image_window() 15. win.clear_overlay() 16. win.set_image(Img) 17. # 绘制面部轮廓 18. win.add_overlay(shape) 19. # 绘制矩阵轮廓 20. win.add_overlay(facebox) 21. # 保持图像 22. # dlib.hit_enter_to_continue() 23. 24. #图像仿射 25. landmarkIndices = [39, 42, 57] 26. npLandmarks = np.float32(list(map(lambda p: (p.x, p.y), shape.parts()))) 27. npLandmarkIndices = np.array(landmarkIndices) 28. 29. TEMPLATE = np.float32([ 30. (0.0792396913815, 0.339223741112), (0.0829219487236, 0.456955367943), 31. (0.0967927109165, 0.575648016728), (0.122141515615, 0.691921601066), 32. (0.168687863544, 0.800341263616), (0.239789390707, 0.895732504778), 33. (0.325662452515, 0.977068762493), (0.422318282013, 1.04329000149), 34. (0.531777802068, 1.06080371126), (0.641296298053, 1.03981924107), 35. (0.738105872266, 0.972268833998), (0.824444363295, 0.889624082279), 36. (0.894792677532, 0.792494155836), (0.939395486253, 0.681546643421), 37. (0.96111933829, 0.562238253072), (0.970579841181, 0.441758925744), 38. (0.971193274221, 0.322118743967), (0.163846223133, 0.249151738053), 39. (0.21780354657, 0.204255863861), (0.291299351124, 0.192367318323), 40. (0.367460241458, 0.203582210627), (0.4392945113, 0.233135599851), 41. (0.586445962425, 0.228141644834), (0.660152671635, 0.195923841854), 42. (0.737466449096, 0.182360984545), (0.813236546239, 0.192828009114), 43. (0.8707571886, 0.235293377042), (0.51534533827, 0.31863546193), 44. (0.516221448289, 0.396200446263), (0.517118861835, 0.473797687758), 45. (0.51816430343, 0.553157797772), (0.433701156035, 0.604054457668), 46. (0.475501237769, 0.62076344024), (0.520712933176, 0.634268222208), 47. (0.565874114041, 0.618796581487), (0.607054002672, 0.60157671656), 48. (0.252418718401, 0.331052263829), (0.298663015648, 0.302646354002), 49. (0.355749724218, 0.303020650651), (0.403718978315, 0.33867711083), 50. (0.352507175597, 0.349987615384), (0.296791759886, 0.350478978225), 51. (0.631326076346, 0.334136672344), (0.679073381078, 0.29645404267), 52. (0.73597236153, 0.294721285802), (0.782865376271, 0.321305281656), 53. (0.740312274764, 0.341849376713), (0.68499850091, 0.343734332172), 54. (0.353167761422, 0.746189164237), (0.414587777921, 0.719053835073), 55. (0.477677654595, 0.706835892494), (0.522732900812, 0.717092275768), 56. (0.569832064287, 0.705414478982), (0.635195811927, 0.71565572516), 57. (0.69951672331, 0.739419187253), (0.639447159575, 0.805236879972), 58. (0.576410514055, 0.835436670169), (0.525398405766, 0.841706377792), 59. (0.47641545769, 0.837505914975), (0.41379548902, 0.810045601727), 60. (0.380084785646, 0.749979603086), (0.477955996282, 0.74513234612), 61. (0.523389793327, 0.748924302636), (0.571057789237, 0.74332894691), 62. (0.672409137852, 0.744177032192), (0.572539621444, 0.776609286626), 63. (0.5240106503, 0.783370783245), (0.477561227414, 0.778476346951)]) 64. 65. TPL_MIN, TPL_MAX = np.min(TEMPLATE, axis=0), np.max(TEMPLATE, axis=0) 66. MINMAX_TEMPLATE = (TEMPLATE - TPL_MIN) / (TPL_MAX - TPL_MIN) 67. H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices], 68. 96 * MINMAX_TEMPLATE[npLandmarkIndices]) # 其中两个位置就是变换前后的对应位置关系。输 出的就是仿射矩阵M 69. thumbnail = cv2.warpAffine(Img, H, (96, 96)) # 仿射函数cv2.warpAffine()接受三个参数,需要变换的原始图像,移动矩阵M 70. # 以及变换的图像大小(这个大小如果不和原始图像大小相同,那么函数会自 动通过插值来调整像素间的关系) 71. 72. cv2.imshow('image', thumbnail); 73. cv2.waitKey();
效果:96x96:
下一篇:图像处理(三):在Windows系统里配置dlib环境并做图像批量处理
AIEarth是一个由众多领域内专家博主共同打造的学术平台,旨在建设一个拥抱智慧未来的学术殿堂!【平台地址:https://devpress.csdn.net/aiearth】 很高兴认识你!加入我们共同进步!