Motion detection using Java
Hi, I am new to OpenCV and as a first target application, i have decided to get motion detection working.
I have an another application which keep on capturing images from a fixed source in half second and stores it on file system. So I am passing two images to the opencv application and it will process them to detect motion between two frames.
I am performing the following steps to detect motion:-
- Subtract Image1 from Image2 using Core.subtract(Image1, Image2, diffFrame);
- The diffFrame[x][y] will be have a zero value if Image1[x][y] - Image2[x][y] = 0, i.e. no difference at this pixel.
- Then I calculate the number of nonZeroValue of diffFrame using Core.countNonZero(diffFrame);
Then I calculate percent of changedPixels i.e. nonZeroValue(diffFrame) by applying this formula
float motionPercent = (nonZeroValue(diffFrame)/nonZeroValue(Image1)) X 100;
And If this motionPercent is more than some certain value, i consider it as motion.
But the problem with this way of solving is that it fails if object is moving too slow as the diff in not more than my threshold values.
Is there any better way of doing this?
Regards,
Naval Gandhi