zoukankan      html  css  js  c++  java
  • 数字图像处理之并行处理(1)

      在处理图像时,会经常对像素进行操作,实时性要求较高的场所往往会使用并行处理,好在(C/C++ API)支持多种并行方式:mpi,openmp,intel ipp 等,今天记录一种利用 openmp简单的并行处理图像方法:灰度图像取反。

      需要用到的头文件:#include “omp.h”,作者是基于opencv3.0处理的图片,cpu为赛扬E3200,双核。

    话不多说上代码:

    #include "opencv2/opencv.hpp"
    #include "omp.h"
    #include "time.h"
    #include <iostream>
    
    #pragma comment(lib,"opencv_world300.lib")
    
    using namespace cv;
    using namespace std;
    
    
    void normal(){
        clock_t start,end;
        Mat img = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);//读一张图(1920*1080),转为灰度图//
    
        Mat out(img.rows,img.cols,CV_8U);//初始化输出图像//
    
        unsigned char* p=out.data;//像素指针//
    
        //取反//
        start = clock();
        for(int i = 0;i<img.rows*img.cols;i++){
            *p++ =0xff-img.data[i];
        }
        end = clock();
    
        cout<<"norm_time"<<(end-start)<<endl;
    
    
    }
    
    void test_omp(){
    
        clock_t start,end;
        Mat img = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);//读一张图,转为灰度图//
    
        Mat out(img.rows,img.cols,CV_8U);//初始化输出图像//
    
        unsigned char* p=out.data;//像素指针//
        //omp//
        int num = img.rows*img.cols;//openmp 限制循环格式//
    
        start =clock();
    #pragma omp parallel for
        for (int i = 0;i<num;i++)
        {
            *p++ =0xff -img.data[i];
        }
        end  = clock();
    
        cout<<"omp_time"<<(end-start)<<endl;
    
    }
    
    
    
    int main(){
    
        normal();
        test_omp();
    
        return 0;
    
    }

    release模式 结果如下:

    时间单位为毫秒,双核并行速度刚好是普通处理两倍

    后记:

      openmp主要用于循环级的并行程序,某些应用可能并不适合,当然由于图像数据的特殊性正好合适;其次openmp编写对for循环的格式要求比较多,具体细节可以参考http://www.cnblogs.com/yangyangcv/archive/2012/03/23/2413335.html。

      今天就写这么多了,未完待续。。。

    rebooting...
  • 相关阅读:
    离开APM的弹性云还是真弹性吗
    系统性能工程师
    How the performance impacts your revenue-性能影响营收
    The Performance Manifesto
    APM系列-国外新兴厂商New Relic vs. AppDynamics
    Performance testing architecture
    Does Little'law really applicable to apply performance model now?
    Load average in Linux的精确含义
    Eric's并发用户数估算与Little定律的等价性
    Sublime Text 3 插件安装及Vim 模式设置
  • 原文地址:https://www.cnblogs.com/hutiann/p/5719471.html
Copyright © 2011-2022 走看看