aihot  2017-05-25 21:02:02  OpenCV |   查看评论   

testSimd.cpp:

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <string>  
  4.   
  5. using namespace std;  
  6.   
  7. void BgraToGrayTest()  
  8. {  
  9.     string strImageName = "../../../testdata/cat.jpg";  
  10.     int iImageWidth = 10000;  
  11.     int iImageHeight = 10000;  
  12.   
  13.     cv::Mat matSrc = cv::imread(strImageName, 1);  
  14.     cv::cvtColor(matSrc, matSrc, cv::COLOR_BGR2BGRA);  
  15.     cv::resize(matSrc, matSrc, cv::Size(iImageWidth, iImageHeight), 0, 0, 1);  
  16.   
  17.     cv::Mat matDst1, matDst2;  
  18.     matDst1 = cv::Mat::zeros(iImageHeight, iImageWidth, CV_8UC1);  
  19.     matDst2 = cv::Mat::zeros(iImageHeight, iImageWidth, CV_8UC1);  
  20.   
  21.     int iRemainder = iImageWidth & 0x03;  
  22.     int iGrayStride =  iRemainder ? iImageWidth + 4 - iRemainder : iImageWidth;  
  23.     CV_Assert(iRemainder == 0);  
  24.   
  25.     double dTimeC = cv::getTickCount();  
  26.     Simd::Base::BgraToGray(matSrc.data, iImageWidth, iImageHeight, iImageWidth * 4, matDst1.data, iGrayStride);  
  27.     dTimeC = ((double)cv::getTickCount() - dTimeC) / cv::getTickFrequency();  
  28.   
  29.     double dTimeSimd = cv::getTickCount();  
  30.     Simd::Sse2::BgraToGray(matSrc.data, iImageWidth, iImageHeight, iImageWidth * 4, matDst2.data, iGrayStride);  
  31.     dTimeSimd = ((double)cv::getTickCount() - dTimeSimd) / cv::getTickFrequency();  
  32.   
  33.     cout<<"C run time : "<<dTimeC<<endl;  
  34.     cout<<"Simd run time : "<<dTimeSimd<<endl;  
  35.   
  36.     int iDiffCount = 0;  
  37.   
  38.     for (int i = 0; i < iImageHeight; i++) {  
  39.         uchar* p1 = matDst1.ptr<uchar>(i);  
  40.         uchar* p2 = matDst2.ptr<uchar>(i);  
  41.   
  42.         for (int j = 0; j < iImageWidth; j++) {  
  43.             if (p1[j] != p2[j])  
  44.                 iDiffCount ++;  
  45.         }     
  46.     }  
  47.   
  48.     cout<<"the different count: "<<iDiffCount<<endl;  
  49. }  
  50.   
  51. int main(int argc, char* argv[])  
  52. {  
  53.     BgraToGrayTest();  
  54.   
  55.     cout<<"ok!"<<endl;  
  56.     return 0;  
  57. }  

运行结果见下 图:

开源库Simd在vs2010中的编译及简单使用

运行多次,SIMD的执行速度基本上比C快3倍,它们的结果是完全一致的。

 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自开源库Simd在vs2010中的编译及简单使用

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]