OpenCV setNumThreads问题

时间:2014-03-04 14:12:27

标签: opencv

我正在尝试在OpenCV 2.4.8上启用代码并行化,并且由于某种原因它无法正常工作。我已经使用WITH_TBB = ONWITH_OPENMP = ON构建了openCV,但出于某种原因,OpenCV仍未对我的处理过程添加任何提升。

这里有谁知道发生了什么事?

更新

 int main() {

InitCounter();
vector< vector<Point> > _contours;
vector<Vec4i> _storage;
vector<Vec3f> circles;

VideoCapture capture("30-1.avi");
ofstream fout("data.txt"); 

if(!capture.isOpened()) 
return -1;

//time_t start, end;
int counter=0;
int frameCounter=0;
int frameno=0;

//clock_t startTime = clock();

//time(&start);

for(;;)
{

    Mat frame, finalFrame;
    capture >> frame; 

    // double start=CLOCK2();

    finalFrame = frame;

    cvtColor(frame, frame, CV_BGR2GRAY);

    GaussianBlur(frame, frame, Size(7,7), 1.5, 1.5);
    threshold(frame, frame, 20, 255, CV_THRESH_BINARY);

    dilate(frame, frame, Mat(), Point(-1, -1), 2, 1, 1);
    erode(frame, frame, Mat(), Point(-1, -1), 2, 1, 1);

    Canny(frame, frame, 20, 20*2, 3 );

    //double dur = CLOCK2()-start;
    //printf("avg time per frame %f ms. fps %f. frameno = %d\n",avgdur(dur),avgfps(),frameno++ );

    //time(&end);
    //++counter;
    //double sec=difftime(end,start);
    //double fps=counter/sec;
    //cout<<fps<<endl;
    //fout << fps<<"\n";
    //frameCounter++;


    vector<Vec3f> circles;

    findContours(frame,_contours,_storage,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );

    vector<vector<Point> > contours_poly( _contours.size() );
    vector<Rect> boundRect( _contours.size() );
    vector<Point2f>center( _contours.size() );
    vector<float>radius( _contours.size() );

    int temp = 0;

    for( int i = 0; i < _contours.size(); i++ )
    { 
        if( _contours[i].size() > 100 )
        {
           approxPolyDP( Mat(_contours[i]), contours_poly[i], 3, true );
           boundRect[i] = boundingRect( Mat(_contours[i]) );
           minEnclosingCircle( (Mat)_contours[i], center[i], radius[i] );
           temp = i;
           break;
        }
    }

    //Scalar color2 = Scalar( 0, 0,255 );
    //rectangle( finalFrame, boundRect[temp].tl(), boundRect[temp].br(), color2, 2, 7, 0 );

    //fout << avgdur(dur)<<"\n"; 
    //frameCounter++;

    //if(frameCounter == 3600)
    //break;

    //imshow("frame", finalFrame);
    if(waitKey(1000/120) >= 0) break;
    //waitKey(1000/120);
}

 //clock_t ends = clock();
 //cout << "Running Time : " << (double) (ends - startTime) / CLOCKS_PER_SEC << endl;

system("pause");
 }

1 个答案:

答案 0 :(得分:1)

尝试运行此代码(将其(主体)附加到程序的开头):

#include "stdafx.h"
#include <stdio.h>
#include <omp.h>

int main(int argc, char* argv[])
{

#ifdef _OPENMP
    printf("OpenMP is supported. Supported version is: %4.2f\n",_OPENMP/100.0);

    int N_PROC = omp_get_num_procs(); // Number of available processors
    printf("Number of available processors: %d\n",N_PROC);

    int MAX_THREADS = omp_get_max_threads(); // Numbers of available threads
    printf("Numbers of available threads: %d\n",MAX_THREADS);

#else
    printf("OpenMP is not supported.");
#endif

 getchar();
    return 0;
}

它打印什么?

UPD:手动并行化的例子:

void NEDI(Mat& src,Mat &dst,int ZK=1,int MT=6,int ML=3,double BT=16,int BS=16,int SZ=8)
{
    if(src.channels()>1)
    {
        vector<Mat> src_arr;
        cv::split(src,src_arr);
        Mat tmp;
        // If openmp supported, then go parallel
#ifdef _OPENMP
        // Thread number
        omp_set_num_threads(3);
#pragma omp parallel shared(src_arr) private(tmp)
        {       
            int n=omp_get_thread_num(); // Get number of current thread
            Nedi_1_Channel(src_arr[n],tmp,ZK,MT,ML,BT,BS,SZ); // Process data for current thread
            tmp.copyTo(src_arr[n]);
        }       
#else
        Nedi_1_Channel(src_arr[0],tmp,ZK,MT,ML,BT,BS,SZ);
        tmp.copyTo(src_arr[0]);

        Nedi_1_Channel(src_arr[1],tmp,ZK,MT,ML,BT,BS,SZ);
        tmp.copyTo(src_arr[1]);

        Nedi_1_Channel(src_arr[2],tmp,ZK,MT,ML,BT,BS,SZ);
        tmp.copyTo(src_arr[2]);
#endif

        cv::merge(src_arr,dst);
    }else
    {
        Nedi_1_Channel(src,dst,ZK,MT,ML,BT,BS,SZ);
    }
}