Search code examples
c#multithreadingimage-processingwebcamtask-parallel-library

Capture high speed webcam and process frames using threads?


I have a PS3 Eye webcam capable of delivering frames up to 120 times a second based on the resolution.

I need to do some processing on each frame, and currently that takes up quite a lot of time. The webcam is set to 60 fps, that leaves around 16 ms of processing time per frame, but due to the processing bottleneck, i am only able to retrieve around 30 frames a second.

Behind the scenes a while loop retrieves the bitmap data from the camera, and sets a bitmap property, and notifies that a new frame has been retrieved, so the images wont stack up if processing takes too long.

I am thinking i should be able to somehow use threading to make the processing "faster".

Perhaps like this

  1. Frame received
  2. Thread 1 starts processing
  3. Frame received
  4. Thread 1 is busy
  5. Thread 2 starts processing
  6. Thread 1 finished processing
  7. Frame received
  8. Thread 1 starts processing
  9. Thread 2 finished processing
  10. Etc

Do you guys think threading, perhaps by using the Task Parallel library will improve performance?

Thanks in advance!


Solution

  • Your idea should work. Theoretically you should now be able to "double time" and get to the desired 60FPS.

    One problem:

    1. Frame received
    2. Thread 1 starts processing
    3. Frame received
    4. Thread 1 is busy
    5. Thread 2 starts processing
    6. Frame received

    Whoops, now what, both are busy. So you might want to have a 3rd thread to act like a "server" which fetches the images, queues and serves them to you "worker" threads.