sach0508
Posts: 3
Joined: Wed Jan 27, 2016 8:39 am

Comparing two images taken from Pi camera

Wed Jan 27, 2016 8:46 am

Hi,

I am doing a Project based on Image processing. And I am in need of comparing 2 images taken from Pi camera.

I am using OpenCV on Python. Can anybody suggest me the way to go.

I am expecting a matrix form of image data and matrix comparison.

What are the libraries I need to add?

drgeoff
Posts: 9251
Joined: Wed Jan 25, 2012 6:39 pm

Re: Comparing two images taken from Pi camera

Wed Jan 27, 2016 3:38 pm

"comparing" is not a precise enough explanation of what you want to do.

Can you express the comparison operation mathematically, or at least describe in words what you want to do?

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

Re: Comparing two images taken from Pi camera

Wed Jan 27, 2016 3:42 pm

You need to explain what you are trying to do or at least what sort of comparison you need.
To places to look could be:
OpenCV is often used for image analysis.
Motion is a video surveillance program that detects motion in video by detecting differences between images (frames).
You can also investigate Numpy and Pillow python libraries.
Mathematica might be useful.
Processing might also be worth a look.

gordon77
Posts: 3895
Joined: Sun Aug 05, 2012 3:12 pm

Re: Comparing two images taken from Pi camera

Wed Jan 27, 2016 4:15 pm

I agree you need to define what you want to compare but here's a script to get you started..

It compares 2 images by converting to grey, then comparing the difference in value for each pixel between frames against a threshold value, then summing the number of changed pixels exceeding threshold, using numpy, and if that total exceeds a trigger value set decides there's movement.

Code: Select all

import cv2
import numpy as np
import os

print "Press Ctrl * C to EXIT"

if os.path.exists('/dev/video0') == False:
  path = 'sudo modprobe bcm2835-v4l2 max_video_width=2592 max_video_height=1944'
  os.system (path)
  time.sleep(1)

width = 640
height = 480
cam = cv2.VideoCapture(0)
cam.set(3,width)
cam.set(4,height)
thres = 20 # set difference between pixel values between frames
trigger = (height * width) / 10   # 10% of pixels

winName = "Picture"
winName3 = "Differences"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
cv2.namedWindow(winName3, cv2.CV_WINDOW_AUTOSIZE)
s = np.zeros((height,width,3), np.uint8)
t0 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
 
while True:
    
  s = cam.read()[1]
  cv2.imshow(winName,s)
  t = cv2.cvtColor(s,cv2.COLOR_RGB2GRAY)
  c = cv2.absdiff(t, t0)
  c[c < thres] = 0
  c[c >= thres] = 200
  cv2.imshow( winName3,c)
  total = np.sum(c)/200
  if total > trigger:
    print "Movement"
  t0 = t
  key = cv2.waitKey(10)

sach0508
Posts: 3
Joined: Wed Jan 27, 2016 8:39 am

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 4:27 am

By comparison, I meant that if there is any difference in pixel values, I infer there is a movement.

gordon77
Posts: 3895
Joined: Sun Aug 05, 2012 3:12 pm

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 8:42 am

My example does that, so hopefully a place to start for you.

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

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 9:28 am

sach0508 wrote:By comparison, I meant that if there is any difference in pixel values, I infer there is a movement.
Just be aware that there will be differences in pixels when there is no movement. What you want to detect is "movement". One approach to doing that is simple pixel subtraction and that may be good enough for you. OTOH you may need to measure size or velocity or screen out some areas or use temporal filtering or other techniques so that you reliably detect only the sort of motion you want to detect.

Motion does pixel differencing and works quite well in many cases. Well worth playing around with, even if you want to roll your own.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 22064
Joined: Sat Jul 30, 2011 7:41 pm

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 9:53 am

DIvide the image up in to macroblocks, lets say 64x64 blocks per image. Work out some sort of difference value for each block. (RMS, PSNR etc), then find out which blocks have major movement. Two next to each other may imply something moved from one point to another.

Or you could take a H264 style coarse motion estimation approach, look for areas of similarity, see if similar areas 'move' around the image.

Lots of things to play with! All take loads of CPU.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

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

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 9:59 am

jamesh wrote:DIvide the image up in to macroblocks, lets say 64x64 blocks per image. Work out some sort of difference value for each block. (RMS, PSNR etc), then find out which blocks have major movement. Two next to each other may imply something moved from one point to another.

Or you could take a H264 style coarse motion estimation approach, look for areas of similarity, see if similar areas 'move' around the image.

Lots of things to play with! All take loads of CPU.
Or use the H264 motion vectors and cluster them to find objects in motion.

The clever bit is to get a detection method that spots people (or whatever your object of choice may be) in motion but isn't triggered by the sun coming out or a tree swaying in the breeze.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 5:40 pm

sach0508 wrote:By comparison, I meant that if there is any difference in pixel values, I infer there is a movement.
You will quickly find out that in the real world there are leaves that shift slightly even in a modest wind, and worse that the sun moves across the sky and clouds interfere. These will all change the values of pixels.

This is why, perhaps, you should start out by reducing the complexity of the picture to edges. But the question then becomes, in addition to everything else you've got to worry about, how to deal with leaf areas that might have something move in front of them.

You could subtract the edges from the leaves but you'll have to deal with those edges moving. Whether you first remove edges that exist all the time and then end up with new edges that are your target remaining, or if you just leave them in and somehow find the target edge and remove that, I don't know. Maybe you do both choosing the method depending on the type of background in each part of the picture. Trying to do one thing to an entire picture and get the best results might be hard.

gordon77
Posts: 3895
Joined: Sun Aug 05, 2012 3:12 pm

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 5:43 pm

Or you ignore, mask out, areas of the screen you are not interested in or contain regular movement.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 5:55 pm

gordon77 wrote:Or you ignore, mask out, areas of the screen you are not interested in or contain regular movement.
It's great if those leaves are way up in a tree that can't be a person without him wearing his Johnny Rocket Pack, which doesn't exist, but what if the leaves are right behind where people might walk? So you have to ignore the regular movement while still paying attention to anything new in the difficult region.

gordon77
Posts: 3895
Joined: Sun Aug 05, 2012 3:12 pm

Re: Comparing two images taken from Pi camera

Fri Jan 29, 2016 6:05 pm

stderr wrote:
gordon77 wrote:Or you ignore, mask out, areas of the screen you are not interested in or contain regular movement.
It's great if those leaves are way up in a tree that can't be a person without him wearing his Johnny Rocket Pack, which doesn't exist, but what if the leaves are right behind where people might walk? So you have to ignore the regular movement while still paying attention to anything new in the difficult region.
A mixture of both techniques maybe.

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

Re: Comparing two images taken from Pi camera

Sat Jan 30, 2016 10:48 am

stderr wrote:
gordon77 wrote:Or you ignore, mask out, areas of the screen you are not interested in or contain regular movement.
It's great if those leaves are way up in a tree that can't be a person without him wearing his Johnny Rocket Pack, which doesn't exist, but what if the leaves are right behind where people might walk? So you have to ignore the regular movement while still paying attention to anything new in the difficult region.
One is to track motion over many frames. Trees don't move about. But it does get complex. I don't know of a stable open source project that does that, but there may be some.
You can use object detection / recognition to filter basic motion results. Face detector or person detector for example,This can get very complex and slow to process.

Here is an example of a people detector:
http://www.quizover.com/oer/course/late ... eolectures

Generally you accept that you will get false alarms and / or may not detect every person in view. Siting your cameras to point at people and not foliage is the best way to start.

sach0508
Posts: 3
Joined: Wed Jan 27, 2016 8:39 am

Re: Comparing two images taken from Pi camera

Tue Feb 02, 2016 11:21 am

Thank guys for the replies.It helped. I appreciate your interest. Will post again if I have any more doubts.

Narmadha
Posts: 1
Joined: Sun Mar 24, 2019 8:42 am

Re: Comparing two images taken from Pi camera

Sun Mar 24, 2019 8:45 am

I m using comparison algorithm for finding the difference between two images. Which camera will suitable for comparison...

gordon77
Posts: 3895
Joined: Sun Aug 05, 2012 3:12 pm

Re: Comparing two images taken from Pi camera

Sun Mar 24, 2019 12:49 pm

Any that will work with the Pi, including the pi camera and some USB cameras.
May depend on what you are comparing and what resolution required.

The latest Pi camera is v2, and does 8MP. If considering night images you may want to look at the pi noir.

BwLinus
Posts: 5
Joined: Sat Apr 13, 2019 10:32 pm

Re: Comparing two images taken from Pi camera

Fri Apr 19, 2019 8:22 pm

hello,
Is it possible to compare a single image taken by the Pi camera with several images stored in a certain folder?
help

gordon77
Posts: 3895
Joined: Sun Aug 05, 2012 3:12 pm

Re: Comparing two images taken from Pi camera

Sat Apr 20, 2019 7:52 am

Yes. You could look at the code l posted earlier in this thread and you could load an image to compare against instead of comparing to the last image.
Also take a look here.. viewtopic.php?t=221245&start=50 where it will recognise objects.

BwLinus
Posts: 5
Joined: Sat Apr 13, 2019 10:32 pm

Re: Comparing two images taken from Pi camera

Sun Apr 21, 2019 7:13 am

Thank you.
Are there any libraries that need to be installed apart from OpenCV.?
Which is the easiest way of installing OpenCV?

gordon77
Posts: 3895
Joined: Sun Aug 05, 2012 3:12 pm

Re: Comparing two images taken from Pi camera

Sun Apr 21, 2019 8:36 am

See the method l use here...

viewtopic.php?t=238118

Return to “Advanced users”