OpenCV-Python中的图像处理-视频分析
- 视频分析
- Meanshift算法
- Camshift算法
- 光流
- Lucas-Kanade Optical Flow
- Dense Optical Flow
视频分析
学习使用 Meanshift 和 Camshift 算法在视频中找到并跟踪目标对象:
Meanshift算法
Meanshift 算法的基本原理是和很简单的。假设我们有一堆点(比如直方
图反向投影得到的点),和一个小的圆形窗口,我们要完成的任务就是将这个窗
口移动到最大灰度密度处(或者是点最多的地方)。如下图所示:
初始窗口是蓝色的“C1”,它的圆心为蓝色方框“C1_o”,而窗口中所有点质心却是“C1_r”(小的蓝色圆圈),很明显圆心和点的质心没有重合。所以移动圆心 C1_o 到质心 C1_r,这样我们就得到了一个新的窗口。这时又可以找到新窗口内所有点的质心,大多数情况下还是不重合的,所以重复上面的操作:将新窗口的中心移动到新的质心。就这样不停的迭代操作直到窗口的中心和其所包含点的质心重合为止(或者有一点小误差)。按照这样的操作我们的窗口最终会落在像素值(和)最大的地方。如上图所示“C2”是窗口的最后位址,我们可以看出来这个窗口中的像素点最多。
要在 OpenCV 中使用 Meanshift 算法首先我们要对目标对象进行设置,
计算目标对象的直方图,这样在执行 meanshift 算法时我们就可以将目标对
象反向投影到每一帧中去了。另外我们还需要提供窗口的起始位置。在这里我
们值计算 H( Hue)通道的直方图,同样为了避免低亮度造成的影响,我们使
用函数 cv2.inRange() 将低亮度的值忽略掉。
import numpy as np
import cv2
from matplotlib import pyplot as plt
# 视频下载地址https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4
cap = cv2.VideoCapture('./resource/opencv/video/slow_traffic_small.mp4')ret,frame = cap.read()# setup initial location of window
x, y, w, h = 300, 200, 100, 50 # simply hardcoded the values
track_window = (x, y, w, h)# set up the ROI for tracking
roi = frame[y:y+h, x:x+w]hsv_roi = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)while(1):ret, frame = cap.read()if ret == True:hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)ret, track_window = cv2.meanShift(dst, track_window, term_crit)x,y,w,h = track_windowimg2 = cv2.rectangle(frame, (x,y), (x+w, y+h), 255, 2)k = cv2.waitKey(60)&0xFFif k == 27:breakelse:cv2.imshow('img', img2)else:breakcap.release()
cv2.destroyAllWindows()
Camshift算法
与 Meanshift 基本一样,但是返回的结果是一个带旋转角度的矩形以及这个矩形的参数(被用到下一次迭代过程中)。
import numpy as np
import cv2
from matplotlib import pyplot as plt
# 视频下载地址https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4
cap = cv2.VideoCapture('./resource/opencv/video/slow_traffic_small.mp4')# take first frame of the video
ret, frame = cap.read()# setup initial location of window
x, y, w, h = 300, 200, 100, 50 # simply hardcoded the values
track_window = (x, y, w, h)
# set up the ROI for tracking
roi = frame[y:y+h, x:x+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
# Setup the termination criteria, either 10 iteration or move by at least 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )while(1):ret, frame = cap.read()if ret == True:hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)# apply camshift to get the new locationret, track_window = cv2.CamShift(dst, track_window, term_crit)# Draw it on imagepts = cv2.boxPoints(ret)pts = np.int0(pts)img2 = cv2.polylines(frame,[pts],True, 255,2)k = cv2.waitKey(30) & 0xffif k == 27:breakelse:cv2.imshow('img2',img2)else:cap.release()cv2.destroyAllWindows()
光流
Lucas-Kanade Optical Flow
- 光流的概念以及 Lucas-Kanade 光流法
- 函数 cv2.calcOpticalFlowPyrLK() 对图像中的特征点进行跟踪
import numpy as np
import cv2cap = cv2.VideoCapture('./resource/opencv/video/slow_traffic_small.mp4')# params for Shi-Tomasi corner detection
feature_params = dict(maxCorners = 100,qualityLevel = 0.3,minDistance = 7,blockSize = 7)# parameters for lucas kanade optical flow
# maxLevel 为使用的图像金字塔层数
lk_params = dict(winSize = (15,15),maxLevel = 2,criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Create some random colors
color = np.random.randint(0, 255, (100, 3))# Take first frame and find corners in it
ret, old_frame = cap.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params)# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)while(1):ret, frame = cap.read()frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# calculate optical flow 能够获取点的新位置p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)# Select good pointsgood_new = p1[st==1]good_old = p0[st==1]# draw the tracksfor i,(new, old) in enumerate(zip(good_new, good_old)):a,b = new.ravel()c,d = old.ravel()mask = cv2.line(mask, (int(a), int(b)), (int(c), int(d)), color[i].tolist(), 2)frame = cv2.circle(frame, (int(a), int(b)), 5, color[i].tolist(), -1)img = cv2.add(frame, mask)cv2.imshow('frame', img)k = cv2.waitKey(30) & 0xFFif k == 27:breakold_gray = frame_gray.copy()p0 = good_new.reshape(-1, 1, 2)cv2.destroyAllWindows()
cap.release()
Dense Optical Flow
import numpy as np
import cv2 as cvcap = cv.VideoCapture('./resource/opencv/video/vtest.avi')
ret, frame1 = cap.read()
prvs = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[..., 1] = 255
while(1):ret, frame2 = cap.read()if not ret:print('No frames grabbed!')breaknext = cv.cvtColor(frame2, cv.COLOR_BGR2GRAY)flow = cv.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1])hsv[..., 0] = ang*180/np.pi/2hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)cv.imshow('frame2', bgr)k = cv.waitKey(30) & 0xffif k == 27:breakelif k == ord('s'):cv.imwrite('./resource/opencv/video/opticalfb.png', frame2)cv.imwrite('./resource/opencv/video/opticalhsv.png', bgr)prvs = next
cv.destroyAllWindows()