Home | History | Annotate | Download | only in test
      1 /*M///////////////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
      4 //
      5 //  By downloading, copying, installing or using the software you agree to this license.
      6 //  If you do not agree to this license, do not download, install,
      7 //  copy or use the software.
      8 //
      9 //
     10 //                           License Agreement
     11 //                For Open Source Computer Vision Library
     12 //
     13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
     14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
     15 // Third party copyrights are property of their respective owners.
     16 //
     17 // Redistribution and use in source and binary forms, with or without modification,
     18 // are permitted provided that the following conditions are met:
     19 //
     20 //   * Redistribution's of source code must retain the above copyright notice,
     21 //     this list of conditions and the following disclaimer.
     22 //
     23 //   * Redistribution's in binary form must reproduce the above copyright notice,
     24 //     this list of conditions and the following disclaimer in the documentation
     25 //     and/or other materials provided with the distribution.
     26 //
     27 //   * The name of the copyright holders may not be used to endorse or promote products
     28 //     derived from this software without specific prior written permission.
     29 //
     30 // This software is provided by the copyright holders and contributors "as is" and
     31 // any express or implied warranties, including, but not limited to, the implied
     32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     33 // In no event shall the Intel Corporation or contributors be liable for any direct,
     34 // indirect, incidental, special, exemplary, or consequential damages
     35 // (including, but not limited to, procurement of substitute goods or services;
     36 // loss of use, data, or profits; or business interruption) however caused
     37 // and on any theory of liability, whether in contract, strict liability,
     38 // or tort (including negligence or otherwise) arising in any way out of
     39 // the use of this software, even if advised of the possibility of such damage.
     40 //
     41 //M*/
     42 
     43 #ifndef __OPENCV_TEST_INTERPOLATION_HPP__
     44 #define __OPENCV_TEST_INTERPOLATION_HPP__
     45 
     46 #include "opencv2/core.hpp"
     47 #include "opencv2/imgproc.hpp"
     48 
     49 template <typename T> T readVal(const cv::Mat& src, int y, int x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
     50 {
     51     if (border_type == cv::BORDER_CONSTANT)
     52         return (y >= 0 && y < src.rows && x >= 0 && x < src.cols) ? src.at<T>(y, x * src.channels() + c) : cv::saturate_cast<T>(borderVal.val[c]);
     53 
     54     return src.at<T>(cv::borderInterpolate(y, src.rows, border_type), cv::borderInterpolate(x, src.cols, border_type) * src.channels() + c);
     55 }
     56 
     57 template <typename T> struct NearestInterpolator
     58 {
     59     static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
     60     {
     61         return readVal<T>(src, int(y), int(x), c, border_type, borderVal);
     62     }
     63 };
     64 
     65 template <typename T> struct LinearInterpolator
     66 {
     67     static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
     68     {
     69         int x1 = cvFloor(x);
     70         int y1 = cvFloor(y);
     71         int x2 = x1 + 1;
     72         int y2 = y1 + 1;
     73 
     74         float res = 0;
     75 
     76         res += readVal<T>(src, y1, x1, c, border_type, borderVal) * ((x2 - x) * (y2 - y));
     77         res += readVal<T>(src, y1, x2, c, border_type, borderVal) * ((x - x1) * (y2 - y));
     78         res += readVal<T>(src, y2, x1, c, border_type, borderVal) * ((x2 - x) * (y - y1));
     79         res += readVal<T>(src, y2, x2, c, border_type, borderVal) * ((x - x1) * (y - y1));
     80 
     81         return cv::saturate_cast<T>(res);
     82     }
     83 };
     84 
     85 template <typename T> struct CubicInterpolator
     86 {
     87     static float bicubicCoeff(float x_)
     88     {
     89         float x = fabsf(x_);
     90         if (x <= 1.0f)
     91         {
     92             return x * x * (1.5f * x - 2.5f) + 1.0f;
     93         }
     94         else if (x < 2.0f)
     95         {
     96             return x * (x * (-0.5f * x + 2.5f) - 4.0f) + 2.0f;
     97         }
     98         else
     99         {
    100             return 0.0f;
    101         }
    102     }
    103 
    104     static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
    105     {
    106         const float xmin = ceilf(x - 2.0f);
    107         const float xmax = floorf(x + 2.0f);
    108 
    109         const float ymin = ceilf(y - 2.0f);
    110         const float ymax = floorf(y + 2.0f);
    111 
    112         float sum  = 0.0f;
    113         float wsum = 0.0f;
    114 
    115         for (float cy = ymin; cy <= ymax; cy += 1.0f)
    116         {
    117             for (float cx = xmin; cx <= xmax; cx += 1.0f)
    118             {
    119                 const float w = bicubicCoeff(x - cx) * bicubicCoeff(y - cy);
    120                 sum += w * readVal<T>(src, (int) floorf(cy), (int) floorf(cx), c, border_type, borderVal);
    121                 wsum += w;
    122             }
    123         }
    124 
    125         float res = (!wsum)? 0 : sum / wsum;
    126 
    127         return cv::saturate_cast<T>(res);
    128     }
    129 };
    130 
    131 #endif // __OPENCV_TEST_INTERPOLATION_HPP__
    132