万州房地产网站建设,画册设计说明怎么写,网站排名优化化,wordpress一键分享代码实验环境#xff1a;Python3.6OpenCV3.4pycharm2019代码实现#xff1a;首先是一个简单的不用kalman滤波的运动目标追踪代码这里可以根据需要进行摄像头运动目标识别#xff0c;只要把camera cv2.VideoCapture(./video/yellow_ball.mp4)# 改成camera cv2.VideoCapture(0) …实验环境Python3.6OpenCV3.4pycharm2019代码实现首先是一个简单的不用kalman滤波的运动目标追踪代码这里可以根据需要进行摄像头运动目标识别只要把camera cv2.VideoCapture(./video/yellow_ball.mp4)# 改成camera cv2.VideoCapture(0) 就是摄像头内识别完整代码from collections import dequeimport numpy as npimport cv2# imutilsimport time#设定阈值HSV空间redLower np.array([11, 100, 100])redUpper np.array([20, 255, 255])#初始化追踪点的列表mybuffer 64pts deque(maxlenmybuffer)#打开摄像头camera cv2.VideoCapture(./video/yellow_ball.mp4)fourcc cv2.VideoWriter_fourcc(*XVID) # 保存文件为avi格式fps camera.get(cv2.CAP_PROP_FPS)size (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)), int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))) # 输出视频尺寸长和宽out cv2.VideoWriter(new_yellow_ball.avi, fourcc, fps, size) # 输出视频#等待两秒time.sleep(2)#遍历每一帧检测红色瓶盖while True:#读取帧(ret, frame) camera.read()if ret False:break#判断是否成功打开摄像头# if not ret:# print(No Camera)# break#frame imutils.resize(frame, width600)#转到HSV空间hsv cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)#根据阈值构建掩膜mask cv2.inRange(hsv, redLower, redUpper)#腐蚀操作mask cv2.erode(mask, None, iterations2)#膨胀操作其实先腐蚀再膨胀的效果是开运算去除噪点mask cv2.dilate(mask, None, iterations2)#轮廓检测cnts cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]#初始化瓶盖圆形轮廓质心center None#如果存在轮廓if len(cnts) 0:#找到面积最大的轮廓c max(cnts, key cv2.contourArea)#确定面积最大的轮廓的外接圆((x, y), radius) cv2.minEnclosingCircle(c)#计算轮廓的矩M cv2.moments(c)#计算质心center (int(M[m10]/M[m00]), int(M[m01]/M[m00]))#只有当半径大于10时才执行画图if radius 10:cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)cv2.circle(frame, center, 5, (0, 0, 255), -1)#把质心添加到pts中并且是添加到列表左侧pts.appendleft(center)#遍历追踪点分段画出轨迹for i in range(1, len(pts)):if pts[i - 1] is None or pts[i] is None:continue#计算所画小线段的粗细thickness int(np.sqrt(mybuffer / float(i 1)) * 2.5)#画出小线段cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)#res cv2.bitwise_and(frame, frame, maskmask)cv2.imshow(Frame, frame)out.write(frame)#键盘检测检测到esc键退出k cv2.waitKey(5)0xFFif k 27:break#摄像头释放camera.release()#销毁所有窗口cv2.destroyAllWindows()这里是基于kalman滤波的运动目标识别与轨迹绘制import cv2import numpy as np# hsv阈值便于进行轮廓判断及轨迹绘制需要根据运动目标的颜色自己进行调整min_hsv_bound (35, 100, 100)max_hsv_bound (77, 255, 255)#状态向量stateSize 6#观测向量measSize 4coutrSize 0kf cv2.KalmanFilter(stateSize,measSize,coutrSize)state np.zeros(stateSize, np.float32)#[x,y,v_x,v_y,w,h],簇心位置速度高宽meas np.zeros(measSize, np.float32)#[z_x,z_y,z_w,z_h]procNoise np.zeros(stateSize, np.float32)#状态转移矩阵cv2.setIdentity(kf.transitionMatrix)#生成单位矩阵# [1 0 dT 0 0 0]# [0 1 0 dT 0 0]# [0 0 1 0 0 0]# [0 0 0 1 0 0]# [0 0 0 0 1 0]# [0 0 0 0 0 1]#观测矩阵# [1 0 0 0 0 0]# [0 1 0 0 0 0]# [0 0 0 0 1 0]# [0 0 0 0 0 1]kf.measurementMatrix np.zeros((measSize,stateSize),np.float32)kf.measurementMatrix[0,0]1.0kf.measurementMatrix[1,1]1.0kf.measurementMatrix[2,4]1.0kf.measurementMatrix[3,5]1.0#预测噪声# [Ex 0 0 0 0 0]# [0 Ey 0 0 0 0]# [0 0 Ev_x 0 0 0]# [0 0 0 Ev_y 0 0]# [0 0 0 0 Ew 0]# [0 0 0 0 0 Eh]cv2.setIdentity(kf.processNoiseCov)kf.processNoiseCov[0,0] 1e-2kf.processNoiseCov[1,1] 1e-2kf.processNoiseCov[2,2] 5.0kf.processNoiseCov[3,3] 5.0kf.processNoiseCov[4,4] 1e-2kf.processNoiseCov[5,5] 1e-2#测量噪声cv2.setIdentity(kf.measurementNoiseCov)# for i in range(len(kf.measurementNoiseCov)):# kf.measurementNoiseCov[i,i] 1e-1video_cap cv2.VideoCapture(./video/green_ball.mp4)# 视频输出fps video_cap.get(cv2.CAP_PROP_FPS) #获得视频帧率即每秒多少帧size (int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))videoWriter cv2.VideoWriter(./video/new_green.mp4 ,cv2.VideoWriter_fourcc(m, p, 4, v), fps, size)ticks 0i0found FalsenotFoundCount 0prePointCen [] #存储小球中心点位置meaPointCen []while(True):ret, frame video_cap.read()if ret is False:breakcv2.imshow(frame,frame)cv2.waitKey(1)precTick ticksticks float(cv2.getTickCount())res frame.copy()# dT float(1/fps)dT float((ticks - precTick)/cv2.getTickFrequency())if(found):#预测得到的小球位置kf.transitionMatrix[0,2] dTkf.transitionMatrix[1,3] dTstate kf.predict()width state[4]height state[5]x_left state[0] - width/2 #左上角横坐标y_left state[1] - height/2 #左上角纵坐标x_right state[0] width/2y_right state[1] height/2center_x state[0]center_y state[1]prePointCen.append((int(center_x),int(center_y)))cv2.circle(res, (int(center_x),int(center_y)),2,(255,0,0),-1)cv2.rectangle(res,(x_left,y_left),(x_right,y_right),(255,0,0),2)#根据颜色二值化得到的小球位置frame cv2.GaussianBlur(frame, (5,5), 3.0, 3.0)frame cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)rangeRes cv2.inRange(frame, min_hsv_bound,max_hsv_bound)kernel np.ones((3, 3), np.uint8)# 腐蚀膨胀rangeRes cv2.erode(rangeRes, kernel, iterations2)rangeRes cv2.dilate(rangeRes, kernel, iterations2)# cv2.imshow(Threshold, rangeRes)cv2.waitKey(1)contours cv2.findContours(rangeRes.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[-2]#检测轮廓只检测最外围轮廓保存物体边界上所有连续的轮廓点到contours向量内balls []ballsBox []for i in range(len(contours)):x, y, w, h cv2.boundingRect(np.array(contours[i]))ratio float(w/h)if(ratio 1.0):ratio 1.0 / ratioif(ratio 0.75 and w*h400):balls.append(contours[i])ballsBox.append([x, y, w, h])print( Balls found:, len(ballsBox))print(\n)for i in range(len(balls)):# 绘制小球轮廓cv2.drawContours(res, balls, i, (20,150,20),1)cv2.rectangle(res,(ballsBox[i][0],ballsBox[i][1]),(ballsBox[i][0]ballsBox[i][2],ballsBox[i][1]ballsBox[i][3]),(0,255,0),2) #二值化得到小球边界center_x ballsBox[i][0] ballsBox[i][2] / 2center_y ballsBox[i][1] ballsBox[i][3] / 2meaPointCen.append((int(center_x),int(center_y)))cv2.circle(res,(int(center_x),int(center_y)), 2, (20,150,20) ,-1)name ( str(center_x) , str(center_y) )cv2.putText(res, name, (int(center_x) 3, int(center_y) - 3), cv2.FONT_HERSHEY_COMPLEX, 0.5, (20,150,20), 2)n len(prePointCen)for i in range(1, n):print(i)if prePointCen[i-1] is None or prePointCen[i] is None:continue# 注释掉的这块是为了绘制能够随时间先后慢慢消失的追踪轨迹但是有一些小错误# 计算所画小线段的粗细# thickness int(np.sqrt(64 / float(n - i 1))*2.5)# print(thickness)# 画出小线段# cv2.line(res, prePointCen[i-1], prePointCen[i], (0, 0, 255), thickness)cv2.line(res, prePointCen[i-1], prePointCen[i], (0,0,255), 1, 4)if(len(balls) 0):notFoundCount 1print(notFoundCount,notFoundCount)print(\n)if notFoundCount 100:found Falseelse:#测量得到的物体位置notFoundCount 0meas[0] ballsBox[0][0] ballsBox[0][2] / 2meas[1] ballsBox[0][1] ballsBox[0][3] / 2meas[2] float(ballsBox[0][2])meas[3] float(ballsBox[0][3])#第一次检测if not found:for i in range(len(kf.errorCovPre)):kf.errorCovPre[i,i] 1state[0] meas[0]state[1] meas[1]state[2] 0state[3] 0state[4] meas[2]state[5] meas[3]kf.statePost statefound Trueelse:kf.correct(meas) #Kalman修正print(rr,res.shape)print(Measure matrix:, meas)cv2.imshow(Tracking, res)cv2.waitKey(1)videoWriter.write(res)大概就是这样了如果追踪效果不好就自己调整一下阈值范围吧