Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >OpenCV中OpenMP的使用

OpenCV中OpenMP的使用

作者头像
流川疯
发布于 2019-01-18 08:41:13
发布于 2019-01-18 08:41:13
1.6K00
代码可运行
举报
运行总次数:0
代码可运行

vs2010中调用openMP,并添加头文件#include<omp.h>

代码来源:

作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include "stdafx.h"

#include "cv.h" 
#include "highgui.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <omp.h>

#pragma comment(lib,"opencv_core2410d.lib")              
#pragma comment(lib,"opencv_highgui2410d.lib")              
#pragma comment(lib,"opencv_imgproc2410d.lib")    

 

void EdgeOpenMP(IplImage *src,IplImage *dst,int thresh) 
{ 
    int height    = src->height; 
    int width     = src->width; 
    int step      = src->widthStep; 
    uchar *data1      = (uchar *)src->imageData; 
    uchar *data2      = (uchar *)dst->imageData;

    int i=step; 
    #pragma omp parallel for 
    for(i=step+1;i<height*width;i++){ 
         if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh) 
            data2[i]=255;/* 对于单通道,前后两帧差分大于门限 
            或者对于多通道前后两帧的一个指标差分大于门限,则视为边缘*/ 
         else 
            data2[i]=0; 
    } 
}

void Edge(IplImage *src,IplImage *dst,int thresh) 
{ 
    int height    = src->height; 
    int width     = src->width; 
    int step      = src->widthStep; 
    uchar *data1      = (uchar *)src->imageData; 
    uchar *data2      = (uchar *)dst->imageData;

   int i=step; 
    for(i=step+1;i<height*width;i++){ 
         if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh) 
            data2[i]=255; 
         else 
            data2[i]=0; 
    } 
}


int main() 
{ 
  char filename[512]; 
  IplImage *src,*edge1,*edge2; 
  puts("File name:"); 
  gets(filename); 
  src = cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE ); 
  edge1=cvCloneImage(src); 
  edge2=cvCloneImage(src);

  cvNamedWindow("src", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("src", 100, 100); 
  cvShowImage( "src", src); 
  cvNamedWindow("Edge", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("Edge", 200, 100); 
  cvNamedWindow("EdgeOpenMP", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("EdgeOpenMP", 300, 100); 
  /* 以上都是准备一些窗口和图形基本数据 */

  int tekrar=100;//运行次数 
  int thresh=30; 
  double start, end,t1, t2; 
  
  /* 计算没有使用OpenMP优化的时间 */ 
  start= (double)cvGetTickCount();//记下开始的时钟计数,以便计算函数或用户代码执行时间 
  for(int i=0;i<tekrar;i++) 
    Edge(src,edge1,thresh); 
  end= (double)cvGetTickCount();//记下结束的时钟计数 
  t1= (end-start)/((double)cvGetTickFrequency()*1000.);//计算运行时间,以毫秒为单位 
  printf( "Run time without OpenMP = %g ms\n", t1 );

  /* 计算使用了OpenMP优化的时间 */ 
  start= (double)cvGetTickCount(); 
  for(int i=0;i<tekrar;i++) 
    EdgeOpenMP(src,edge2,thresh); 
  end= (double)cvGetTickCount(); 
  t2= (end-start)/((double)cvGetTickFrequency()*1000.); 
  printf( "Run time with OpenMP = %g ms\n", t2 );

  printf( "Performance ratio (%%) = %% %.1f \n", 100*(t1/t2-1) );

  cvShowImage( "Edge", edge1); 
  cvShowImage( "EdgeOpenMP", edge2); 
  cvWaitKey(); 
  cvDestroyWindow("Edge"); 
  cvDestroyWindow("EdgeOpenMP"); 
  cvReleaseImage(&src); 
  cvReleaseImage(&edge1); 
  cvReleaseImage(&edge2); 
}
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
这是我的结果:


这里的测试结果:
http://blog.csdn.net/augusdi/article/details/8808226
  在cpp文件中添加如下代码:

[cpp] view plaincopyprint?

  1. #include "stdafx.h"  
  2. #include<omp.h>  
  3. #include<iostream>  
  4. usingnamespace std;  
  5. //循环测试函数  
  6. void test()  
  7. {  
  8. for(int i=0;i<10000;i++)  
  9. {  
  10. }  
  11. }  
  12. int _tmain(int argc,_TCHAR* argv[])  
  13. {  
  14. cout<<"这是一个串行测试程序!\n";  
  15. double start = omp_get_wtime( );//获取起始时间  
  16. for(int i = 0; i < 10000; i++)  
  17. {   
  18. test();  
  19. }  
  20. double end = omp_get_wtime( );  
  21. cout<<"计算耗时为:"<<end -start<<"\n";  
  22. cin>>end;  
  23. return 0;  
  24. }  
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include "stdafx.h"

#include<omp.h>

#include<iostream>

usingnamespace std;


//循环测试函数
void test()
{
for(int i=0;i<10000;i++)
{

}
}


int _tmain(int argc,_TCHAR* argv[])
{
cout<<"这是一个串行测试程序!\n";
double start = omp_get_wtime( );//获取起始时间

for(int i = 0; i < 10000; i++)
{ 
test();
}

double end = omp_get_wtime( );

cout<<"计算耗时为:"<<end -start<<"\n";

cin>>end;

return 0;
}

       以上代码中红色字体为添加的代码,以上程序是一个典型的串行程序,经过随机运行10次,其平均耗时约0.283273s(具体所耗时间跟测试计算机有密切的关系,测试电脑CPU采用Core I7 2630QM,4核)。

       下面将其转换成并行程序,只需要在for循环加上#pragma omp parallel for即可,如下代码(注意红色部分):

[cpp] view plaincopyprint?

  1. #include "stdafx.h"  
  2. #include<omp.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5. //循环测试函数  
  6. void test()  
  7. {  
  8. for(inti=0;i<10000;i++)  
  9. {  
  10. }  
  11. }  
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14. cout<<"这是一个并行测试程序!\n";  
  15. doublestart = omp_get_wtime( );//获取起始时间  
  16. #pragma ompparallel for  
  17. for(inti = 0; i < 10000; i++)   
  18. {  
  19. test();  
  20. }  
  21. doubleend = omp_get_wtime( );  
  22. cout<<"计算耗时为:"<<end -start<<"\n";  
  23. cin>>end;  
  24. return0;  
  25. }  
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include "stdafx.h"

#include<omp.h>

#include <iostream>

using namespace std;


//循环测试函数
void test()
{
for(inti=0;i<10000;i++)
{

}
}

int _tmain(int argc, _TCHAR* argv[])
{
cout<<"这是一个并行测试程序!\n";

doublestart = omp_get_wtime( );//获取起始时间


#pragma ompparallel for
for(inti = 0; i < 10000; i++) 
{
test();
}


doubleend = omp_get_wtime( );

cout<<"计算耗时为:"<<end -start<<"\n";

cin>>end;

return0;
}

       同样,也经过10次随机的运行,其平均耗时约为0.06358044s,两种不同运行方式的比较结果如下表所示:

次数

串行

并行

1

0.283382

0.0746704

2

0.283654

0.0686404

3

0.283212

0.0536631

4

0.280234

0.0517737

5

0.283041

0.0717588

6

0.283126

0.0524264

7

0.281881

0.0580316

8

0.283301

0.0730386

9

0.284545

0.0745088

10

0.286353

0.0572926

平均值

0.283273

0.06358044

       两种运行方式的结果如下图所示:

       从上面的分析结果可见,采用OpenMP并行所耗时间仅为串行的22.44%,节约近4.5倍的时间。

相关程序源码下载地址: http://download.csdn.net/detail/xwebsite/3843187

代码语言:javascript
代码运行次数:0
运行
复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年03月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
OpenCV计算物体的重心坐标(2值图像)
http://download.csdn.net/detail/wangyaninglm/9389338 
流川疯
2019/01/18
2K0
C++多线程-多核编程
多核编程并不是最近才兴起的新鲜事物。早在intel发布双核cpu之前,多核编程已经在业内存在了,只不过那时候是多处理器编程而已。为了实现多核编程,人们开发实现了几种多核编程的标准。open-mp就是其中的一种。对于open-mp还不太熟悉的朋友,可以参照维基百科的相关解释。
cwl_java
2020/01/15
2.4K0
修改ncnn的openmp异步处理方法 附C++样例代码
ncnn刚发布不久,博主在ios下尝试编译。 遇上了openmp的编译问题。 寻找各种解决方案无果,亲自操刀。 采用std::thread 替换 openmp。 ncnn项目地址: https://github.com/Tencent/ncnn 后来询问ncnn的作者才知道在ios下的编译方法。 至此,当时的临时方案 采用std::thread 替换 openmp。 想想也许在一些特定情况下还是比较适用的,当前方便两者之间进行切换验证。 抽空写了一个示例项目。 项目地址: https://github.co
cpuimage
2018/04/12
2.2K0
利用OpenMP实现埃拉托斯特尼(Eratosthenes)素数筛法并行化
筛法是一种简单检定素数的算法。据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274~194年)发明的,又称埃拉托斯特尼筛法(sieve of Eratosthenes)。
恋喵大鲤鱼
2018/08/03
1.5K0
OpenCV导向滤波(引导滤波)实现(Guided Filter)代码,以及使用颜色先验算法去雾
 论文下载地址:http://research.microsoft.com/en-us/um/people/jiansun/papers/GuidedFilter_ECCV10.pdf
流川疯
2019/01/18
2.6K0
OpenCV 1 图像分割--分水岭算法代码
// watershed_test20140801.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" // // ch9_watershed image // This is an exact copy of the watershed.cpp demo in the OpenCV ../samples/c directory // // Think about using a morphologically eroded forground and backg
流川疯
2022/11/29
2970
OpenCV由汉字生成图片(透明)----可以对抗论文查重!!!
        今天听说很多同志们写毕业论文重复率过高的问题,大牛说用图片代替字就行了,我就想用OpenCV实现一下看看能不能搞,果不其然还是可以的!!!主要的难点在于普通格式的图片背景不透明,需要使用背景透明的png格式图片就行。
流川疯
2019/01/18
1.6K0
谷歌开源项目Google Preview Image Extractor(PIEX) (附上完整demo代码)
前天偶然看到谷歌开源项目中有一个近乎无人问津的项目Google Preview Image Extractor(PIEX) 。 项目地址: https://github.com/google/piex 官方的描述是这样的: The Preview Image Extractor (PIEX) is designed to find and extract the largest JPEG compressed preview image contained in a RAW file. 也就是说,这个项目
cpuimage
2018/04/12
2.2K0
谷歌开源项目Google Preview Image Extractor(PIEX) (附上完整demo代码)
OpenCV 实现哈哈镜效果
代码,有参考别人的代码 // haha_mirror.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> #include "cv.h" #include "highgui.h" #include "math.h" #include "opencv2/core/core.hpp" #pragma comment(lib,"opencv_core2410d.lib") #pragma comment
流川疯
2022/06/16
3500
OpenCV 实现哈哈镜效果
多核程序设计的相关基础知识----以误差扩散算法为例
    本文从基础入手,主要阐述基于桌面电脑的多核程序设计的基础知识,包括一些向量化运算,虚拟机算,多线程等的相关知识总结。
流川疯
2019/01/18
7880
OpenMP基础----以图像处理中的问题为例
1.循环语句中的循环变量必须是有符号整形,如果是无符号整形就无法使用,OpenMP3.0中取消了这个约束
流川疯
2022/05/10
1.3K0
【C++】基础:OpenMP并行编程入门
OpenMP是一种用于并行编程的开放标准,它旨在简化共享内存多线程编程的开发过程。OpenMP提供了一组指令和库例程,可以将顺序程序转换为可并行执行的代码。
DevFrank
2024/07/24
6880
OpenGL OpenCV根据视差图重建三维信息
代码如下: // disparity_to_3d_reconstruction.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" //Huang,Hai
流川疯
2022/11/29
4260
OpenGL OpenCV根据视差图重建三维信息
OpenMP并行编程入门指南
在C++中使用openmp进行多线程编程 - DWVictor - 博客园 (cnblogs.com)
用户9831583
2023/02/27
1.8K0
OpenMP并行编程入门指南
OpenCV进行图像相似度对比的几种办法
PSNR(Peak Signal to Noise Ratio),一种全参考的图像质量评价指标。
流川疯
2019/01/18
6.7K0
OpenCV 闭合轮廓检测
这个好像是骨头什么的,但是要求轮廓闭合,于是对图片进行一下膨胀操作,再次检测轮廓就好了。
流川疯
2022/11/29
9330
OpenCV 闭合轮廓检测
OpenMP并行编程简介
在这学期的并行计算课程中,老师讲了OpenMP,MPI,CUDA这3种并行计算编程模型,我打算把相关的知识点记录下来,便于以后用到的时候查阅。
王云峰
2019/12/25
3.2K0
OpenMP并行编程简介
基于重心偏移的视差计算
    视差的计算,主要要计算待匹配图像对应像素的水平偏移,那么针对一个物体而言,其在场景中的视差大体上应该是平滑的,所以可以直接针对分割出的物体计算重心的水平偏移从而得到视差值,我做了一个小实验,感觉效果还行,下面是代码和实验结果,希望各位有什么想法大家交流。
流川疯
2019/01/18
7770
图像处理-图像分割-大津法
最大类间方差法是1979年由日本学者大津提出的,是一种自适应阈值确定的方法,又叫大津法,简称OTSU
AomanHao
2022/01/14
5750
GrabCut in One Cut(基于图割算法grabcut的一次快速图像分割的OpenCV实现)----目前效果最好的图割
    这是博主近期看到的效果最好,实现最简单,运算时间最短的交互式图割算法,而且由于是发明图割算法实验室原班人马的文章和代码,所以非常值得研究。
流川疯
2019/01/18
2.5K0
推荐阅读
相关推荐
OpenCV计算物体的重心坐标(2值图像)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验