運用OpenMP提速圖像處理速度


一、算法測試
// openmptest的測試程序
# include  "stdafx.h"
void Test( int n){
     for ( int i = 0;i < 10000;i ++)
    {
         int j = 0;
        j  = j + 1;
    }
    printf( "%d",n);
}
int _tmain( int argc, _TCHAR * argv[])
{
     for ( int i = 0;i < 10;i ++)
    {
        Test(i);
    }
    getchar();
     return  0;
}
而開啟openmp

// openmptest的測試程序

# include  "stdafx.h"
void Test( int n){
     for ( int i = 0;i < 10000;i ++)
    {
         int j = 0;
        j  = j + 1;
    }
    printf( "%d",n);
}
int _tmain( int argc, _TCHAR * argv[])
{
     for ( int i = 0;i < 10;i ++)
    {
        Test(i);
    }
    getchar();
     return  0;
}
結果
可以發現明顯運算的順序變化了,就是因為有並行的存在。
二、批量處理多張圖片
編寫較為復雜的opencv 程序
// openmptest的測試程序
# include  "stdafx.h"
# include  <iostream >
# include  <opencv2 /opencv.hpp >  
# include  "GoCvHelper.h"
using  namespace std;
using  namespace cv;
using  namespace GO;
Mat Test(Mat src){
    Mat draw;
    Mat gray;
    cvtColor(src,gray,COLOR_BGR2GRAY);
    threshold(gray,gray, 100, 255,THRESH_OTSU);
    connection2(gray,draw);
     return draw;
}
int _tmain( int argc, _TCHAR * argv[])
{    
     //時間記錄
     const int64 start  = getTickCount();
    vector <Mat > vectorMats;
     //文件目錄
     char cbuf[ 100=  "F:/圖片資源/紋理庫brodatz/brodatzjpg";
     //獲取所有文件
    getFiles(cbuf,vectorMats);
     //循環處理
    // #pragma omp parallel for
     for ( int i = 0;i <vectorMats.size();i ++)
    {
        Mat dst  = Test(vectorMats[i]);
    }
    
     //時間
     double duration  = (cv : :getTickCount()  - start) /getTickFrequency();
    printf( "共消耗時間%f",duration);
    waitKey();
     return  0;
}
使用openmp的時間
不用mp的是這么長時間
三、處理視頻類流數據
 進一步對openmp進行研究,發現它對於流數據也有很好支持:
# pragma omp parallel sections  
    {
        # pragma omp section  
        {
                GetHessianLambdas(camframe, 5,lambda1_Sigma5,lambda2_Sigma5);
        }
        # pragma omp section  
        {
                GetHessianLambdas(camframe, 7,lambda1_Sigma7,lambda2_Sigma7);
        }
    }
就直接可已將運算速度至少增加一倍。
四、多平台支持。
而且對於QT的支持也非常直接,直接采用
QMAKE_CXXFLAGS += -fopenmp
LIBS += -fopenmp
加入配置文件,連代碼都不需要修改,非常方便。




附件列表

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM