移动侦测和动测或报警区别,监控移动侦测报警什么意思
1.帧差法原理移动侦测即是根据视频每帧或者几帧之间像素的差异,对差异值设置阈值,筛选大于阈值的像素点,做掩模图即可选出视频中存在变化的桢。
帧差法
较为简单的视频中物体移动侦测
,帧差法分为:单帧差
、两桢差
、和三桢差
。随着帧数的增加是防止检测结果的重影。2.算法思路
文章以截取视频为例进行单帧差法移动侦测
3.python实现代码def threh(video,save_video,thres1,area_threh): cam=cv2 .视频捕捉(视频)#打开一个视频input_fps=cam.get(cv2 .CAP_PROP_FPS) ret_val,input _ image=cam。read()index=[]images=[]images。追加(input _ image)video _ length=int(cam。获取(cv2。CAP _ PROP _ FRAME _ COUNT))input _ image=cv2。resize(input _ image,(512,512))ending _ FRAME=video _ length fourcc=cv2 .video writer _ fourcc(* XVID )out=cv2 .VideoWriter(save_video,fourcc,input_fps,(512,512))gray _ lwpCV=cv2。CVT颜色(输入图像,cv2 .COLOR_BGR2GRAY) gray_lwpCV=cv2 .GaussianBlur(gray_lwpCV,(21,21),0)background=gray _ lwpCV # es=cv2。getstructuringelement(cv2 .MORPH_ELLIPSE,(9,4)) i=0 #默认为0 outt=[]while(cam。iso pened())和ret _ val==True和I 2999:# #如果I % 2==1:ret _ val,input _ image=cam。read()输入_图像=cv2。resize(input _ image,(512,512))gray _ lwpCV=cv2。CVT颜色(input _ image,cv2。COLOR_BGR2GRAY) gray_lwpCV=cv2 .GaussianBlur(gray_lwpCV,(21,21),0) diff=cv2.absdiff(background,gray_lwpCV) outt.append(diff) #跟着图像变换背景tem _ diff=diff。flatten()tem _ ds=PD .series(tem _ diff)tem _ per=1-len(tem _ ds[tem _ ds==0])/len(tem _ ds)if(tem _ per 0.2)(tem _ per 0.75):background=gray _ lwpCV else:diff=cv2。阈值(diff,thres1,255,cv2 .THRESH_BINARY)[1] ret,thresh=cv2.threshold(diff.copy(),150,255,0)轮廓,层次=cv2。寻找轮廓(阈值,cv2 .RETR _外部,cv2 .CHAIN_APPROX_SIMPLE) # contours,hierarchy=cv2。查找轮廓(差异。复制(),cv2 .RETR _外部,cv2 .CHAIN _ APPROX _ SIMPLE)对于轮廓中的c:if(cv2。轮廓面积(c)area _ threh)(cv2。轮廓面积(c)int(512 * 512 * 0.3)):#对于矩形区域,只显示大于给定阈值的轮廓(去除微小的变化等噪点)continue (x,y,w,h)=cv2.boundingRect(c) #该函数计算矩形的边界框cv2.rectangle(input_image,(x,y),(x w,y h),(0,255,0),2)index。追加(I)# cv2。im show( contours ,input_image) # cv2.imshow(dis ,diff)out。写入(input _ image)图像。append(input _ image)I=I 1 out。释放()凸轮。release()返回outt,index,images ` `# #调取函数outt=threh(new_video.mp4 , test6.mp4 ,253000)