在可移动相机中篡改相机

问题描述

我正在从事一个摄像机篡改项目。我有用于静态摄像机篡改检测的代码。篡改意味着遮挡或散焦相机。我想针对移动摄像机或旋转摄像机对其进行修改,以使其从背景中获取所有帧,然后使用相同的背景减法方法将其与新帧进行比较。

我已经尝试过,但是无法弄清楚如何使用帧列表来比较捕获的帧和其他帧。

import numpy as np
import cv2
from playsound import playsound
import time

#cap = cv2.VideoCapture('http://192.168.43.1:8080/video')  #opening of IP camera just enter the ip address
cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractormog2()    #generating a foreground mask

frame_list = [] #creating the list of frame
##################################################################
while(True):
    ret,frame = cap.read()
    if ret == True:
        if frame not in frame_list:
            frame_list.append(frame)  #This list contain all the frames
###################################################################    
#ret,frame = cap.read()        #to get the initial frame 
#fgmask = fgbg.apply(frame)     #to save the initial frame
kernel = np.ones((5,5),np.uint8)  #creating a matrix of (5,5) consisting of 1
while(True):
    ret,frame = cap.read()    #reading all the frames
    if ret == True:
        
        a = 0
        bounding_rect = []  # An empty list where will furthur input the contours
        
        fgmask = fgbg.apply(frame) #Applying the changes of the backgroud to the foreground mask
        fgmask= cv2.erode(fgmask,kernel,iterations=5) 
        fgmask = cv2.dilate(fgmask,iterations = 5)    #Erosion and dilation is done to detect even the blur objects better
        
        cv2.imshow('frame',frame)  #Showing the frame.
        contours,_ = cv2.findContours(fgmask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #The mode RETR_TREE together with the CHAIN APPROX_SIMPLE returns only the endpoints required to draw contour
        
        for i in range(0,len(contours)):
            bounding_rect.append(cv2.boundingRect(contours[i]))  #cv2.bounding rectangle gives the coordinates of bounding rectangle and then we will input these coordinates to the list we made
        for i in range(0,len(contours)):
            if bounding_rect[i][2] >=40 or bounding_rect[i][3] >= 40: #setting the threshold for the width and height if the contour 
                a = a+(bounding_rect[i][2])*bounding_rect[i][3]     #updating the area of contour
            if(a >=int(frame.shape[0])*int(frame.shape[1])/3):    #It is basically the comparison of the change in area of the background,so if there is a certain change in area it will detect the tampering
                cv2.putText(frame,"TAMPERING DETECTED",(5,30),cv2.FONT_HERShey_SIMPLEX,1,(0,255,255),2)   
                #playsound('warning.mp3') #put the address of the warning tune in the playsound object and uncomment it 
            cv2.imshow('frame',frame)    #showing the final frame     
               
        if cv2.waitKey(30) & 0xff== ord('q'): #To close the camera press q
            break
        
    else : break
    
cap.release()
cv2.destroyAllWindows()

解决方法

我认为背景减去并不是解决您问题的好方法,您的方法主要用于通过减去背景来检测运动物体。此方法最常用于静态相机,但对于移动相机,可能还需要考虑强度变化或纹理变化。