Detect and display difference between 2 frames with OpenCV Python

Viewed 471

I'm attempting to take 2 different frames from my screen and make it obvious where the differences are. I want to add colour to the two images such that a user can clearly spot all the differences within a second or two.

Here's what I'm using as a reference

First frame to compare

Second frame to compare

My current solution is to take a capture of the screen within my bounding box, wait half a second and then take another capture and display the difference which should work in theory, but obviously my code is incorrect. I've attempted debugging it and it is driving me up the walls, wouldn't be surprised if it's a simple fix.

My current code:

import cv2
import time
import numpy as np
from mss import mss
from PIL import Image

sct = mss()

bounding_box = {'top': 374, 'left': 601, 'width': 717, 'height': 369}

img1 = sct_img = sct.grab(bounding_box)
time.sleep(0.1)
img2 = sct_img = sct.grab(bounding_box)
diff = cv2.absdiff(img1, img2)
mask = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)

th = 1
imask =  mask>th

canvas = np.zeros_like(img2, np.uint8)
canvas[imask] = img2[imask]

cv2.imshow('screen', np.array(canvas))

My other problem is due to the nature of my samples, it may need a third or fourth name for reference to exclude false differences due to recurring pixels within each frame.

Here is what my earlier prototype result looked like when my code was working:

Here

which as you can see displays my issue with having boxes with the same number or letter as there should be a maximum of 2 boxes not highlighted as there are only 2 constant numbers in my real-time samples.

0 Answers
Related