Added SharedMatrix
[profile/ivi/opencv.git] / samples / cpp / tutorial_code / core / ippasync / ippasync_sample.cpp
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|baboon.jpg | image file       }"
40     "{a accel     |cpu        | 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":
96              accelType == HPP_ACCEL_TYPE_OCL ? "ocl": "?";
97
98     printf("accelType %s\n", sAccel.c_str());
99
100     virtMatrix = hppiCreateVirtualMatrices(accel, 1);
101
102     for(;;)
103     {
104         cap >> image;
105         if(image.empty())
106             break;
107
108         cvtColor( image, gray, COLOR_BGR2GRAY );
109
110         result.create( image.rows, image.cols, CV_8U);
111
112         double execTime = (double)getTickCount();
113
114         //convert Mat to hppiMatrix
115         src = hpp::getHpp(gray,accel);
116         dst = hpp::getHpp(result,accel);
117
118         sts = hppiSobel(accel,src, HPP_MASK_SIZE_3X3,HPP_NORM_L1,virtMatrix[0]);
119         CHECK_STATUS(sts,"hppiSobel");
120
121         sts = hppiConvert(accel, virtMatrix[0], 0, HPP_RND_MODE_NEAR, dst, HPP_DATA_TYPE_8U);
122         CHECK_STATUS(sts,"hppiConvert");
123
124         // Wait for tasks to complete
125         sts = hppWait(accel, HPP_TIME_OUT_INFINITE);
126         CHECK_STATUS(sts, "hppWait");
127
128         execTime = ((double)getTickCount() - execTime)*1000./getTickFrequency();
129
130         printf("Time : %0.3fms\n", execTime);
131
132         imshow("image", image);
133         imshow("rez", result);
134
135         waitKey(15);
136
137         sts =  hppiFreeMatrix(src);
138         CHECK_DEL_STATUS(sts,"hppiFreeMatrix");
139
140         sts =  hppiFreeMatrix(dst);
141         CHECK_DEL_STATUS(sts,"hppiFreeMatrix");
142
143     }
144
145     if (!useCamera)
146         waitKey(0);
147
148     if (virtMatrix)
149     {
150         sts = hppiDeleteVirtualMatrices(accel, virtMatrix);
151         CHECK_DEL_STATUS(sts,"hppiDeleteVirtualMatrices");
152     }
153
154     if (accel)
155     {
156         sts = hppDeleteInstance(accel);
157         CHECK_DEL_STATUS(sts, "hppDeleteInstance");
158     }
159
160     printf("SUCCESS\n");
161
162 #else
163
164     printf("IPP Async not supported\n");
165
166 #endif
167
168     return 0;
169 }