Jump to content

[C#] Fastet / Ressource Friendliest Motion Detection

cluelessgenius

can someone tell me how to get motion detection over a webcam running...

 

online i can only find examples from 2004 and all the references are expired.

 

i usually dont do this kind of work so i got no clue where to start right now.

 

i dont need anything fancy just the bare minimum to detect motion from the webcam of a notebook

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Motion detection is the easiest at least. I don't have a link at hand but the workflow is pretty simple.

1 - Get reference frame

2 - Pull frame from the camera each X milliseconds/seconds.

3 - compare for movement

 

I have in the past use ImageMagik for image manipulation and used a contrast enhancer and other method in order to get a contour image version of each frame. This remove all colors and nearly all artifact cause by lighting, pixelation. Then i compared the image for similarity (this algorithm exist on the web) with a percentage value you have to be closed by to the reference frame in order to consider movement occured.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Zagna said:

Maybe with Accord.NET?

i think machine vision might be a bit overkill here no?

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Theoretically, a camera sensor is just a passive LIDAR sensor, so there's an entire field of knowledge to look into.

But I suspect that the passive LIDAR perspective is overkill for what you are trying to do, and probably only works well for high quality cameras.

 

One way that I can think of off the top of my head would be to run a good edge detection filter over each frame, then run a polygon detection algorithm over that. Mark each polygon, and track their positions. Any existing polygon that moves more than your error value would then trigger the motion detection. But that has a hidden problem, which is: How do I match up the same polygons, possibly in different positions, over multiple images?

This may very well be the same algorithm that @Franck mentioned, depending on how you actually do the image comparison.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, straight_stewie said:

Theoretically, a camera sensor is just a passive LIDAR sensor, so there's an entire field of knowledge to look into.

But I suspect that the passive LIDAR perspective is overkill for what you are trying to do, and probably only works well for high quality cameras.

 

One way that I can think of off the top of my head would be to run a good edge detection filter over each frame, then run a polygon detection algorithm over that. Mark each polygon, and track their positions. Any existing polygon that moves more than your error value would then trigger the motion detection. But that has a hidden problem, which is: How do I match up the same polygons, possibly in different positions, over multiple images?

This may very well be the same algorithm that @Franck mentioned, depending on how you actually do the image comparison.

Yes exactly that, edge algorithm, i was looking for the word.

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/6/2020 at 4:57 PM, Franck said:

Then i compared the image for similarity (this algorithm exist on the web) with a percentage value you have

any links you could throw my way.

 

i got the camera running now using opencvsharp 

 

but now how to get a simple boolean "movement"?

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, cluelessgenius said:

any links you could throw my way.

 

i got the camera running now using opencvsharp 

 

but now how to get a simple boolean "movement"?

Not that i remember on top of my head. I have done that in ~2002-2003. Back then it was .Net 1.1. @straight_stewie had the proper term, it's called edge detection nowaday there is probably more library that does that and those library probably even had the last step which is compare the results for similarities.

 

But here an example of a quick search about it and with Image Magick as well.

https://www.imagemagick.org/discourse-server/viewtopic.php?t=25983

The thing is that edge detection clear the image a lot so you use a lot less details to compare.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Franck said:

Not that i remember on top of my head. I have done that in ~2002-2003. Back then it was .Net 1.1. @straight_stewie had the proper term, it's called edge detection nowaday there is probably more library that does that and those library probably even had the last step which is compare the results for similarities.

 

But here an example of a quick search about it and with Image Magick as well.

https://www.imagemagick.org/discourse-server/viewtopic.php?t=25983

The thing is that edge detection clear the image a lot so you use a lot less details to compare.

well i got edge detection running like this

FrameSource cam = Cv2.CreateFrameSource_Camera(1);
Cv2.NamedWindow("Camera", WindowMode.FullScreen);

Mat t0 = new Mat();
Mat t1 = new Mat();
Mat t2 = new Mat();

Mat frame = new Mat();
while (true)
{
    cam.NextFrame(t0);
    cam.NextFrame(t1);
    cam.NextFrame(t2);

    Cv2.CvtColor(t0, t0, ColorConversionCodes.BGR2GRAY);
    Cv2.CvtColor(t1, t1, ColorConversionCodes.BGR2GRAY);
    Cv2.CvtColor(t2, t2, ColorConversionCodes.BGR2GRAY);

    Cv2.ImShow("Camera", DiffImage(t0, t1, t2));

    if (Cv2.WaitKey(1) == (int)ConsoleKey.Enter)
        break;
}

Mat DiffImage(Mat t0, Mat t1, Mat t2)
{
    Mat d1 = new Mat();
    Cv2.Absdiff(t2, t1, d1);

    Mat d2 = new Mat();
    Cv2.Absdiff(t1, t0, d2);

    Mat diff = new Mat();
    Cv2.BitwiseAnd(d1, d2, diff);

    return diff;
}

and it shows the edges visually but its not sensitive enough for my lighting situation so the screen is mostly black and i havent understood the method enough to get me a simple bool out of it

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, cluelessgenius said:

and it shows the edges visually but its not sensitive enough for my lighting situation

Well that as to be configured for each situations. Like security camera system you need to have enough light or it won't detect anything unless you tweak the filter per camera basis. Hence why you typically use a infrared signal as you have a much better light source at night and have a better detection that way. If your environment is to dim you need to enhance the brightness and contrast to grasp more details of the image from the camera. Also you should always desaturate the image as 99% of the webcam have horrible color accuracy which can throw you off.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×