I have a correctly working code to process 1000 image files using openCV. As the operation is independent of individual files, I have used multithreading using the std::async
function.
I am launching the threads by following function call inside for
loop.
std::vector<std::future<cv::Mat>> processingThread;
for (int i = 0; i < jsonObjects.size(); i++) {
processingThread.emplace_back(std::async(std::launch::async, (cv::Mat(CameraToBEV::*)(json&, std::vector<cv::Point2f>, cv::Point2f, cv::Point2f)) & CameraToBEV::process, &cbevVec[i], std::ref(jsonObjects[i]), roiBox1, opDim,ipDim));
}
Above code is working fine and taking about 100 milliseconds. But to collect the results I am using another for
loop as follows;
std::vector<cv::Mat> opMatArr;
for (auto& future : processingThread) {
opMatArr.emplace_back(future.get());
}
This is also working fine but it is taking 9 seconds to execute, which kind of defeating the purpose of using multithreading as I am sequentially populating the vector of cv::Mat
objects.
Is there any way like, parallelly, as in, in few milliseconds I should be able to get all the cv::Mat
objects in the std::vector opMatArr
?