opencv on mbed

Dependencies:   mbed

Committer:
joeverbout
Date:
Thu Mar 31 21:16:38 2016 +0000
Revision:
0:ea44dc9ed014
OpenCV on mbed attempt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeverbout 0:ea44dc9ed014 1 /*M///////////////////////////////////////////////////////////////////////////////////////
joeverbout 0:ea44dc9ed014 2 //
joeverbout 0:ea44dc9ed014 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
joeverbout 0:ea44dc9ed014 4 //
joeverbout 0:ea44dc9ed014 5 // By downloading, copying, installing or using the software you agree to this license.
joeverbout 0:ea44dc9ed014 6 // If you do not agree to this license, do not download, install,
joeverbout 0:ea44dc9ed014 7 // copy or use the software.
joeverbout 0:ea44dc9ed014 8 //
joeverbout 0:ea44dc9ed014 9 //
joeverbout 0:ea44dc9ed014 10 // License Agreement
joeverbout 0:ea44dc9ed014 11 // For Open Source Computer Vision Library
joeverbout 0:ea44dc9ed014 12 //
joeverbout 0:ea44dc9ed014 13 // Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
joeverbout 0:ea44dc9ed014 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
joeverbout 0:ea44dc9ed014 15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
joeverbout 0:ea44dc9ed014 16 // Copyright (C) 2015, Itseez Inc., all rights reserved.
joeverbout 0:ea44dc9ed014 17 // Third party copyrights are property of their respective owners.
joeverbout 0:ea44dc9ed014 18 //
joeverbout 0:ea44dc9ed014 19 // Redistribution and use in source and binary forms, with or without modification,
joeverbout 0:ea44dc9ed014 20 // are permitted provided that the following conditions are met:
joeverbout 0:ea44dc9ed014 21 //
joeverbout 0:ea44dc9ed014 22 // * Redistribution's of source code must retain the above copyright notice,
joeverbout 0:ea44dc9ed014 23 // this list of conditions and the following disclaimer.
joeverbout 0:ea44dc9ed014 24 //
joeverbout 0:ea44dc9ed014 25 // * Redistribution's in binary form must reproduce the above copyright notice,
joeverbout 0:ea44dc9ed014 26 // this list of conditions and the following disclaimer in the documentation
joeverbout 0:ea44dc9ed014 27 // and/or other materials provided with the distribution.
joeverbout 0:ea44dc9ed014 28 //
joeverbout 0:ea44dc9ed014 29 // * The name of the copyright holders may not be used to endorse or promote products
joeverbout 0:ea44dc9ed014 30 // derived from this software without specific prior written permission.
joeverbout 0:ea44dc9ed014 31 //
joeverbout 0:ea44dc9ed014 32 // This software is provided by the copyright holders and contributors "as is" and
joeverbout 0:ea44dc9ed014 33 // any express or implied warranties, including, but not limited to, the implied
joeverbout 0:ea44dc9ed014 34 // warranties of merchantability and fitness for a particular purpose are disclaimed.
joeverbout 0:ea44dc9ed014 35 // In no event shall the Intel Corporation or contributors be liable for any direct,
joeverbout 0:ea44dc9ed014 36 // indirect, incidental, special, exemplary, or consequential damages
joeverbout 0:ea44dc9ed014 37 // (including, but not limited to, procurement of substitute goods or services;
joeverbout 0:ea44dc9ed014 38 // loss of use, data, or profits; or business interruption) however caused
joeverbout 0:ea44dc9ed014 39 // and on any theory of liability, whether in contract, strict liability,
joeverbout 0:ea44dc9ed014 40 // or tort (including negligence or otherwise) arising in any way out of
joeverbout 0:ea44dc9ed014 41 // the use of this software, even if advised of the possibility of such damage.
joeverbout 0:ea44dc9ed014 42 //
joeverbout 0:ea44dc9ed014 43 //M*/
joeverbout 0:ea44dc9ed014 44
joeverbout 0:ea44dc9ed014 45 #ifndef __OPENCV_CORE_IPPASYNC_HPP__
joeverbout 0:ea44dc9ed014 46 #define __OPENCV_CORE_IPPASYNC_HPP__
joeverbout 0:ea44dc9ed014 47
joeverbout 0:ea44dc9ed014 48 #ifdef HAVE_IPP_A
joeverbout 0:ea44dc9ed014 49
joeverbout 0:ea44dc9ed014 50 #include "opencv2/core.hpp"
joeverbout 0:ea44dc9ed014 51 #include <ipp_async_op.h>
joeverbout 0:ea44dc9ed014 52 #include <ipp_async_accel.h>
joeverbout 0:ea44dc9ed014 53
joeverbout 0:ea44dc9ed014 54 namespace cv
joeverbout 0:ea44dc9ed014 55 {
joeverbout 0:ea44dc9ed014 56
joeverbout 0:ea44dc9ed014 57 namespace hpp
joeverbout 0:ea44dc9ed014 58 {
joeverbout 0:ea44dc9ed014 59
joeverbout 0:ea44dc9ed014 60 /** @addtogroup core_ipp
joeverbout 0:ea44dc9ed014 61 This section describes conversion between OpenCV and [Intel&reg; IPP Asynchronous
joeverbout 0:ea44dc9ed014 62 C/C++](http://software.intel.com/en-us/intel-ipp-preview) library. [Getting Started
joeverbout 0:ea44dc9ed014 63 Guide](http://registrationcenter.intel.com/irc_nas/3727/ipp_async_get_started.htm) help you to
joeverbout 0:ea44dc9ed014 64 install the library, configure header and library build paths.
joeverbout 0:ea44dc9ed014 65 */
joeverbout 0:ea44dc9ed014 66 //! @{
joeverbout 0:ea44dc9ed014 67
joeverbout 0:ea44dc9ed014 68 //! convert OpenCV data type to hppDataType
joeverbout 0:ea44dc9ed014 69 inline int toHppType(const int cvType)
joeverbout 0:ea44dc9ed014 70 {
joeverbout 0:ea44dc9ed014 71 int depth = CV_MAT_DEPTH(cvType);
joeverbout 0:ea44dc9ed014 72 int hppType = depth == CV_8U ? HPP_DATA_TYPE_8U :
joeverbout 0:ea44dc9ed014 73 depth == CV_16U ? HPP_DATA_TYPE_16U :
joeverbout 0:ea44dc9ed014 74 depth == CV_16S ? HPP_DATA_TYPE_16S :
joeverbout 0:ea44dc9ed014 75 depth == CV_32S ? HPP_DATA_TYPE_32S :
joeverbout 0:ea44dc9ed014 76 depth == CV_32F ? HPP_DATA_TYPE_32F :
joeverbout 0:ea44dc9ed014 77 depth == CV_64F ? HPP_DATA_TYPE_64F : -1;
joeverbout 0:ea44dc9ed014 78 CV_Assert( hppType >= 0 );
joeverbout 0:ea44dc9ed014 79 return hppType;
joeverbout 0:ea44dc9ed014 80 }
joeverbout 0:ea44dc9ed014 81
joeverbout 0:ea44dc9ed014 82 //! convert hppDataType to OpenCV data type
joeverbout 0:ea44dc9ed014 83 inline int toCvType(const int hppType)
joeverbout 0:ea44dc9ed014 84 {
joeverbout 0:ea44dc9ed014 85 int cvType = hppType == HPP_DATA_TYPE_8U ? CV_8U :
joeverbout 0:ea44dc9ed014 86 hppType == HPP_DATA_TYPE_16U ? CV_16U :
joeverbout 0:ea44dc9ed014 87 hppType == HPP_DATA_TYPE_16S ? CV_16S :
joeverbout 0:ea44dc9ed014 88 hppType == HPP_DATA_TYPE_32S ? CV_32S :
joeverbout 0:ea44dc9ed014 89 hppType == HPP_DATA_TYPE_32F ? CV_32F :
joeverbout 0:ea44dc9ed014 90 hppType == HPP_DATA_TYPE_64F ? CV_64F : -1;
joeverbout 0:ea44dc9ed014 91 CV_Assert( cvType >= 0 );
joeverbout 0:ea44dc9ed014 92 return cvType;
joeverbout 0:ea44dc9ed014 93 }
joeverbout 0:ea44dc9ed014 94
joeverbout 0:ea44dc9ed014 95 /** @brief Convert hppiMatrix to Mat.
joeverbout 0:ea44dc9ed014 96
joeverbout 0:ea44dc9ed014 97 This function allocates and initializes new matrix (if needed) that has the same size and type as
joeverbout 0:ea44dc9ed014 98 input matrix. Supports CV_8U, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F.
joeverbout 0:ea44dc9ed014 99 @param src input hppiMatrix.
joeverbout 0:ea44dc9ed014 100 @param dst output matrix.
joeverbout 0:ea44dc9ed014 101 @param accel accelerator instance (see hpp::getHpp for the list of acceleration framework types).
joeverbout 0:ea44dc9ed014 102 @param cn number of channels.
joeverbout 0:ea44dc9ed014 103 */
joeverbout 0:ea44dc9ed014 104 inline void copyHppToMat(hppiMatrix* src, Mat& dst, hppAccel accel, int cn)
joeverbout 0:ea44dc9ed014 105 {
joeverbout 0:ea44dc9ed014 106 hppDataType type;
joeverbout 0:ea44dc9ed014 107 hpp32u width, height;
joeverbout 0:ea44dc9ed014 108 hppStatus sts;
joeverbout 0:ea44dc9ed014 109
joeverbout 0:ea44dc9ed014 110 if (src == NULL)
joeverbout 0:ea44dc9ed014 111 return dst.release();
joeverbout 0:ea44dc9ed014 112
joeverbout 0:ea44dc9ed014 113 sts = hppiInquireMatrix(src, &type, &width, &height);
joeverbout 0:ea44dc9ed014 114
joeverbout 0:ea44dc9ed014 115 CV_Assert( sts == HPP_STATUS_NO_ERROR);
joeverbout 0:ea44dc9ed014 116
joeverbout 0:ea44dc9ed014 117 int matType = CV_MAKETYPE(toCvType(type), cn);
joeverbout 0:ea44dc9ed014 118
joeverbout 0:ea44dc9ed014 119 CV_Assert(width%cn == 0);
joeverbout 0:ea44dc9ed014 120
joeverbout 0:ea44dc9ed014 121 width /= cn;
joeverbout 0:ea44dc9ed014 122
joeverbout 0:ea44dc9ed014 123 dst.create((int)height, (int)width, (int)matType);
joeverbout 0:ea44dc9ed014 124
joeverbout 0:ea44dc9ed014 125 size_t newSize = (size_t)(height*(hpp32u)(dst.step));
joeverbout 0:ea44dc9ed014 126
joeverbout 0:ea44dc9ed014 127 sts = hppiGetMatrixData(accel,src,(hpp32u)(dst.step),dst.data,&newSize);
joeverbout 0:ea44dc9ed014 128
joeverbout 0:ea44dc9ed014 129 CV_Assert( sts == HPP_STATUS_NO_ERROR);
joeverbout 0:ea44dc9ed014 130 }
joeverbout 0:ea44dc9ed014 131
joeverbout 0:ea44dc9ed014 132 /** @brief Create Mat from hppiMatrix.
joeverbout 0:ea44dc9ed014 133
joeverbout 0:ea44dc9ed014 134 This function allocates and initializes the Mat that has the same size and type as input matrix.
joeverbout 0:ea44dc9ed014 135 Supports CV_8U, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F.
joeverbout 0:ea44dc9ed014 136 @param src input hppiMatrix.
joeverbout 0:ea44dc9ed014 137 @param accel accelerator instance (see hpp::getHpp for the list of acceleration framework types).
joeverbout 0:ea44dc9ed014 138 @param cn number of channels.
joeverbout 0:ea44dc9ed014 139 @sa howToUseIPPAconversion, hpp::copyHppToMat, hpp::getHpp.
joeverbout 0:ea44dc9ed014 140 */
joeverbout 0:ea44dc9ed014 141 inline Mat getMat(hppiMatrix* src, hppAccel accel, int cn)
joeverbout 0:ea44dc9ed014 142 {
joeverbout 0:ea44dc9ed014 143 Mat dst;
joeverbout 0:ea44dc9ed014 144 copyHppToMat(src, dst, accel, cn);
joeverbout 0:ea44dc9ed014 145 return dst;
joeverbout 0:ea44dc9ed014 146 }
joeverbout 0:ea44dc9ed014 147
joeverbout 0:ea44dc9ed014 148 /** @brief Create hppiMatrix from Mat.
joeverbout 0:ea44dc9ed014 149
joeverbout 0:ea44dc9ed014 150 This function allocates and initializes the hppiMatrix that has the same size and type as input
joeverbout 0:ea44dc9ed014 151 matrix, returns the hppiMatrix*.
joeverbout 0:ea44dc9ed014 152
joeverbout 0:ea44dc9ed014 153 If you want to use zero-copy for GPU you should to have 4KB aligned matrix data. See details
joeverbout 0:ea44dc9ed014 154 [hppiCreateSharedMatrix](http://software.intel.com/ru-ru/node/501697).
joeverbout 0:ea44dc9ed014 155
joeverbout 0:ea44dc9ed014 156 Supports CV_8U, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F.
joeverbout 0:ea44dc9ed014 157
joeverbout 0:ea44dc9ed014 158 @note The hppiMatrix pointer to the image buffer in system memory refers to the src.data. Control
joeverbout 0:ea44dc9ed014 159 the lifetime of the matrix and don't change its data, if there is no special need.
joeverbout 0:ea44dc9ed014 160 @param src input matrix.
joeverbout 0:ea44dc9ed014 161 @param accel accelerator instance. Supports type:
joeverbout 0:ea44dc9ed014 162 - **HPP_ACCEL_TYPE_CPU** - accelerated by optimized CPU instructions.
joeverbout 0:ea44dc9ed014 163 - **HPP_ACCEL_TYPE_GPU** - accelerated by GPU programmable units or fixed-function
joeverbout 0:ea44dc9ed014 164 accelerators.
joeverbout 0:ea44dc9ed014 165 - **HPP_ACCEL_TYPE_ANY** - any acceleration or no acceleration available.
joeverbout 0:ea44dc9ed014 166 @sa howToUseIPPAconversion, hpp::getMat
joeverbout 0:ea44dc9ed014 167 */
joeverbout 0:ea44dc9ed014 168 inline hppiMatrix* getHpp(const Mat& src, hppAccel accel)
joeverbout 0:ea44dc9ed014 169 {
joeverbout 0:ea44dc9ed014 170 int htype = toHppType(src.type());
joeverbout 0:ea44dc9ed014 171 int cn = src.channels();
joeverbout 0:ea44dc9ed014 172
joeverbout 0:ea44dc9ed014 173 CV_Assert(src.data);
joeverbout 0:ea44dc9ed014 174 hppAccelType accelType = hppQueryAccelType(accel);
joeverbout 0:ea44dc9ed014 175
joeverbout 0:ea44dc9ed014 176 if (accelType!=HPP_ACCEL_TYPE_CPU)
joeverbout 0:ea44dc9ed014 177 {
joeverbout 0:ea44dc9ed014 178 hpp32u pitch, size;
joeverbout 0:ea44dc9ed014 179 hppQueryMatrixAllocParams(accel, src.cols*cn, src.rows, htype, &pitch, &size);
joeverbout 0:ea44dc9ed014 180 if (pitch!=0 && size!=0)
joeverbout 0:ea44dc9ed014 181 if ((int)(src.data)%4096==0 && pitch==(hpp32u)(src.step))
joeverbout 0:ea44dc9ed014 182 {
joeverbout 0:ea44dc9ed014 183 return hppiCreateSharedMatrix(htype, src.cols*cn, src.rows, src.data, pitch, size);
joeverbout 0:ea44dc9ed014 184 }
joeverbout 0:ea44dc9ed014 185 }
joeverbout 0:ea44dc9ed014 186
joeverbout 0:ea44dc9ed014 187 return hppiCreateMatrix(htype, src.cols*cn, src.rows, src.data, (hpp32s)(src.step));;
joeverbout 0:ea44dc9ed014 188 }
joeverbout 0:ea44dc9ed014 189
joeverbout 0:ea44dc9ed014 190 //! @}
joeverbout 0:ea44dc9ed014 191 }}
joeverbout 0:ea44dc9ed014 192
joeverbout 0:ea44dc9ed014 193 #endif
joeverbout 0:ea44dc9ed014 194
joeverbout 0:ea44dc9ed014 195 #endif
joeverbout 0:ea44dc9ed014 196