00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef PIXELS_H_DEFINED
00039 #define PIXELS_H_DEFINED
00040
00041 #include "Util/MathFunctions.H"
00042 #include "Image/PixelsTypes.H"
00043 #include <cmath>
00044 #include <string>
00045
00046
00047
00048
00049
00050
00051
00052 namespace pix_helper
00053 {
00054 typedef double (unary_math_func)(double);
00055
00056 template <class P>
00057 inline P do_unary_func(P pixel, unary_math_func* func)
00058 {
00059
00060
00061
00062
00063 for (size_t i = 0; i < pixel.myDim; ++i)
00064 pixel.p[i] =
00065 clamped_convert<typename P::ScalarType>
00066 (func(static_cast<double>(pixel.p[i])));
00067
00068 return pixel;
00069 }
00070 }
00071
00072 #define PIX_FREE_MATH_FUNCTIONS(PIX, TPARAMS) \
00073 \
00074 \
00075 template <TPARAMS> inline PIX \
00076 abs(PIX A) { return pix_helper::do_unary_func(A, &::fabs); } \
00077 \
00078 template <TPARAMS> inline PIX \
00079 sqrt(PIX A) { return pix_helper::do_unary_func(A, &::sqrt); } \
00080 \
00081 template <TPARAMS> inline PIX \
00082 floor(PIX A) { return pix_helper::do_unary_func(A, &::floor); } \
00083 \
00084 template <TPARAMS> inline PIX \
00085 ceil(PIX A) { return pix_helper::do_unary_func(A, &::ceil); } \
00086 \
00087 template <TPARAMS> inline PIX \
00088 round(PIX A) { return pix_helper::do_unary_func(A, &::round); } \
00089 \
00090 template <TPARAMS> inline PIX \
00091 log(const PIX A) { return pix_helper::do_unary_func(A, &::log); } \
00092 \
00093 template <TPARAMS> inline PIX \
00094 log10(PIX A) { return pix_helper::do_unary_func(A, &::log10); } \
00095 \
00096 template <TPARAMS> inline PIX \
00097 exp(PIX A) { return pix_helper::do_unary_func(A, &::exp); } \
00098 \
00099 template <TPARAMS> inline PIX \
00100 erf(PIX A) { return pix_helper::do_unary_func(A, &::erf); } \
00101 \
00102 template <TPARAMS> inline PIX \
00103 erfc(PIX A) { return pix_helper::do_unary_func(A, &::erfc); } \
00104 \
00105 template <TPARAMS> inline PIX \
00106 sign(PIX A) { return pix_helper::do_unary_func(A, &::sign); } \
00107 \
00108 \
00109 \
00110 template <TPARAMS> \
00111 inline T max(PIX A) \
00112 { \
00113 T a = A.p[0]; \
00114 for (size_t i = 1; i < A.myDim; i++) \
00115 if (A.p[i] > a) a = A.p[i]; \
00116 \
00117 return a; \
00118 } \
00119 \
00120 template <TPARAMS> \
00121 inline T min(PIX A) \
00122 { \
00123 T a = A.p[0]; \
00124 for (size_t i = 1; i < A.myDim; i++) \
00125 if (A.p[i] < a) a = A.p[i]; \
00126 \
00127 return a; \
00128 } \
00129 \
00130 template <TPARAMS> \
00131 inline bool isFinite(PIX A) \
00132 { \
00133 for (size_t i = 0; i < A.myDim; i++) \
00134 if (!isFinite(A.p[i])) return false; \
00135 \
00136 return true; \
00137 } \
00138 \
00139 template <TPARAMS> \
00140 inline T sum(PIX A) \
00141 { \
00142 T sum = 0; \
00143 for (size_t i = 0; i < A.myDim; i++) \
00144 sum = A.p[i] + sum; \
00145 \
00146 return sum; \
00147 } \
00148 \
00149 template <TPARAMS> \
00150 inline T mean(PIX A) \
00151 { \
00152 T sum = 0; \
00153 for (size_t i = 0; i < A.myDim; i++) \
00154 { \
00155 sum = A.p[i] + sum; \
00156 } \
00157 \
00158 return clamped_convert<T>(sum/double(A.myDim)); \
00159 } \
00160 \
00161 \
00162 \
00163 template <TPARAMS> inline PIX \
00164 sin(PIX A) { return pix_helper::do_unary_func(A, &::sin); } \
00165 \
00166 template <TPARAMS> inline PIX \
00167 cos(PIX A) { return pix_helper::do_unary_func(A, &::cos); } \
00168 \
00169 template <TPARAMS> inline PIX \
00170 tan(PIX A) { return pix_helper::do_unary_func(A, &::tan); } \
00171 \
00172 template <TPARAMS> inline PIX \
00173 sec(PIX A) { return pix_helper::do_unary_func(A, &::sec); } \
00174 \
00175 template <TPARAMS> inline PIX \
00176 cosec(PIX A) { return pix_helper::do_unary_func(A, &::cosec); } \
00177 \
00178 template <TPARAMS> inline PIX \
00179 cotan(PIX A) { return pix_helper::do_unary_func(A, &::cotan); } \
00180 \
00181 \
00182 \
00183 template <TPARAMS> inline PIX \
00184 asin(PIX A) { return pix_helper::do_unary_func(A, &::asin); } \
00185 \
00186 template <TPARAMS> inline PIX \
00187 acos(PIX A) { return pix_helper::do_unary_func(A, &::acos); } \
00188 \
00189 template <TPARAMS> inline PIX \
00190 atan(PIX A) { return pix_helper::do_unary_func(A, &::atan); } \
00191 \
00192 template <TPARAMS> inline PIX \
00193 asec(PIX A) { return pix_helper::do_unary_func(A, &::asec); } \
00194 \
00195 template <TPARAMS> inline PIX \
00196 acosec(PIX A) { return pix_helper::do_unary_func(A, &::acosec); } \
00197 \
00198 template <TPARAMS> inline PIX \
00199 acotan(PIX A) { return pix_helper::do_unary_func(A, &::acotan); } \
00200 \
00201 \
00202 \
00203 template <TPARAMS> inline PIX \
00204 sinh(PIX A) { return pix_helper::do_unary_func(A, &::sinh); } \
00205 \
00206 template <TPARAMS> inline PIX \
00207 cosh(PIX A) { return pix_helper::do_unary_func(A, &::cosh); } \
00208 \
00209 template <TPARAMS> inline PIX \
00210 tanh(PIX A) { return pix_helper::do_unary_func(A, &::tanh); } \
00211 \
00212 template <TPARAMS> inline PIX \
00213 sech(PIX A) { return pix_helper::do_unary_func(A, &::sech); } \
00214 \
00215 template <TPARAMS> inline PIX \
00216 cosech(PIX A) { return pix_helper::do_unary_func(A, &::cosech); } \
00217 \
00218 template <TPARAMS> inline PIX \
00219 cotanh(PIX A) { return pix_helper::do_unary_func(A, &::cotanh); } \
00220 \
00221 \
00222 \
00223 template <TPARAMS> inline PIX \
00224 asinh(PIX A) { return pix_helper::do_unary_func(A, &::asinh); } \
00225 \
00226 template <TPARAMS> inline PIX \
00227 acosh(PIX A) { return pix_helper::do_unary_func(A, &::acosh); } \
00228 \
00229 template <TPARAMS> inline PIX \
00230 atanh(PIX A) { return pix_helper::do_unary_func(A, &::atanh); } \
00231 \
00232 template <TPARAMS> inline PIX \
00233 asech(PIX A) { return pix_helper::do_unary_func(A, &::asech); } \
00234 \
00235 template <TPARAMS> inline PIX \
00236 acosech(PIX A) { return pix_helper::do_unary_func(A, &::acosech); } \
00237 \
00238 template <TPARAMS> inline PIX \
00239 acotanh(PIX A) { return pix_helper::do_unary_func(A, &::acotanh); } \
00240 \
00241 \
00242 \
00243 template <TPARAMS> inline PIX \
00244 maxmerge(PIX A, const PIX B) \
00245 { \
00246 for (size_t i = 0; i < A.myDim; i++) \
00247 if (A.p[i] < B.p[i]) A.p[i] = B.p[i]; \
00248 \
00249 return A; \
00250 } \
00251 \
00252 template <TPARAMS> inline PIX \
00253 minmerge(PIX A, const PIX B) \
00254 { \
00255 for (size_t i = 0; i < A.myDim; i++) \
00256 if (A.p[i] > B.p[i]) A.p[i] = B.p[i]; \
00257 \
00258 return A; \
00259 } \
00260 \
00261 template <TPARAMS> inline PIX \
00262 pow(PIX A, const T val) \
00263 { \
00264 for (size_t i = 0; i < A.myDim; i++) \
00265 A.p[i] = clamped_convert<T>(pow(A.p[i], val)); \
00266 \
00267 return A; \
00268 } \
00269 \
00270 template <TPARAMS> inline PIX \
00271 pow(PIX A, const PIX B) \
00272 { \
00273 for (size_t i = 0; i < A.myDim; i++) \
00274 A.p[i] = clamped_convert<T>(pow(A.p[i], B.p[i])); \
00275 \
00276 return A; \
00277 } \
00278 \
00279 template <TPARAMS> inline PIX \
00280 rand(PIX A,const int low, const int high) \
00281 { \
00282 for (size_t i = 0; i < A.myDim; i++) \
00283 A.p[i] = clamped_convert<T>(1+(high*rand() \
00284 /(RAND_MAX+low))); \
00285 \
00286 return A; \
00287 } \
00288 \
00289 template <TPARAMS> inline PIX \
00290 logN(PIX A, const T n) \
00291 { \
00292 for (size_t i = 0; i < A.myDim; i++) \
00293 A.p[i] = logN(A.p[i], n); \
00294 \
00295 return A; \
00296 } \
00297 \
00298 template <TPARAMS> inline PIX \
00299 logsig(PIX A, const T a, const T b) \
00300 { \
00301 for (size_t i = 0; i < A.myDim; i++) \
00302 A.p[i] = logsig(A.p[i], a, b); \
00303 \
00304 return A; \
00305 } \
00306 \
00307 template <TPARAMS> inline PIX \
00308 tansig(PIX A) \
00309 { \
00310 for (size_t i = 0; i < A.myDim; i++) \
00311 A.p[i] = tansig(A.p[i]); \
00312 \
00313 return A; \
00314 } \
00315 \
00316 template <TPARAMS> inline PIX \
00317 saturate(PIX A, const T n) \
00318 { \
00319 for (size_t i = 0; i < A.myDim; i++) \
00320 A.p[i] = saturate(A.p[i], n); \
00321 \
00322 return A; \
00323 } \
00324 \
00325 template <TPARAMS> inline PIX \
00326 poly(PIX A, const T* pn, const size_t n) \
00327 { \
00328 PIX B(0); \
00329 for (size_t i = 0; i < A.myDim; ++i) \
00330 for (size_t j = 0; j < n; ++j) \
00331 B.p[i] += clamped_convert<T>(double(pn[j]) * pow(A.p[i],j)); \
00332 \
00333 return B; \
00334 } \
00335 \
00336 template <TPARAMS> inline PIX \
00337 gauss(PIX A, const PIX Mu, const PIX Std) \
00338 { \
00339 for (size_t i = 0; i < A.myDim; i++) \
00340 A.p[i] = gauss(A.p[i], Mu.p[i], Std.p[i]); \
00341 \
00342 return A; \
00343 } \
00344 \
00345 template <TPARAMS> inline PIX \
00346 gauss(PIX A, const T Mu, const T Std) \
00347 { \
00348 for (size_t i = 0; i < A.myDim; i++) \
00349 A.p[i] = gauss(A.p[i], Mu, Std); \
00350 \
00351 return A; \
00352 }
00353
00354
00355 PIX_FREE_MATH_FUNCTIONS(PixRGB<T>, class T)
00356 PIX_FREE_MATH_FUNCTIONS(PixHSV<T>, class T)
00357 PIX_FREE_MATH_FUNCTIONS(PixJpegYUV<T>, class T)
00358 PIX_FREE_MATH_FUNCTIONS(PixVideoYUV<T>, class T)
00359 PIX_FREE_MATH_FUNCTIONS(PixYIQ<T>, class T)
00360 PIX_FREE_MATH_FUNCTIONS(PixH2SV1<T>, class T)
00361 PIX_FREE_MATH_FUNCTIONS(PixH2SV2<T>, class T)
00362 PIX_FREE_MATH_FUNCTIONS(PixH2SV3<T>, class T)
00363
00364 #define LIST_2(p1, p2) p1, p2
00365 PIX_FREE_MATH_FUNCTIONS(PixHyper<LIST_2(T, dim)>, LIST_2(class T, size_t dim))
00366 #undef LIST_2
00367
00368 #undef PIX_FREE_MATH_FUNCTIONS
00369
00370
00371
00372
00373 std::string convertToString(const PixRGB<byte>& val);
00374
00375
00376
00377 void convertFromString(const std::string& str, PixRGB<byte>& val);
00378
00379
00380
00381 std::string convertToString(const PixHSV<byte>& val);
00382
00383
00384 void convertFromString(const std::string& str, PixHSV<byte>& val);
00385
00386
00387
00388 std::string convertToString(const PixDKL<byte>& val);
00389
00390
00391 void convertFromString(const std::string& str, PixDKL<byte>& val);
00392
00393 #endif