# Revision history [back]

### 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:-

1. Subtract Image1 from Image2 using Core.subtract(Image1, Image2, diffFrame);
2. 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.
3. Then I calculate the number of nonZeroValue of diffFrame using Core.countNonZero(diffFrame);
4. Then I calculate percent of changedPixels i.e. nonZeroValue(diffFrame) by applying this formula

 float motionPercent = (nonZeroValue(diffFrame)/nonZeroValue(Image1)) X 100;

5. 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?

### 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:-

1. Subtract Image1 from Image2 using Core.subtract(Image1, Image2, diffFrame);
2. 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.
3. Then I calculate the number of nonZeroValue of diffFrame using Core.countNonZero(diffFrame);
4. Then I calculate percent of changedPixels i.e. nonZeroValue(diffFrame) by applying this formula

 float motionPercent = (nonZeroValue(diffFrame)/nonZeroValue(Image1)) X 100;

5. 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

### 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:-

1. Subtract Image1 from Image2 using Core.subtract(Image1, Image2, diffFrame);
2. 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.
3. Then I calculate the number of nonZeroValue of diffFrame using Core.countNonZero(diffFrame);
4. Then I calculate percent of changedPixels i.e. nonZeroValue(diffFrame) by applying this formula

 float motionPercent = (nonZeroValue(diffFrame)/nonZeroValue(Image1)) X 100;

5. 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

http://paxcel.net/blog/