3

I have a C++ program that continuously takes depth frames from a kinect sensor (usung opencv) and saves them to a png. I have another MATLAB script that reads these images and processes them.

The problem is that MATLAB fails to read the png since the C++ program is likely overwriting the file faster than MATLAB can read it. I figured that if the image writes and reads could be synchronized, the problem can be solved.

What could be the simplest way to achieve this? I'm willing to take a some performance hit as long as the solution works.

Nixt
  • 144
  • 1
  • 9
  • Perhaps consider the approach suggested here: http://stackoverflow.com/questions/1513583/how-to-call-matlab-code-from-c – Matt Jul 05 '13 at 16:06

2 Answers2

5

Make a temporary empty/single-byte file whose presence indicates that the C++ program has written a new frame, then tell the C++ to wait until Matlab has deleted, read the png, and written a different temporary file to indicate that it has finished?

Hugh Nolan
  • 2,508
  • 13
  • 15
2

If Matlab is always slower than C++ in processing a single frame, then just have your C++ program write a new frame every time with sequential numbering, then have Matlab delete the frame that it had just read. This way you end up with a "buffer" and you only use the disk space that is needed for both programs to function correctly.

Bee
  • 2,472
  • 20
  • 26
  • I don't think this will work because the C++ program will write frames to this "buffer" faster than MATLAB can delete from it. The buffer will continue to grow in size until the disk space is exhausted. – Nixt Jul 05 '13 at 16:41
  • You either care about Matlab processing each and every frame or you don't. If it's a limited period of time, it's OK for the buffer to grow. This will guarantee that your Matlab program reads each frame. If it's an infinite loop, and your Maltab program is slower than C++, then at some point you need to drop a frame to catchup, which is the solution you already have. It means you don't care if a frame is dropped. – Bee Jul 05 '13 at 21:00
  • That's right, some frames need to be skipped. MATLAB can skip a frame or two but it must not lag behind. – Nixt Jul 06 '13 at 04:08