I have a couple of CCTV cameras taking snapshots every hour and saving them into a folder with the date and time. The scene (Field of view) of each camera doesn't change. I want to detect if the camera was shifted up/bottom or left/right from the previous view by comparing two snapshots.
this is similar to what I am trying to do: Python: Detect camera shift angle comparing the reference image and live image taken from a camera
But the solution is not clear, and the post is old.
How Can I apply the optical flow to compare two images and detect the shift of the scene without detecting the object's movement inside?
this is what I tried so far
# optical flow between two frames
def optical_flow(old_frame, frame):
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
mask_features = np.zeros_like(old_gray)
mask_features[:,0:20] = 1
mask_features[:,620:640] = 1
# params for ShiTomasi corner detection
# maxCorners is the number of pixels from the corner
feature_params = dict( maxCorners = 800,
qualityLevel = 0.3,
minDistance = 3,
blockSize = 7,
mask = mask_features)
# Parameters for lucas kanade optical flow
lk_params = dict( winSize = (15,15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
corners = init_new_features(old_gray, feature_params)
while True:
try:
cam_moved = False
# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, corners, None, **lk_params)
good_new = p1[st==1]
good_old = corners[st==1]
# draw the tracks
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
distance = calculateDistance(a,b,c,d)
#print('Distance: ',int(distance))
if distance > 5.0:
cam_moved = True
# update the previous frame and previous points
old_gray = frame_gray.copy()
corners = init_new_features(old_gray, feature_params)
else:
old_gray = frame_gray.copy()
corners = good_new.reshape(-1,1,2)
#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)
return cam_moved
except TypeError as e:
print(e)
break
Thank you for any advice or help you can provide.