OpenCV CascadeClassifier C++ Interface in Multiple

2020-06-22 04:33发布

问题:

I would like to run an OpenCV C++ Interface using the CascadeClassifier Objects in multiple threads.

The way my program works is my main thread loads "some_file.xml" into a CascadeClassifier object. Three or more threads are spawned and they are passed the cascade object. The program soon crashes thereafter. I have done several tests and concluded that the CascadeClassifier object is not thread-safe when doing a "detectmultiscale" function.

I would like to avoid having to read the same file off of the hard drive every time a new thread is spawned. How can this be avoided?

回答1:

If you are working with LBP cascade of with Haar cascade stored in new format then you can avoid reading cascade from file system for each new thread:

Load cascade into memory:

cv::FileStorage fs(path_to_cascade_file, cv::FileStorage::READ);
if (!fs.isOpened())
    HandleError();

Next pass fs object to the each new thread and create CascadeClassifier object:

cv::CascadeClassifier cc;
if (!cc.read(fs.getFirstTopLevelNode())
    HandleError2();