How to implement a circular buffer of cv::Mat objects (OpenCV)?
Asked Answered
P

3

5

I'm trying to implement a circular buffer for my program. The buffer is used to share data between two threads as shown below. I use OpenCV to grab video frames from camera (Thread 1). Then I would like to store this data in a circular buffer, so that Thread 2 can get the data from the buffer.

enter image description here

How can I implement a circular buffer for cv::Mat objects in C++? I know how to create circular buffer for standard C++ objects (like int or char) but I can't make it work with objects of type cv::Mat.

Any suggestions?

Photolithography answered 27/2, 2012 at 21:51 Comment(7)
What difficulty are you having with the cv::Mat portion of the problem? How does that data type change the task of writing a circular buffer?Parkway
@RobKennedy I included the code that I'm having problems with. I used en.wikipedia.org/wiki/Circular_buffer as example and modified it to store data of cv::Mat type (instead of type 'int') but now the code throws a run-time error. Thank you.Photolithography
The circular buffer code itself works fine for me (MSVC 2010 Ultimate SP1), which means your crash is cause by something OpenCV related, which you would probably be best off using a debugger to find.Silvas
Is this the actual code that's crashing? I don't see threads here.Jarboe
In cbWrite shouldn't the check be for cb->end==cb->size (to reset cb->end = 0 to avoid buffer overruns? I realize this probably isn't what is leading to the crash but it still looks dangerous.Refection
@MarkRansom Yes, this is the actual code that's crashing and the code does not have threads yet (just want to get the code working without threads first). ThanksPhotolithography
Thanks @Necrolis. You may be right, the circular buffer code worked fine for me too for int data types. I checked to make sure that 'elem.value' contains correct data. I think this might have something to do with the way OpenCV stores frames in cv::Mat.Photolithography
P
5

Solved, see Thread safe implementation of circular buffer

Photolithography answered 29/2, 2012 at 15:18 Comment(0)
P
3

Whats wrong with just a vector and an index to the next slot to write to and the next one to process?

All you have to handle is the wrap around when you get to the end, and if you use a power of 2 in the vector size you can use a simple mask for that.

Possing answered 27/2, 2012 at 22:54 Comment(2)
Thanks. I will look into this tomorrow.Photolithography
@Alex, using a vector will certainly simplify the code but I don't think it will fix your error. Be certain to size the vector to the capacity of the circular buffer before trying to use it.Jarboe
J
1

A circular buffer is thread-safe when only the writing thread updates the end pointer and only the reading thread updates the start pointer, and accesses to those pointers are atomic. You have a spot in cbWrite that updates start which will lead to a race condition.

Jarboe answered 27/2, 2012 at 23:6 Comment(1)
The sample code (that's crashing) does not have threads yet. Thanks for the tip though; implementing threads will be my next step.Photolithography

© 2022 - 2024 — McMap. All rights reserved.