candreshidalgo
Posts: 16
Joined: Mon Mar 27, 2017 5:05 pm

Problem comparing if two images are different ( imagechops)

Thu Feb 22, 2018 3:03 pm

I want to know if someone has used imagechops to compare if 2 images are exactly the same

I searched for information but couldn't solve my doubt

this is the code I have and it seems to work because when the images are the same the result in console is NONE
but in case the images are different I don't like modifying the code

Code: Select all

from PIL import Image
from PIL import ImageChops

im1 = Image.open("imagenupload.jpg")
im2 = Image.open("imagereference.jpg")

diff = ImageChops.difference(im2, im1).getbbox()

print diff
I would like the code to do the following

if ( images are same ) -> flag=1
else ( images are different) -> flag=0

I just want to know if two images are different I don't need to know how they differ, only if they are different.

I've been reading about opencv but I've just been learning python for a while now.

very thanks

PiGraham
Posts: 3578
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Problem comparing if two images are different ( imagechops)

Thu Feb 22, 2018 3:26 pm

Difference gives an image and you need a simple number so you need to find out if all the pixels in the difference image are zero value, which means every pixel is identical.

See PIL.ImageStat.Stat

Get the mean from the stats and compare it to some small value to test if the images are the same.

Note that a pixel difference can't tell if two images look similar. It just calculates the difference between each pixel. A tiny offset in position between the two images, or one being slightly brighter or darker than the other will result in the images being rated as different.

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Problem comparing if two images are different ( imagechops)

Fri Feb 23, 2018 11:56 am

Your code returns None when the images are the same, just use that to set your flag.
Only a slight change to your code.

Code: Select all

from PIL import Image
from PIL import ImageChops

im1 = Image.open("imagenupload.jpg")
im2 = Image.open("imagereference.jpg")

Flag = 1 if ImageChops.difference(im2, im1).getbbox() == None else 0

print Flag
Flag will equal 1 if the images are the same, 0 if it isn't.

PiGraham
Posts: 3578
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Problem comparing if two images are different ( imagechops)

Fri Feb 23, 2018 12:25 pm

PingoBags wrote:
Fri Feb 23, 2018 11:56 am
Your code returns None when the images are the same
Well spotted. getbbox returnes the minimum rectangle that contains all non-zero pixels in the image and if all pixels are zero, because the two images are exactly the same in every pixel, he bounding rectangle will be null.

candreshidalgo
Posts: 16
Joined: Mon Mar 27, 2017 5:05 pm

Re: Problem comparing if two images are different ( imagechops)

Tue Feb 27, 2018 12:22 am

thanks
I tried the code :
Flag = 1 if ImageChops.difference(im2, im1).getbbox() == None else 0
but got the following message on the console.
Image

I'm also worried that the change of light at sunset
will result in the images being rated as different

about
PIL.ImageStat.Stat
I didn't find much information


Maybe I'm thinking wrongly about the solution. What I need to do is to take photos
with the raspberry pi 3 and compare the photo with the previous one and if they are
not the same send them to google cloud vision and then...

as it is not a critical process and where this rpi3 does not have a good internet
connection so I need to compare the images every so often.

if anyone has any idea since all the code regarding google cloud vision is ready

very thanks

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Problem comparing if two images are different ( imagechops)

Tue Feb 27, 2018 1:06 am

The issue isn't what I posted, it's ImageChops.difference.
Images do not match error. Could it be the image extension?

Post up a little more of your code.
Are you looking to make motion detection?

PiGraham
Posts: 3578
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Problem comparing if two images are different ( imagechops)

Wed Feb 28, 2018 8:15 am

candreshidalgo wrote:
Tue Feb 27, 2018 12:22 am

Maybe I'm thinking wrongly about the solution. What I need to do is to take photos
with the raspberry pi 3 and compare the photo with the previous one and if they are
not the same send them to google cloud vision and then...
No two separately taken images will ever be the same in the ImageChops.difference sense. There will always be some pixels that differ.
So you probably need a way to detect significant differences in the presence of insignificant differences..

A good startingpoint might be the package "Motion" that should bein the repositories.
That compares successive images for significant changes and can save still images or video when changes are detected.

There are much smarter ways to do this sort of thing but itdoes getcomplex. Beyondlooking for pixels changing brightnss you could look for connected areas of pixels moving together, having the rough size and shape of a huma. You couldlook atpatterns of motion. You could use tensor flow AI to identify what's in the scene from shapes and motions. But that is all heavy stuff.

OpenCV is a great resource for this sort of thing but look for examples that do what you want. Computer vision is a really big subject.
You will find examples using OpenCV to detect faces in an image. Maybe that would help.

I suggest you start with Motion and see how well that detect changes that interest you. If that works for you write a script that posts the images saved by Motion off to the cloud for whatever processing you want to do there

candreshidalgo
Posts: 16
Joined: Mon Mar 27, 2017 5:05 pm

Re: Problem comparing if two images are different ( imagechops)

Mon Mar 05, 2018 3:20 am

Before investigating the motion package, I would like to know if someone could clarify the following for me

motion package is able to detect when an image changes on a TV screen. because that's what I need or
is only able to detect changes such as when an object or person moves in front of the camera.

the images I need to detect on screen sometimes change every 3 minutes at other times change every 15 minutes,
that is not a video are images.

I found a site called pyimagesearch.com where they talk about opencv but I'm just learning so I'm looking for it.
an easy solution for the moment to use google cloud vision.

thanks

candreshidalgo
Posts: 16
Joined: Mon Mar 27, 2017 5:05 pm

Re: Problem comparing if two images are different ( imagechops)

Tue Mar 06, 2018 1:10 pm

the following code is used to compare 2 images I share it in case someone needs a simple code to compare.

Code: Select all

import cv2
import numpy as np

image1 = cv2.imread("myimage1.png")
image2 = cv2.imread("myimage2.png")

difference = cv2.subtract(image1, image2)

result = not np.any(difference) #if difference is all zeros it will return False

if result is True:
    print "The images are the same"
else:
    cv2.imwrite("result.jpg", difference)
    print "the images are different"


also in the following link there is a simple but more technical way

https://www.pyimagesearch.com/2014/09/1 ... wo-images/

thanks

User avatar
paddyg
Posts: 2340
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Problem comparing if two images are different ( imagechops)

Tue Mar 06, 2018 3:10 pm

If you're not checking for any subtle differences then using cv2 is a bit of overkill. In fact numpy and PIL can do an awful lot of the straightforward stuff like

Code: Select all

from PIL import Image
import numpy as np

im1 = np.array(Image.open("myimage1.png"))
im2 = np.array(Image.open("myimage1.png"))
difference = im1 - im2
if np.all(difference == 0): # more readable than 'not np.any'
  print("The images are the same")
else:
  Image.fromarray(
      difference[:,:,:3].astype(np.uint8) # alpha will be generally zero
    ).save("result.jpg")
  print("the images are different")
And will almost certainly be quite a bit faster than cv

PS, even if you really want to use python2 for some reason (and I would strongly advise against it) it's a good idea to get into the habit of putting brackets after your prints.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

PiGraham
Posts: 3578
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Problem comparing if two images are different ( imagechops)

Tue Mar 06, 2018 6:21 pm

candreshidalgo wrote:
Mon Mar 05, 2018 3:20 am

the images I need to detect on screen sometimes change every 3 minutes at other times change every 15 minutes,
that is not a video are images.
Do you want a 'spot the difference' function?
Can the lighting change? Almost certainly it can.
It is possible to make a system detect that an object that has been present for some time has gone or that a new object has appeared, but that is significantly more complex than just subtracting pixels. That is the basic operation, but you will need appropriate time-based filtering, compensation for lighting changes and noise filters to avoid lots of false alarms and missed events.
Are you looking for periodic changes (happen every 3 minutes on the dot)?
If you describe what you are trying to do you might get more useful answers.

User avatar
paddyg
Posts: 2340
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Problem comparing if two images are different ( imagechops)

Tue Mar 06, 2018 7:14 pm

I stupidly just looked at the code on the last post and assumed it was for checking image files that had got renamed or such like, and eliminating duplicates. On reading more of the thread I see that it is to check images from the pi camera, for which this code is completely useless as every single image will be different!

However you can do something pretty effective by setting a threshold then counting how many pixels were outside that. Along the lines of

Code: Select all

im1 = np.array(Image.open("myimage1.png")).astype(np.int) # cast to ints
im2 = np.array(Image.open("myimage1.png")).astype(np.int)
difference = im1 - im2 # if you do this with uint8 arrays the negative values will wrap i.e. (0, 2, 2) - (1, 4, 2) => (255, 254, 0)
diff = np.where(np.abs(difference) > THRSHLD)[0] # np.where will return a tuple of 1D arrays
diff_count = len(diff) # react if more than N pixel_colours have changed enough
It's probably also worth a) reducing the image size and or b) converting to greyscale and or c) using a gradient i.e. edge detection. All of which are standard/easy with PIL or numpy.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”