Home | History | Annotate | Download | only in ippasync
      1 #include <stdio.h>
      2 
      3 #include "opencv2/core/utility.hpp"
      4 #include "opencv2/imgproc.hpp"
      5 #include "opencv2/highgui.hpp"
      6 #include "cvconfig.h"
      7 
      8 using namespace std;
      9 using namespace cv;
     10 
     11 #ifdef HAVE_IPP_A
     12 #include "opencv2/core/ippasync.hpp"
     13 
     14 #define CHECK_STATUS(STATUS, NAME)\
     15     if(STATUS!=HPP_STATUS_NO_ERROR){ printf("%s error %d\n", NAME, STATUS);\
     16     if (virtMatrix) {hppStatus delSts = hppiDeleteVirtualMatrices(accel, virtMatrix); CHECK_DEL_STATUS(delSts,"hppiDeleteVirtualMatrices");}\
     17     if (accel)      {hppStatus delSts = hppDeleteInstance(accel); CHECK_DEL_STATUS(delSts, "hppDeleteInstance");}\
     18     return -1;}
     19 
     20 #define CHECK_DEL_STATUS(STATUS, NAME)\
     21     if(STATUS!=HPP_STATUS_NO_ERROR){ printf("%s error %d\n", NAME, STATUS); return -1;}
     22 
     23 #endif
     24 
     25 static void help()
     26 {
     27  printf("\nThis program shows how to use the conversion for IPP Async.\n"
     28 "This example uses the Sobel filter.\n"
     29 "You can use cv::Sobel or hppiSobel.\n"
     30 "Usage: \n"
     31 "./ipp_async_sobel [--camera]=<use camera,if this key is present>, \n"
     32 "                  [--file_name]=<path to movie or image file>\n"
     33 "                  [--accel]=<accelerator type: auto (default), cpu, gpu>\n\n");
     34 }
     35 
     36 const char* keys =
     37 {
     38     "{c  camera   |           | use camera or not}"
     39     "{fn file_name|../data/baboon.jpg | image file       }"
     40     "{a accel     |auto       | accelerator type: auto (default), cpu, gpu}"
     41 };
     42 
     43 //this is a sample for hppiSobel functions
     44 int main(int argc, const char** argv)
     45 {
     46     help();
     47 
     48     VideoCapture cap;
     49     CommandLineParser parser(argc, argv, keys);
     50     Mat image, gray, result;
     51 
     52 #ifdef HAVE_IPP_A
     53 
     54     hppiMatrix* src,* dst;
     55     hppAccel accel = 0;
     56     hppAccelType accelType;
     57     hppStatus sts;
     58     hppiVirtualMatrix * virtMatrix;
     59 
     60     bool useCamera = parser.has("camera");
     61     string file = parser.get<string>("file_name");
     62     string sAccel = parser.get<string>("accel");
     63 
     64     parser.printMessage();
     65 
     66     if( useCamera )
     67     {
     68         printf("used camera\n");
     69         cap.open(0);
     70     }
     71     else
     72     {
     73         printf("used image %s\n", file.c_str());
     74         cap.open(file.c_str());
     75     }
     76 
     77     if( !cap.isOpened() )
     78     {
     79         printf("can not open camera or video file\n");
     80         return -1;
     81     }
     82 
     83     accelType = sAccel == "cpu" ? HPP_ACCEL_TYPE_CPU:
     84                 sAccel == "gpu" ? HPP_ACCEL_TYPE_GPU:
     85                                   HPP_ACCEL_TYPE_ANY;
     86 
     87     //Create accelerator instance
     88     sts = hppCreateInstance(accelType, 0, &accel);
     89     CHECK_STATUS(sts, "hppCreateInstance");
     90 
     91     accelType = hppQueryAccelType(accel);
     92 
     93     sAccel = accelType == HPP_ACCEL_TYPE_CPU ? "cpu":
     94              accelType == HPP_ACCEL_TYPE_GPU ? "gpu":
     95              accelType == HPP_ACCEL_TYPE_GPU_VIA_DX9 ? "gpu dx9": "?";
     96 
     97     printf("accelType %s\n", sAccel.c_str());
     98 
     99     virtMatrix = hppiCreateVirtualMatrices(accel, 1);
    100 
    101     for(;;)
    102     {
    103         cap >> image;
    104         if(image.empty())
    105             break;
    106 
    107         cvtColor( image, gray, COLOR_BGR2GRAY );
    108 
    109         result.create( image.rows, image.cols, CV_8U);
    110 
    111         double execTime = (double)getTickCount();
    112 
    113         //convert Mat to hppiMatrix
    114         src = hpp::getHpp(gray,accel);
    115         dst = hpp::getHpp(result,accel);
    116 
    117         sts = hppiSobel(accel,src, HPP_MASK_SIZE_3X3,HPP_NORM_L1,virtMatrix[0]);
    118         CHECK_STATUS(sts,"hppiSobel");
    119 
    120         sts = hppiConvert(accel, virtMatrix[0], 0, HPP_RND_MODE_NEAR, dst, HPP_DATA_TYPE_8U);
    121         CHECK_STATUS(sts,"hppiConvert");
    122 
    123         // Wait for tasks to complete
    124         sts = hppWait(accel, HPP_TIME_OUT_INFINITE);
    125         CHECK_STATUS(sts, "hppWait");
    126 
    127         execTime = ((double)getTickCount() - execTime)*1000./getTickFrequency();
    128 
    129         printf("Time : %0.3fms\n", execTime);
    130 
    131         imshow("image", image);
    132         imshow("rez", result);
    133 
    134         waitKey(15);
    135 
    136         sts =  hppiFreeMatrix(src);
    137         CHECK_DEL_STATUS(sts,"hppiFreeMatrix");
    138 
    139         sts =  hppiFreeMatrix(dst);
    140         CHECK_DEL_STATUS(sts,"hppiFreeMatrix");
    141 
    142     }
    143 
    144     if (!useCamera)
    145         waitKey(0);
    146 
    147     if (virtMatrix)
    148     {
    149         sts = hppiDeleteVirtualMatrices(accel, virtMatrix);
    150         CHECK_DEL_STATUS(sts,"hppiDeleteVirtualMatrices");
    151     }
    152 
    153     if (accel)
    154     {
    155         sts = hppDeleteInstance(accel);
    156         CHECK_DEL_STATUS(sts, "hppDeleteInstance");
    157     }
    158 
    159     printf("SUCCESS\n");
    160 
    161 #else
    162 
    163     printf("IPP Async not supported\n");
    164 
    165 #endif
    166 
    167     return 0;
    168 }
    169