We help IT Professionals succeed at work.

Motion detection

yanivba asked
I have a school assignment.
I have a series of jpeg images, and I need to detect motion by the deference between each two consecutive images.
Can some one help me with java source code or pseudo code, becuase I haven't got a clue of how to compare to jpegs.
thenks in advance
Watch Question

Maybe you can compute the "difference" between 2 images by comparing the RGB values : pixels whose RGB values are almost the same on the 2 images belong to the background, other one belong to an eventually moving object.
Here is a point to start ...

hope it helps :-)

My first suggestion is that you have to get rid of the natural "noise" in the image (if it is the image created by a camera or something similar).

I would do that by grouping, 'say, areas 8x8 pixels, and calculating average RGB for them. The result is an image 8 times smaller than the original, but more stable (change of single pixel doesn't influence the average value that much).
Also, by doing this your performance gain will be great.

Then, for each scanline you can calculate separate sum of Red values of pixels in the scanline, sum of Green and sum of Blue values. (You would do the same for the vertical ones, but I will tell only for horizontal ones).


When you compare R, G and B sum arrays for two subsequent images, you can choose to ignore those scanlines where _not all three_ sums are changing drastically.


For those scanlines where you have changes, you can try to do some clustering, by further ignoring isolated lines which change (isolated == line has no changing neighbours or only has few of them)

As a final result you can form an boolean array which only has information if the scanline was altered together with its neighbours.


If you do the same with vertical scanlines, in the intersection you would find the moving object.


It is easy to say all this, but you still have to test if this idea gives you satisfying results. Maybe grouping 8x8 would not be enough or would be too much, depending on content of images which you will get as input (try using 32x32 or 5x5 maybe) or some other of my suggestions need tuning, but that's where I would start.

This is the simplest thing I could think of, bc I tried not to use any advanced math, but more a statistical approach instead.

Good luck!



Neutron, thanks I thuoght of such algorithm (a little different but close).
I need to use the jpeg properties for that - to compare the DCT coefficients - but I don't know how to take them out of the jpeg, I don't know how to comapre them.
So I am sorry if my question wasn't claer enough.
I know I shouls somehow substract one frame from the other. I know that substruction of the DCT's of the frames is equivalent to substruction of the frame before the DCT (liniar). but I don't know how to implement it in java - as I said I havn't got a clue of how to do it.
Well, in standard Java packages, as far as I know, you don't have anything which would help you to intercept the step before applying IDCT to reconstruct image.

What you need is a decoder, so you should look for some opensource jpeg project.
I didn't have enough time do a thorough for such project, but after a very short search on a search engine, I've found this one:

Author's remark says that decoder is incomplete, but a brief look in the source tells me that dequantization and IDCT are implemented, so you can get a quick start on obtaining IDCT input by examining these sources.

The other way (with some waste of time) is to load jpeg using Toolkit.getImage, use PixelGrabber to get pixels and finally do "blocking" and apply FDCT, but it's a double work.

I'm interested at what will you do, so please post some info here about the progress.

Best wishes,
Please update and finalize this question.

Click the link below to track and manage all your open and locked questions here please.

Moondancer - EE Moderator

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for Neutron

Please leave any comments here within the next seven days.
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.