Jake B.
Jake B.

Reputation: 465

Comparing images with OpenCV

I'm trying to monitor a LCD reading using OpenCV (and picamera). I'm taking a reading about every second and I wish that the image is the same for at least three frames (3 seconds). Just to avoid scenarios where I would analyse the image when there is a light switch or something is interrupting the image.

So I read three images in a row and compute the difference between them by printing (img0, img1, img2 are three grayscaled consecutive images):

> print(max(diff(img0,img1), diff(img1,img2), diff(img0, img2)))

where

> def diff(image1, image2):
>     return abs(image1-image2).mean()

I've also used absdiff function with similar results. But it's strange that when there is no image change (visible to me), the values are around 120-160. But when I turn off the lights, the value drops to 110 and then rises to 220. And when I stick a finger to the frame, the value can still be in the 120-160 area. I can't figure out a real pattern that would make a lot of sense. Perhaps the problem is that there is shaking and a lot of per-pixel differences that add up. Is there a smarter way to do this? I'm attaching some sample pictures. frame 1

frame 2

frame 3

Upvotes: 0

Views: 367

Answers (1)

A Kruger
A Kruger

Reputation: 2419

This issue is likely caused because you're subtracting images with type uint8. Any negative numbers will wrap up to be high values. For example,

> np.array([-3,-2,-1,0], dtype=np.uint8)
[253 254 255 0]

So any pixels where image2 is greater than image1 in the diff function will register has high values. You can first change them to type float. Using your first two sample images:

> print(diff(img0, img1))
209.93
> img0 = img0.astype(float)
> img1 = img1.astype(float)
> print(diff(img0, img1))
3.85

Upvotes: 2

Related Questions