Python-OpenCV 处理图像(六):对象识别
生活随笔
收集整理的這篇文章主要介紹了
Python-OpenCV 处理图像(六):对象识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
0x00. 特征識別
這里主要用到兩個函數:
GoodFeaturesToTrack 和 extractSURF
-
GoodFeaturesToTrack: 在圖像中尋找具有大特征值的角點。
-
SURF算法: 是一個穩健的圖像識別和描述算法。
總之這倆個我目前也不清楚能用來干嘛,以后用到了在更新吧。
import cv2.cv as cv import mathim = cv.LoadImage("img/church.png", cv.CV_LOAD_IMAGE_GRAYSCALE) im2 = cv.CloneImage(im)# Goodfeatureto track algorithm eigImage = cv.CreateMat(im.height, im.width, cv.IPL_DEPTH_32F) tempImage = cv.CloneMat(eigImage) cornerCount = 500 quality = 0.01 minDistance = 10corners = cv.GoodFeaturesToTrack(im, eigImage, tempImage, cornerCount, quality, minDistance)radius = 3 thickness = 2for (x,y) in corners:cv.Circle(im, (int(x),int(y)), radius, (255,255,255), thickness)cv.ShowImage("GoodfeaturesToTrack", im)#SURF algorithm hessthresh = 1500 # 400 500 dsize = 0 # 1 layers = 1 # 3 10keypoints, descriptors = cv.ExtractSURF(im2, None, cv.CreateMemStorage(), (dsize, hessthresh, 3, layers)) for ((x, y), laplacian, size, dir, hessian) in keypoints:cv.Circle(im2, (int(x),int(y)), cv.Round(size/2), (255,255,255), 1)x2 = x+((size/2)*math.cos(dir))y2 = y+((size/2)*math.sin(dir))cv.Line(im2, (int(x),int(y)), (int(x2),int(y2)), (255,255,255), 1)cv.ShowImage("SURF ", im2)cv.WaitKey(0)0x01. 人臉識別
可以使用 OpenCV 訓練好的級聯分類器來識別圖像中的人臉,當然還有很多其他的分類器:例如表情識別,鼻子等,具體可在這里下載:
OpenCV分類器
具體使用代碼:
#import library - MUST use cv2 if using opencv_traincascade import cv2# rectangle color and stroke color = (0,0,255) # reverse of RGB (B,G,R) - weird strokeWeight = 1 # thickness of outline# set window name windowName = "Object Detection"# load an image to search for faces img = cv2.imread("mao.jpg")# load detection file (various files for different views and uses) cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")# preprocessing, as suggested by: http://www.bytefish.de/wiki/opencv/object_detection # img_copy = cv2.resize(img, (img.shape[1]/2, img.shape[0]/2)) # gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY) # gray = cv2.equalizeHist(gray)# detect objects, return as list rects = cascade.detectMultiScale(img)# display until escape key is hit while True:# get a list of rectanglesfor x,y, width,height in rects:cv2.rectangle(img, (x,y), (x+width, y+height), color, strokeWeight)# display!cv2.imshow(windowName, img)# escape key (ASCII 27) closes windowif cv2.waitKey(20) == 27:break# if esc key is hit, quit! exit()效果:
總結
以上是生活随笔為你收集整理的Python-OpenCV 处理图像(六):对象识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python-OpenCV 处理视频(五
- 下一篇: Python-OpenCV 处理图像(七