运用OpenMP提速图像处理速度
生活随笔
收集整理的這篇文章主要介紹了
运用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加入配置文件,連代碼都不需要修改,非常方便。
來自為知筆記(Wiz)
#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加入配置文件,連代碼都不需要修改,非常方便。
來自為知筆記(Wiz)
附件列表
?
轉載于:https://www.cnblogs.com/jsxyhelu/p/8044844.html
總結
以上是生活随笔為你收集整理的运用OpenMP提速图像处理速度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷P2759 奇怪的函数
- 下一篇: Python基础学习总结(六)