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