Home > Net >  Detect differences in two image with variable threshold
Detect differences in two image with variable threshold

Time:03-23

I am working on a python program to detect if there is something missing in a picture as compared to another picture. Basically, it should take pictures of a place, save it, and after that whenever we run the program, it should take pictures from the camera of the same place and compare them to the picture we saved earlier to detect if an object is missing in the picture.

Requirement:

For example, we take a reference photo of the floor with two apples and one orange laying on the ground, then save that picture for future reference. After that, we take one apple from the floor and now there is only one apple and one orange on the floor. When we run the program, it should take a photo using a camera and compare that photo to the one we took earlier. It should detect that a certain area of the picture (where the missing apple was placed) is different in the new picture and alert the user (print a message)...This is the whole idea.

Here is what I achieved so far:

I used a tool called ImageChops from the Pillow library. The problem with it is that it detects even very small changes that can occur due to noise. But I want it to detect major changes like an object missing or added to the picture. I would also like to control it with a threshold. I searched for doing this over the internet but couldn't find anything useful. Here is the code that I used

from PIL import Image, ImageChops
import cv2
import os
import sys

img1 = Image.open('1.jpg')
img2 = Image.open('2.jpg')

diff = ImageChops.difference(img1, img2)
if diff.getbbox():
    print("Total {} differences found".format(diff.getbbox()))
    diff.show()
else:
    print('No difference found')

I would really appreciate it if someone can point me in the right direction. Thanks

CodePudding user response:

The general problem you're describing is called "feature matching". OpenCV has lots of tooling for this exact purpose: a simple approach is this one https://docs.opencv.org/4.x/dc/dc3/tutorial_py_matcher.html, which compares ORB features (basically, generalized corners) between two images. These kinds of features are more robust to lighting and noise than just comparing pixel values directly.

  • Related