cv::parallel_for_ 的一个例子
重點(diǎn):
??? paralle_for_設(shè)置成n個線程,則實(shí)際只有n-1線程并行,第n個線程會等待其他線程運(yùn)行結(jié)束后再執(zhí)行,所以n=1和n=2實(shí)際上都是串行
cv::parallel_for_是opencv封裝的一個多線程接口,利用這個接口可以方便實(shí)現(xiàn)多線程,不用考慮底層細(xì)節(jié),以下是一個具體的例子
??? 繼承ParallelLoopBody,重載運(yùn)算符()
class LoopBody : public cv::ParallelLoopBody
{
public:
??? LoopBody (const std::vector<std::string>& filenames)
??????? : m_filenames(filenames)
??? {
??? }
??? virtual void operator()(const cv::Range& r) const
??? {
??????? for (int i = r.start; i != r.end; i++)? //遍歷
??????? {
?? ??? ??? ?std::cout << m_filenames[i] << std::endl;
??????????????? ?
??????????? //load image and to to sth
??????? }
??? }
protected:
??? const std::vector<std::string>& m_filenames;?? ?
};
??? 啟動循環(huán)
?? ??? ?LoopBody body(filenames);
?? ??? ?cv::parallel_for_(cv::Range(0, static_cast<int>(filenames.size())), body); //啟動
??? 控制線成數(shù)目
??? 以前還有一個cv::parallel_for() 函數(shù)(不是以下劃線結(jié)束)是單線程,就相當(dāng)于for循環(huán),但新版本的opencv里沒有找個函數(shù)了,那么如果利用cv::parallel_for_()實(shí)現(xiàn)單線成的for循環(huán)?答案是把線程數(shù)設(shè)置成1
cv::setNumThreads(1);
原文鏈接:https://blog.csdn.net/z0n1l2/article/details/86567139
總結(jié)
以上是生活随笔為你收集整理的cv::parallel_for_ 的一个例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 八款常用的 Python GUI 开发框
- 下一篇: pycharm中使用anaconda中p