OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Intel® IPP Asynchronous C/C++ library in OpenCV

Goal

The tutorial demonstrates the Intel® IPP Asynchronous C/C++ library usage with OpenCV. The code example below illustrates implementation of the Sobel operation, accelerated with Intel® IPP Asynchronous C/C++ functions. In this code example, cv::hpp::getMat and cv::hpp::getHpp functions are used for data conversion between hppiMatrix and Mat matrices.

Code

You may also find the source code in the samples/cpp/tutorial_code/core/ippasync/ippasync_sample.cpp file of the OpenCV source library or download it from here.

1 #include <stdio.h>
2 
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
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 }
hppiMatrix * getHpp(const Mat &src, hppAccel accel)
Create hppiMatrix from Mat.
Definition: ippasync.hpp:168
#define CV_8U
Definition: cvdef.h:101
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
virtual bool open(const String &filename)
Open video file or a capturing device for video capturing.
double getTickFrequency()
Returns the number of ticks per second.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
designed for command line arguments parsing
Definition: utility.hpp:594
Class for video capturing from video files, image sequences or cameras. The class provides C++ API fo...
Definition: videoio.hpp:429
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
void create(int rows, int cols, int type)
Allocates new array data if needed.
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
int64 getTickCount()
Returns the number of ticks.
n-dimensional dense array class
Definition: mat.hpp:726
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:511
int waitKey(int delay=0)
Waits for a pressed key.

Explanation

  1. Create parameters for OpenCV:
    VideoCapture cap;
    Mat image, gray, result;
    and IPP Async:
    hppiMatrix* src,* dst;
    hppAccel accel = 0;
    hppAccelType accelType;
    hppStatus sts;
    hppiVirtualMatrix * virtMatrix;
  2. Load input image or video. How to open and read video stream you can see in the Video Input with OpenCV and similarity measurement tutorial.
    if( useCamera )
    {
    printf("used camera\n");
    cap.open(0);
    }
    else
    {
    printf("used image %s\n", file.c_str());
    cap.open(file.c_str());
    }
    if( !cap.isOpened() )
    {
    printf("can not open camera or video file\n");
    return -1;
    }
  3. Create accelerator instance using hppCreateInstance:
    accelType = sAccel == "cpu" ? HPP_ACCEL_TYPE_CPU:
    sAccel == "gpu" ? HPP_ACCEL_TYPE_GPU:
    HPP_ACCEL_TYPE_ANY;
    //Create accelerator instance
    sts = hppCreateInstance(accelType, 0, &accel);
    CHECK_STATUS(sts, "hppCreateInstance");
  4. Create an array of virtual matrices using hppiCreateVirtualMatrices function.
    virtMatrix = hppiCreateVirtualMatrices(accel, 1);
  5. Prepare a matrix for input and output data:
    cap >> image;
    if(image.empty())
    break;
    cvtColor( image, gray, COLOR_BGR2GRAY );
    result.create( image.rows, image.cols, CV_8U);
  6. Convert Mat to hppiMatrix using cv::hpp::getHpp and call hppiSobel function.
    //convert Mat to hppiMatrix
    src = getHpp(gray, accel);
    dst = getHpp(result, accel);
    sts = hppiSobel(accel,src, HPP_MASK_SIZE_3X3,HPP_NORM_L1,virtMatrix[0]);
    CHECK_STATUS(sts,"hppiSobel");
    sts = hppiConvert(accel, virtMatrix[0], 0, HPP_RND_MODE_NEAR, dst, HPP_DATA_TYPE_8U);
    CHECK_STATUS(sts,"hppiConvert");
    // Wait for tasks to complete
    sts = hppWait(accel, HPP_TIME_OUT_INFINITE);
    CHECK_STATUS(sts, "hppWait");
    We use hppiConvert because hppiSobel returns destination matrix with HPP_DATA_TYPE_16S data type for source matrix with HPP_DATA_TYPE_8U type. You should check hppStatus after each call IPP Async function.
  7. Create windows and show the images, the usual way.
    imshow("image", image);
    imshow("rez", result);
    waitKey(15);
  8. Delete hpp matrices.
    sts = hppiFreeMatrix(src);
    CHECK_DEL_STATUS(sts,"hppiFreeMatrix");
    sts = hppiFreeMatrix(dst);
    CHECK_DEL_STATUS(sts,"hppiFreeMatrix");
  9. Delete virtual matrices and accelerator instance.
    if (virtMatrix)
    {
    sts = hppiDeleteVirtualMatrices(accel, virtMatrix);
    CHECK_DEL_STATUS(sts,"hppiDeleteVirtualMatrices");
    }
    if (accel)
    {
    sts = hppDeleteInstance(accel);
    CHECK_DEL_STATUS(sts, "hppDeleteInstance");
    }

Result

After compiling the code above we can execute it giving an image or video path and accelerator type as an argument. For this tutorial we use baboon.png image as input. The result is below.

How_To_Use_IPPA_Result.jpg