1

Is it possible to achieve motion detection using php?

I'm not a GD/Image library expert, but shouldn't it be possible to detect if there's a significant difference between two images and have that trigger a warning?

hakre
  • 193,403
  • 52
  • 435
  • 836
Citizen
  • 12,430
  • 26
  • 76
  • 117

4 Answers4

5

I've been working on this type of problem recently. I've managed to compare two images together at any specified level resolution, and gather information about where change is happening, if any. An example using my code.

// setup two images to work with
$i1 = new SimpleImage("./img/0392.jpg");
$i2 = new SimpleImage("./img/0393.jpg");

$state = new State(15, 8, $i1);
$state = $state->difference(new State(15, 8, $i2), rgbColorDistance);
$state->abs()->denoiseStdDev()->scale(10)->round(0);

// $box will hold an array (x,y,w,h) that indicates location of change
$box = $state->getBoundingBox($i1->getWidth(), $i1->getHeight());

Examples and explanation on my blogpost

View code and download from Github

It's still rudimentary but it's a start towards my larger goal of constantly reading motion from a stream of images updated from a webcam.

Pat Cullen
  • 135
  • 1
  • 3
4

I think in theory you can do this, you can come up with a basic image comparison. However doing this in PHP is not the best option, if you wanted to do fast frame by frame comparisons you would need to look into a compiled language.

As a basic image detection method you can do the following,

Greyscale Images, Sample every x pixels and compare (Use a threshold value). Then calculate the change percentage, if it's over say 15% you can assume motion.

It's a crude and basic method but it soulds like it will work for your needs.

LiamB
  • 18,243
  • 19
  • 75
  • 116
1

Found something: http://phpmotiondetect.sourceforge.net/

But I did not tested it.

mgutt
  • 5,867
  • 2
  • 50
  • 77
0

Sure, you can use something like imagecolorat to get the colors from pixels on 2 different images.

So yeah, you could do it just like in any other language I guess.

Nanne
  • 64,065
  • 16
  • 119
  • 163