Commit bfdc06cb authored by Alexander Alekhin's avatar Alexander Alekhin
parent 91e16573
if(IOS OR WINRT)
ocv_module_disable(superres)
endif()
set(the_description "Super Resolution")
if(HAVE_CUDA)
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wshadow)
endif()
ocv_define_module(superres opencv_imgproc opencv_video
OPTIONAL opencv_videoio opencv_cudaarithm opencv_cudafilters opencv_cudawarping opencv_cudaimgproc opencv_cudaoptflow opencv_cudacodec
WRAP python)
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef OPENCV_SUPERRES_HPP
#define OPENCV_SUPERRES_HPP
#include "opencv2/core.hpp"
#include "opencv2/superres/optical_flow.hpp"
/**
@defgroup superres Super Resolution
The Super Resolution module contains a set of functions and classes that can be used to solve the
problem of resolution enhancement. There are a few methods implemented, most of them are described in
the papers @cite Farsiu03 and @cite Mitzel09 .
*/
namespace cv
{
namespace superres
{
//! @addtogroup superres
//! @{
class CV_EXPORTS FrameSource
{
public:
virtual ~FrameSource();
virtual void nextFrame(OutputArray frame) = 0;
virtual void reset() = 0;
};
CV_EXPORTS Ptr<FrameSource> createFrameSource_Empty();
CV_EXPORTS Ptr<FrameSource> createFrameSource_Video(const String& fileName);
CV_EXPORTS Ptr<FrameSource> createFrameSource_Video_CUDA(const String& fileName);
CV_EXPORTS Ptr<FrameSource> createFrameSource_Camera(int deviceId = 0);
/** @brief Base class for Super Resolution algorithms.
The class is only used to define the common interface for the whole family of Super Resolution
algorithms.
*/
class CV_EXPORTS SuperResolution : public cv::Algorithm, public FrameSource
{
public:
/** @brief Set input frame source for Super Resolution algorithm.
@param frameSource Input frame source
*/
void setInput(const Ptr<FrameSource>& frameSource);
/** @brief Process next frame from input and return output result.
@param frame Output result
*/
void nextFrame(OutputArray frame) CV_OVERRIDE;
void reset() CV_OVERRIDE;
/** @brief Clear all inner buffers.
*/
virtual void collectGarbage();
//! @brief Scale factor
/** @see setScale */
virtual int getScale() const = 0;
/** @copybrief getScale @see getScale */
virtual void setScale(int val) = 0;
//! @brief Iterations count
/** @see setIterations */
virtual int getIterations() const = 0;
/** @copybrief getIterations @see getIterations */
virtual void setIterations(int val) = 0;
//! @brief Asymptotic value of steepest descent method
/** @see setTau */
virtual double getTau() const = 0;
/** @copybrief getTau @see getTau */
virtual void setTau(double val) = 0;
//! @brief Weight parameter to balance data term and smoothness term
/** @see setLabmda */
virtual double getLabmda() const = 0;
/** @copybrief getLabmda @see getLabmda */
virtual void setLabmda(double val) = 0;
//! @brief Parameter of spacial distribution in Bilateral-TV
/** @see setAlpha */
virtual double getAlpha() const = 0;
/** @copybrief getAlpha @see getAlpha */
virtual void setAlpha(double val) = 0;
//! @brief Kernel size of Bilateral-TV filter
/** @see setKernelSize */
virtual int getKernelSize() const = 0;
/** @copybrief getKernelSize @see getKernelSize */
virtual void setKernelSize(int val) = 0;
//! @brief Gaussian blur kernel size
/** @see setBlurKernelSize */
virtual int getBlurKernelSize() const = 0;
/** @copybrief getBlurKernelSize @see getBlurKernelSize */
virtual void setBlurKernelSize(int val) = 0;
//! @brief Gaussian blur sigma
/** @see setBlurSigma */
virtual double getBlurSigma() const = 0;
/** @copybrief getBlurSigma @see getBlurSigma */
virtual void setBlurSigma(double val) = 0;
//! @brief Radius of the temporal search area
/** @see setTemporalAreaRadius */
virtual int getTemporalAreaRadius() const = 0;
/** @copybrief getTemporalAreaRadius @see getTemporalAreaRadius */
virtual void setTemporalAreaRadius(int val) = 0;
//! @brief Dense optical flow algorithm
/** @see setOpticalFlow */
virtual Ptr<cv::superres::DenseOpticalFlowExt> getOpticalFlow() const = 0;
/** @copybrief getOpticalFlow @see getOpticalFlow */
virtual void setOpticalFlow(const Ptr<cv::superres::DenseOpticalFlowExt> &val) = 0;
protected:
SuperResolution();
virtual void initImpl(Ptr<FrameSource>& frameSource) = 0;
virtual void processImpl(Ptr<FrameSource>& frameSource, OutputArray output) = 0;
bool isUmat_;
private:
Ptr<FrameSource> frameSource_;
bool firstCall_;
};
/** @brief Create Bilateral TV-L1 Super Resolution.
This class implements Super Resolution algorithm described in the papers @cite Farsiu03 and
@cite Mitzel09 .
Here are important members of the class that control the algorithm, which you can set after
constructing the class instance:
- **int scale** Scale factor.
- **int iterations** Iteration count.
- **double tau** Asymptotic value of steepest descent method.
- **double lambda** Weight parameter to balance data term and smoothness term.
- **double alpha** Parameter of spacial distribution in Bilateral-TV.
- **int btvKernelSize** Kernel size of Bilateral-TV filter.
- **int blurKernelSize** Gaussian blur kernel size.
- **double blurSigma** Gaussian blur sigma.
- **int temporalAreaRadius** Radius of the temporal search area.
- **Ptr\<DenseOpticalFlowExt\> opticalFlow** Dense optical flow algorithm.
*/
CV_EXPORTS Ptr<SuperResolution> createSuperResolution_BTVL1();
CV_EXPORTS Ptr<SuperResolution> createSuperResolution_BTVL1_CUDA();
//! @} superres
}
}
#endif // OPENCV_SUPERRES_HPP
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef OPENCV_SUPERRES_OPTICAL_FLOW_HPP
#define OPENCV_SUPERRES_OPTICAL_FLOW_HPP
#include "opencv2/core.hpp"
namespace cv
{
namespace superres
{
//! @addtogroup superres
//! @{
class CV_EXPORTS DenseOpticalFlowExt : public cv::Algorithm
{
public:
virtual void calc(InputArray frame0, InputArray frame1, OutputArray flow1, OutputArray flow2 = noArray()) = 0;
virtual void collectGarbage() = 0;
};
class CV_EXPORTS FarnebackOpticalFlow : public virtual DenseOpticalFlowExt
{
public:
/** @see setPyrScale */
virtual double getPyrScale() const = 0;
/** @copybrief getPyrScale @see getPyrScale */
virtual void setPyrScale(double val) = 0;
/** @see setLevelsNumber */
virtual int getLevelsNumber() const = 0;
/** @copybrief getLevelsNumber @see getLevelsNumber */
virtual void setLevelsNumber(int val) = 0;
/** @see setWindowSize */
virtual int getWindowSize() const = 0;
/** @copybrief getWindowSize @see getWindowSize */
virtual void setWindowSize(int val) = 0;
/** @see setIterations */
virtual int getIterations() const = 0;
/** @copybrief getIterations @see getIterations */
virtual void setIterations(int val) = 0;
/** @see setPolyN */
virtual int getPolyN() const = 0;
/** @copybrief getPolyN @see getPolyN */
virtual void setPolyN(int val) = 0;
/** @see setPolySigma */
virtual double getPolySigma() const = 0;
/** @copybrief getPolySigma @see getPolySigma */
virtual void setPolySigma(double val) = 0;
/** @see setFlags */
virtual int getFlags() const = 0;
/** @copybrief getFlags @see getFlags */
virtual void setFlags(int val) = 0;
};
CV_EXPORTS Ptr<FarnebackOpticalFlow> createOptFlow_Farneback();
CV_EXPORTS Ptr<FarnebackOpticalFlow> createOptFlow_Farneback_CUDA();
// CV_EXPORTS Ptr<DenseOpticalFlowExt> createOptFlow_Simple();
class CV_EXPORTS DualTVL1OpticalFlow : public virtual DenseOpticalFlowExt
{
public:
/** @see setTau */
virtual double getTau() const = 0;
/** @copybrief getTau @see getTau */
virtual void setTau(double val) = 0;
/** @see setLambda */
virtual double getLambda() const = 0;
/** @copybrief getLambda @see getLambda */
virtual void setLambda(double val) = 0;
/** @see setTheta */
virtual double getTheta() const = 0;
/** @copybrief getTheta @see getTheta */
virtual void setTheta(double val) = 0;
/** @see setScalesNumber */
virtual int getScalesNumber() const = 0;
/** @copybrief getScalesNumber @see getScalesNumber */
virtual void setScalesNumber(int val) = 0;
/** @see setWarpingsNumber */
virtual int getWarpingsNumber() const = 0;
/** @copybrief getWarpingsNumber @see getWarpingsNumber */
virtual void setWarpingsNumber(int val) = 0;
/** @see setEpsilon */
virtual double getEpsilon() const = 0;
/** @copybrief getEpsilon @see getEpsilon */
virtual void setEpsilon(double val) = 0;
/** @see setIterations */
virtual int getIterations() const = 0;
/** @copybrief getIterations @see getIterations */
virtual void setIterations(int val) = 0;
/** @see setUseInitialFlow */
virtual bool getUseInitialFlow() const = 0;
/** @copybrief getUseInitialFlow @see getUseInitialFlow */
virtual void setUseInitialFlow(bool val) = 0;
};
CV_EXPORTS Ptr<DualTVL1OpticalFlow> createOptFlow_DualTVL1();
CV_EXPORTS Ptr<DualTVL1OpticalFlow> createOptFlow_DualTVL1_CUDA();
class CV_EXPORTS BroxOpticalFlow : public virtual DenseOpticalFlowExt
{
public:
//! @brief Flow smoothness
/** @see setAlpha */
virtual double getAlpha() const = 0;
/** @copybrief getAlpha @see getAlpha */
virtual void setAlpha(double val) = 0;
//! @brief Gradient constancy importance
/** @see setGamma */
virtual double getGamma() const = 0;
/** @copybrief getGamma @see getGamma */
virtual void setGamma(double val) = 0;
//! @brief Pyramid scale factor
/** @see setScaleFactor */
virtual double getScaleFactor() const = 0;
/** @copybrief getScaleFactor @see getScaleFactor */
virtual void setScaleFactor(double val) = 0;
//! @brief Number of lagged non-linearity iterations (inner loop)
/** @see setInnerIterations */
virtual int getInnerIterations() const = 0;
/** @copybrief getInnerIterations @see getInnerIterations */
virtual void setInnerIterations(int val) = 0;
//! @brief Number of warping iterations (number of pyramid levels)
/** @see setOuterIterations */
virtual int getOuterIterations() const = 0;
/** @copybrief getOuterIterations @see getOuterIterations */
virtual void setOuterIterations(int val) = 0;
//! @brief Number of linear system solver iterations
/** @see setSolverIterations */
virtual int getSolverIterations() const = 0;
/** @copybrief getSolverIterations @see getSolverIterations */
virtual void setSolverIterations(int val) = 0;
};
CV_EXPORTS Ptr<BroxOpticalFlow> createOptFlow_Brox_CUDA();
class PyrLKOpticalFlow : public virtual DenseOpticalFlowExt
{
public:
/** @see setWindowSize */
virtual int getWindowSize() const = 0;
/** @copybrief getWindowSize @see getWindowSize */
virtual void setWindowSize(int val) = 0;
/** @see setMaxLevel */
virtual int getMaxLevel() const = 0;
/** @copybrief getMaxLevel @see getMaxLevel */
virtual void setMaxLevel(int val) = 0;
/** @see setIterations */
virtual int getIterations() const = 0;
/** @copybrief getIterations @see getIterations */
virtual void setIterations(int val) = 0;
};
CV_EXPORTS Ptr<PyrLKOpticalFlow> createOptFlow_PyrLK_CUDA();
//! @}
}
}
#endif // OPENCV_SUPERRES_OPTICAL_FLOW_HPP
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "perf_precomp.hpp"
using namespace perf;
static const char * impls[] = {
#ifdef HAVE_CUDA
"cuda",
#endif
"plain"
};
#if defined(HAVE_HPX)
#include <hpx/hpx_main.hpp>
#endif
CV_PERF_TEST_MAIN_WITH_IMPLS(superres, impls, printCudaInfo())
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_PERF_PRECOMP_HPP__
#define __OPENCV_PERF_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/ts/cuda_perf.hpp"
#include "opencv2/superres.hpp"
#include "opencv2/superres/optical_flow.hpp"
#endif
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "perf_precomp.hpp"
#include "opencv2/ts/ocl_perf.hpp"
namespace opencv_test
{
using namespace perf;
using namespace cv::superres;
using namespace cv::cuda;
namespace
{
class OneFrameSource_CPU : public FrameSource
{
public:
explicit OneFrameSource_CPU(const Mat& frame) : frame_(frame) {}
void nextFrame(OutputArray frame)
{
frame.getMatRef() = frame_;
}
void reset()
{
}
private:
Mat frame_;
};
class OneFrameSource_CUDA : public FrameSource
{
public:
explicit OneFrameSource_CUDA(const GpuMat& frame) : frame_(frame) {}
void nextFrame(OutputArray frame)
{
frame.getGpuMatRef() = frame_;
}
void reset()
{
}
private:
GpuMat frame_;
};
class ZeroOpticalFlow : public DenseOpticalFlowExt
{
public:
virtual void calc(InputArray frame0, InputArray, OutputArray flow1, OutputArray flow2)
{
cv::Size size = frame0.size();
if (!flow2.needed())
{
flow1.create(size, CV_32FC2);
flow1.setTo(cv::Scalar::all(0));
}
else
{
flow1.create(size, CV_32FC1);
flow2.create(size, CV_32FC1);
flow1.setTo(cv::Scalar::all(0));
flow2.setTo(cv::Scalar::all(0));
}
}
virtual void collectGarbage()
{
}
};
} // namespace
PERF_TEST_P(Size_MatType, SuperResolution_BTVL1,
Combine(Values(szSmall64, szSmall128),
Values(MatType(CV_8UC1), MatType(CV_8UC3))))
{
declare.time(5 * 60);
const Size size = get<0>(GetParam());
const int type = get<1>(GetParam());
Mat frame(size, type);
declare.in(frame, WARMUP_RNG);
const int scale = 2;
const int iterations = 50;
const int temporalAreaRadius = 1;
Ptr<DenseOpticalFlowExt> opticalFlow(new ZeroOpticalFlow);
if (PERF_RUN_CUDA())
{
Ptr<SuperResolution> superRes = createSuperResolution_BTVL1_CUDA();
superRes->setScale(scale);
superRes->setIterations(iterations);
superRes->setTemporalAreaRadius(temporalAreaRadius);
superRes->setOpticalFlow(opticalFlow);
superRes->setInput(makePtr<OneFrameSource_CUDA>(GpuMat(frame)));
GpuMat dst;
superRes->nextFrame(dst);
TEST_CYCLE_N(10) superRes->nextFrame(dst);
CUDA_SANITY_CHECK(dst, 2);
}
else
{
Ptr<SuperResolution> superRes = createSuperResolution_BTVL1();
superRes->setScale(scale);
superRes->setIterations(iterations);
superRes->setTemporalAreaRadius(temporalAreaRadius);
superRes->setOpticalFlow(opticalFlow);
superRes->setInput(makePtr<OneFrameSource_CPU>(frame));
Mat dst;
superRes->nextFrame(dst);
TEST_CYCLE_N(10) superRes->nextFrame(dst);
CPU_SANITY_CHECK(dst);
}
}
#ifdef HAVE_OPENCL
namespace ocl {
typedef Size_MatType SuperResolution_BTVL1;
OCL_PERF_TEST_P(SuperResolution_BTVL1 ,BTVL1,
Combine(Values(szSmall64, szSmall128),
Values(MatType(CV_8UC1), MatType(CV_8UC3))))
{
Size_MatType_t params = GetParam();
const Size size = get<0>(params);
const int type = get<1>(params);
Mat frame(size, type);
UMat dst(1, 1, 0);
declare.in(frame, WARMUP_RNG);
const int scale = 2;
const int iterations = 50;
const int temporalAreaRadius = 1;
Ptr<DenseOpticalFlowExt> opticalFlow(new ZeroOpticalFlow);
Ptr<SuperResolution> superRes = createSuperResolution_BTVL1();
superRes->setScale(scale);
superRes->setIterations(iterations);
superRes->setTemporalAreaRadius(temporalAreaRadius);
superRes->setOpticalFlow(opticalFlow);
superRes->setInput(makePtr<OneFrameSource_CPU>(frame));
// skip first frame
superRes->nextFrame(dst);
OCL_TEST_CYCLE_N(10) superRes->nextFrame(dst);
SANITY_CHECK_NOTHING();
}
} // namespace ocl
#endif // HAVE_OPENCL
} // namespace
This diff is collapsed.
This diff is collapsed.
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "opencv2/opencv_modules.hpp"
#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING) && defined(HAVE_OPENCV_CUDAFILTERS)
#include "opencv2/core/cuda/common.hpp"
#include "opencv2/core/cuda/transform.hpp"
#include "opencv2/core/cuda/vec_traits.hpp"
#include "opencv2/core/cuda/vec_math.hpp"
using namespace cv::cuda;
using namespace cv::cuda::device;
namespace btv_l1_cudev
{
void buildMotionMaps(PtrStepSzf forwardMotionX, PtrStepSzf forwardMotionY,
PtrStepSzf backwardMotionX, PtrStepSzf bacwardMotionY,
PtrStepSzf forwardMapX, PtrStepSzf forwardMapY,
PtrStepSzf backwardMapX, PtrStepSzf backwardMapY);
template <int cn>
void upscale(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);
void diffSign(PtrStepSzf src1, PtrStepSzf src2, PtrStepSzf dst, cudaStream_t stream);
void loadBtvWeights(const float* weights, size_t count);
template <int cn> void calcBtvRegularization(PtrStepSzb src, PtrStepSzb dst, int ksize);
}
namespace btv_l1_cudev
{
__global__ void buildMotionMapsKernel(const PtrStepSzf forwardMotionX, const PtrStepf forwardMotionY,
PtrStepf backwardMotionX, PtrStepf backwardMotionY,
PtrStepf forwardMapX, PtrStepf forwardMapY,
PtrStepf backwardMapX, PtrStepf backwardMapY)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= forwardMotionX.cols || y >= forwardMotionX.rows)
return;
const float fx = forwardMotionX(y, x);
const float fy = forwardMotionY(y, x);
const float bx = backwardMotionX(y, x);
const float by = backwardMotionY(y, x);
forwardMapX(y, x) = x + bx;
forwardMapY(y, x) = y + by;
backwardMapX(y, x) = x + fx;
backwardMapY(y, x) = y + fy;
}
void buildMotionMaps(PtrStepSzf forwardMotionX, PtrStepSzf forwardMotionY,
PtrStepSzf backwardMotionX, PtrStepSzf bacwardMotionY,
PtrStepSzf forwardMapX, PtrStepSzf forwardMapY,
PtrStepSzf backwardMapX, PtrStepSzf backwardMapY)
{
const dim3 block(32, 8);
const dim3 grid(divUp(forwardMapX.cols, block.x), divUp(forwardMapX.rows, block.y));
buildMotionMapsKernel<<<grid, block>>>(forwardMotionX, forwardMotionY,
backwardMotionX, bacwardMotionY,
forwardMapX, forwardMapY,
backwardMapX, backwardMapY);
cudaSafeCall( cudaGetLastError() );
cudaSafeCall( cudaDeviceSynchronize() );
}
template <typename T>
__global__ void upscaleKernel(const PtrStepSz<T> src, PtrStep<T> dst, const int scale)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= src.cols || y >= src.rows)
return;
dst(y * scale, x * scale) = src(y, x);
}
template <int cn>
void upscale(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream)
{
typedef typename TypeVec<float, cn>::vec_type src_t;
const dim3 block(32, 8);
const dim3 grid(divUp(src.cols, block.x), divUp(src.rows, block.y));
upscaleKernel<src_t><<<grid, block, 0, stream>>>((PtrStepSz<src_t>) src, (PtrStepSz<src_t>) dst, scale);
cudaSafeCall( cudaGetLastError() );
if (stream == 0)
cudaSafeCall( cudaDeviceSynchronize() );
}
template void upscale<1>(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);
template void upscale<3>(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);
template void upscale<4>(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);
__device__ __forceinline__ float diffSign(float a, float b)
{
return a > b ? 1.0f : a < b ? -1.0f : 0.0f;
}
__device__ __forceinline__ float3 diffSign(const float3& a, const float3& b)
{
return make_float3(
a.x > b.x ? 1.0f : a.x < b.x ? -1.0f : 0.0f,
a.y > b.y ? 1.0f : a.y < b.y ? -1.0f : 0.0f,
a.z > b.z ? 1.0f : a.z < b.z ? -1.0f : 0.0f
);
}
__device__ __forceinline__ float4 diffSign(const float4& a, const float4& b)
{
return make_float4(
a.x > b.x ? 1.0f : a.x < b.x ? -1.0f : 0.0f,
a.y > b.y ? 1.0f : a.y < b.y ? -1.0f : 0.0f,
a.z > b.z ? 1.0f : a.z < b.z ? -1.0f : 0.0f,
0.0f
);
}
struct DiffSign : binary_function<float, float, float>
{
__device__ __forceinline__ float operator ()(float a, float b) const
{
return diffSign(a, b);
}
};
}
namespace cv { namespace cuda { namespace device
{
template <> struct TransformFunctorTraits<btv_l1_cudev::DiffSign> : DefaultTransformFunctorTraits<btv_l1_cudev::DiffSign>
{
enum { smart_block_dim_y = 8 };
enum { smart_shift = 4 };
};
}}}
namespace btv_l1_cudev
{
void diffSign(PtrStepSzf src1, PtrStepSzf src2, PtrStepSzf dst, cudaStream_t stream)
{
transform(src1, src2, dst, DiffSign(), WithOutMask(), stream);
}
__constant__ float c_btvRegWeights[16*16];
template <typename T>
__global__ void calcBtvRegularizationKernel(const PtrStepSz<T> src, PtrStep<T> dst, const int ksize)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x + ksize;
const int y = blockIdx.y * blockDim.y + threadIdx.y + ksize;
if (y >= src.rows - ksize || x >= src.cols - ksize)
return;
const T srcVal = src(y, x);
T dstVal = VecTraits<T>::all(0);
for (int m = 0, count = 0; m <= ksize; ++m)
{
for (int l = ksize; l + m >= 0; --l, ++count)
dstVal = dstVal + c_btvRegWeights[count] * (diffSign(srcVal, src(y + m, x + l)) - diffSign(src(y - m, x - l), srcVal));
}
dst(y, x) = dstVal;
}
void loadBtvWeights(const float* weights, size_t count)
{
cudaSafeCall( cudaMemcpyToSymbol(c_btvRegWeights, weights, count * sizeof(float)) );
}
template <int cn>
void calcBtvRegularization(PtrStepSzb src, PtrStepSzb dst, int ksize)
{
typedef typename TypeVec<float, cn>::vec_type src_t;
const dim3 block(32, 8);
const dim3 grid(divUp(src.cols, block.x), divUp(src.rows, block.y));
calcBtvRegularizationKernel<src_t><<<grid, block>>>((PtrStepSz<src_t>) src, (PtrStepSz<src_t>) dst, ksize);
cudaSafeCall( cudaGetLastError() );
cudaSafeCall( cudaDeviceSynchronize() );
}
template void calcBtvRegularization<1>(PtrStepSzb src, PtrStepSzb dst, int ksize);
template void calcBtvRegularization<3>(PtrStepSzb src, PtrStepSzb dst, int ksize);
template void calcBtvRegularization<4>(PtrStepSzb src, PtrStepSzb dst, int ksize);
}
#endif
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
using namespace cv;
using namespace cv::cuda;
using namespace cv::superres;
using namespace cv::superres::detail;
cv::superres::FrameSource::~FrameSource()
{
}
//////////////////////////////////////////////////////
// EmptyFrameSource
namespace
{
class EmptyFrameSource : public FrameSource
{
public:
void nextFrame(OutputArray frame) CV_OVERRIDE;
void reset() CV_OVERRIDE;
};
void EmptyFrameSource::nextFrame(OutputArray frame)
{
frame.release();
}
void EmptyFrameSource::reset()
{
}
}
Ptr<FrameSource> cv::superres::createFrameSource_Empty()
{
return makePtr<EmptyFrameSource>();
}
//////////////////////////////////////////////////////
// VideoFrameSource & CameraFrameSource
#ifndef HAVE_OPENCV_VIDEOIO
Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)
{
CV_UNUSED(fileName);
CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
return Ptr<FrameSource>();
}
Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
{
CV_UNUSED(deviceId);
CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
return Ptr<FrameSource>();
}
#else // HAVE_OPENCV_VIDEOIO
namespace
{
class CaptureFrameSource : public FrameSource
{
public:
void nextFrame(OutputArray frame) CV_OVERRIDE;
protected:
VideoCapture vc_;
private:
Mat frame_;
};
void CaptureFrameSource::nextFrame(OutputArray _frame)
{
if (_frame.kind() == _InputArray::MAT)
vc_ >> _frame.getMatRef();
else if(_frame.kind() == _InputArray::CUDA_GPU_MAT)
{
vc_ >> frame_;
arrCopy(frame_, _frame);
}
else if (_frame.isUMat())
vc_ >> *(UMat *)_frame.getObj();
else
{
// should never get here
CV_Error(Error::StsBadArg, "Failed to detect input frame kind" );
}
}
class VideoFrameSource : public CaptureFrameSource
{
public:
VideoFrameSource(const String& fileName);
void reset() CV_OVERRIDE;
private:
String fileName_;
};
VideoFrameSource::VideoFrameSource(const String& fileName) : fileName_(fileName)
{
reset();
}
void VideoFrameSource::reset()
{
vc_.release();
vc_.open(fileName_);
CV_Assert( vc_.isOpened() );
}
class CameraFrameSource : public CaptureFrameSource
{
public:
CameraFrameSource(int deviceId);
void reset() CV_OVERRIDE;
private:
int deviceId_;
};
CameraFrameSource::CameraFrameSource(int deviceId) : deviceId_(deviceId)
{
reset();
}
void CameraFrameSource::reset()
{
vc_.release();
vc_.open(deviceId_);
CV_Assert( vc_.isOpened() );
}
}
Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)
{
return makePtr<VideoFrameSource>(fileName);
}
Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
{
return makePtr<CameraFrameSource>(deviceId);
}
#endif // HAVE_OPENCV_VIDEOIO
//////////////////////////////////////////////////////
// VideoFrameSource_CUDA
#ifndef HAVE_OPENCV_CUDACODEC
Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)
{
CV_UNUSED(fileName);
CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
}
#else // HAVE_OPENCV_CUDACODEC
namespace
{
class VideoFrameSource_CUDA : public FrameSource
{
public:
VideoFrameSource_CUDA(const String& fileName);
void nextFrame(OutputArray frame);
void reset();
private:
String fileName_;
Ptr<cudacodec::VideoReader> reader_;
GpuMat frame_;
};
VideoFrameSource_CUDA::VideoFrameSource_CUDA(const String& fileName) : fileName_(fileName)
{
reset();
}
void VideoFrameSource_CUDA::nextFrame(OutputArray _frame)
{
if (_frame.kind() == _InputArray::CUDA_GPU_MAT)
{
bool res = reader_->nextFrame(_frame.getGpuMatRef());
if (!res)
_frame.release();
}
else
{
bool res = reader_->nextFrame(frame_);
if (!res)
_frame.release();
else
arrCopy(frame_, _frame);
}
}
void VideoFrameSource_CUDA::reset()
{
reader_ = cudacodec::createVideoReader(fileName_);
}
}
Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)
{
return makePtr<VideoFrameSource_CUDA>(fileName);
}
#endif // HAVE_OPENCV_CUDACODEC
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
using namespace cv;
using namespace cv::cuda;
Mat cv::superres::arrGetMat(InputArray arr, Mat& buf)
{
switch (arr.kind())
{
case _InputArray::CUDA_GPU_MAT:
arr.getGpuMat().download(buf);
return buf;
case _InputArray::OPENGL_BUFFER:
arr.getOGlBuffer().copyTo(buf);
return buf;
default:
return arr.getMat();
}
}
UMat cv::superres::arrGetUMat(InputArray arr, UMat& buf)
{
switch (arr.kind())
{
case _InputArray::CUDA_GPU_MAT:
arr.getGpuMat().download(buf);
return buf;
case _InputArray::OPENGL_BUFFER:
arr.getOGlBuffer().copyTo(buf);
return buf;
default:
return arr.getUMat();
}
}
GpuMat cv::superres::arrGetGpuMat(InputArray arr, GpuMat& buf)
{
switch (arr.kind())
{
case _InputArray::CUDA_GPU_MAT:
return arr.getGpuMat();
case _InputArray::OPENGL_BUFFER:
arr.getOGlBuffer().copyTo(buf);
return buf;
default:
buf.upload(arr.getMat());
return buf;
}
}
namespace
{
void mat2mat(InputArray src, OutputArray dst)
{
src.getMat().copyTo(dst);
}
void arr2buf(InputArray src, OutputArray dst)
{
dst.getOGlBufferRef().copyFrom(src);
}
void mat2gpu(InputArray src, OutputArray dst)
{
dst.getGpuMatRef().upload(src.getMat());
}
void buf2arr(InputArray src, OutputArray dst)
{
src.getOGlBuffer().copyTo(dst);
}
void gpu2mat(InputArray src, OutputArray dst)
{
GpuMat d = src.getGpuMat();
dst.create(d.size(), d.type());
Mat m = dst.getMat();
d.download(m);
}
void gpu2gpu(InputArray src, OutputArray dst)
{
src.getGpuMat().copyTo(dst.getGpuMatRef());
}
}
void cv::superres::arrCopy(InputArray src, OutputArray dst)
{
if (dst.isUMat() || src.isUMat())
{
src.copyTo(dst);
return;
}
typedef void (*func_t)(InputArray src, OutputArray dst);
static const func_t funcs[10][10] =
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
{ 0, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, 0, buf2arr },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, arr2buf, 0 , gpu2gpu },
};
const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;
CV_Assert( src_kind >= 0 && src_kind < 10 );
CV_Assert( dst_kind >= 0 && dst_kind < 10 );
const func_t func = funcs[src_kind][dst_kind];
CV_Assert( func != 0 );
func(src, dst);
}
namespace
{
void convertToCn(InputArray src, OutputArray dst, int cn)
{
int scn = src.channels();
CV_Assert( scn == 1 || scn == 3 || scn == 4 );
CV_Assert( cn == 1 || cn == 3 || cn == 4 );
static const int codes[5][5] =
{
{ -1, -1, -1, -1, -1 },
{ -1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA },
{ -1, -1, -1, -1, -1 },
{ -1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA },
{ -1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1 }
};
const int code = codes[scn][cn];
CV_Assert( code >= 0 );
switch (src.kind())
{
case _InputArray::CUDA_GPU_MAT:
#ifdef HAVE_OPENCV_CUDAIMGPROC
cuda::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);
#else
CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
#endif
break;
default:
cv::cvtColor(src, dst, code, cn);
break;
}
}
void convertToDepth(InputArray src, OutputArray dst, int depth)
{
const int sdepth = src.depth();
CV_Assert( sdepth <= CV_64F );
CV_Assert( depth == CV_8U || depth == CV_32F );
static const double maxVals[CV_64F + 1] =
{
(double)std::numeric_limits<uchar>::max(),
(double)std::numeric_limits<schar>::max(),
(double)std::numeric_limits<ushort>::max(),
(double)std::numeric_limits<short>::max(),
(double)std::numeric_limits<int>::max(),
1.0,
1.0,
};
const double scale = maxVals[depth] / maxVals[sdepth];
switch (src.kind())
{
case _InputArray::CUDA_GPU_MAT:
src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
break;
case _InputArray::UMAT:
src.getUMat().convertTo(dst, depth, scale);
break;
default:
src.getMat().convertTo(dst, depth, scale);
break;
}
}
}
Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
{
CV_INSTRUMENT_REGION();
if (src.type() == type)
return src;
const int depth = CV_MAT_DEPTH(type);
const int cn = CV_MAT_CN(type);
if (src.depth() == depth)
{
convertToCn(src, buf0, cn);
return buf0;
}
if (src.channels() == cn)
{
convertToDepth(src, buf1, depth);
return buf1;
}
convertToCn(src, buf0, cn);
convertToDepth(buf0, buf1, depth);
return buf1;
}
UMat cv::superres::convertToType(const UMat& src, int type, UMat& buf0, UMat& buf1)
{
CV_INSTRUMENT_REGION();
if (src.type() == type)
return src;
const int depth = CV_MAT_DEPTH(type);
const int cn = CV_MAT_CN(type);
if (src.depth() == depth)
{
convertToCn(src, buf0, cn);
return buf0;
}
if (src.channels() == cn)
{
convertToDepth(src, buf1, depth);
return buf1;
}
convertToCn(src, buf0, cn);
convertToDepth(buf0, buf1, depth);
return buf1;
}
GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
{
if (src.type() == type)
return src;
const int depth = CV_MAT_DEPTH(type);
const int cn = CV_MAT_CN(type);
if (src.depth() == depth)
{
convertToCn(src, buf0, cn);
return buf0;
}
if (src.channels() == cn)
{
convertToDepth(src, buf1, depth);
return buf1;
}
convertToCn(src, buf0, cn);
convertToDepth(buf0, buf1, depth);
return buf1;
}
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_SUPERRES_INPUT_ARRAY_UTILITY_HPP__
#define __OPENCV_SUPERRES_INPUT_ARRAY_UTILITY_HPP__
#include "opencv2/core.hpp"
#include "opencv2/core/cuda.hpp"
namespace cv
{
namespace superres
{
CV_EXPORTS Mat arrGetMat(InputArray arr, Mat& buf);
CV_EXPORTS UMat arrGetUMat(InputArray arr, UMat& buf);
CV_EXPORTS cuda::GpuMat arrGetGpuMat(InputArray arr, cuda::GpuMat& buf);
CV_EXPORTS void arrCopy(InputArray src, OutputArray dst);
CV_EXPORTS Mat convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1);
CV_EXPORTS UMat convertToType(const UMat& src, int type, UMat& buf0, UMat& buf1);
CV_EXPORTS cuda::GpuMat convertToType(const cuda::GpuMat& src, int type, cuda::GpuMat& buf0, cuda::GpuMat& buf1);
}
}
#endif // __OPENCV_SUPERRES_INPUT_ARRAY_UTILITY_HPP__
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Jin Ma jin@multicorewareinc.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors as is and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef cn
#define cn 1
#endif
#define sz (int)sizeof(float)
#define src_elem_at(_src, y, step, x) *(__global const float *)(_src + mad24(y, step, (x) * sz))
#define dst_elem_at(_dst, y, step, x) *(__global float *)(_dst + mad24(y, step, (x) * sz))
__kernel void buildMotionMaps(__global const uchar * forwardMotionPtr, int forwardMotion_step, int forwardMotion_offset,
__global const uchar * backwardMotionPtr, int backwardMotion_step, int backwardMotion_offset,
__global const uchar * forwardMapPtr, int forwardMap_step, int forwardMap_offset,
__global const uchar * backwardMapPtr, int backwardMap_step, int backwardMap_offset,
int rows, int cols)
{
int x = get_global_id(0);
int y = get_global_id(1);
if (x < cols && y < rows)
{
int forwardMotion_index = mad24(forwardMotion_step, y, (int)sizeof(float2) * x + forwardMotion_offset);
int backwardMotion_index = mad24(backwardMotion_step, y, (int)sizeof(float2) * x + backwardMotion_offset);
int forwardMap_index = mad24(forwardMap_step, y, (int)sizeof(float2) * x + forwardMap_offset);
int backwardMap_index = mad24(backwardMap_step, y, (int)sizeof(float2) * x + backwardMap_offset);
float2 forwardMotion = *(__global const float2 *)(forwardMotionPtr + forwardMotion_index);
float2 backwardMotion = *(__global const float2 *)(backwardMotionPtr + backwardMotion_index);
__global float2 * forwardMap = (__global float2 *)(forwardMapPtr + forwardMap_index);
__global float2 * backwardMap = (__global float2 *)(backwardMapPtr + backwardMap_index);
float2 basePoint = (float2)(x, y);
forwardMap[0] = basePoint + backwardMotion;
backwardMap[0] = basePoint + forwardMotion;
}
}
__kernel void upscale(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols,
__global uchar * dstptr, int dst_step, int dst_offset, int scale)
{
int x = get_global_id(0);
int y = get_global_id(1);
if (x < src_cols && y < src_rows)
{
int src_index = mad24(y, src_step, sz * x * cn + src_offset);
int dst_index = mad24(y * scale, dst_step, sz * x * scale * cn + dst_offset);
__global const float * src = (__global const float *)(srcptr + src_index);
__global float * dst = (__global float *)(dstptr + dst_index);
#pragma unroll
for (int c = 0; c < cn; ++c)
dst[c] = src[c];
}
}
inline float diffSign1(float a, float b)
{
return a > b ? 1.0f : a < b ? -1.0f : 0.0f;
}
inline float3 diffSign3(float3 a, float3 b)
{
float3 pos;
pos.x = a.x > b.x ? 1.0f : a.x < b.x ? -1.0f : 0.0f;
pos.y = a.y > b.y ? 1.0f : a.y < b.y ? -1.0f : 0.0f;
pos.z = a.z > b.z ? 1.0f : a.z < b.z ? -1.0f : 0.0f;
return pos;
}
__kernel void diffSign(__global const uchar * src1, int src1_step, int src1_offset,
__global const uchar * src2, int src2_step, int src2_offset,
__global uchar * dst, int dst_step, int dst_offset, int rows, int cols)
{
int x = get_global_id(0);
int y = get_global_id(1);
if (x < cols && y < rows)
*(__global float *)(dst + mad24(y, dst_step, sz * x + dst_offset)) =
diffSign1(*(__global const float *)(src1 + mad24(y, src1_step, sz * x + src1_offset)),
*(__global const float *)(src2 + mad24(y, src2_step, sz * x + src2_offset)));
}
__kernel void calcBtvRegularization(__global const uchar * src, int src_step, int src_offset,
__global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
int ksize, __constant float * c_btvRegWeights)
{
int x = get_global_id(0) + ksize;
int y = get_global_id(1) + ksize;
if (y < dst_rows - ksize && x < dst_cols - ksize)
{
src += src_offset;
#if cn == 1
const float srcVal = src_elem_at(src, y, src_step, x);
float dstVal = 0.0f;
for (int m = 0, count = 0; m <= ksize; ++m)
for (int l = ksize; l + m >= 0; --l, ++count)
{
dstVal += c_btvRegWeights[count] * (diffSign1(srcVal, src_elem_at(src, y + m, src_step, x + l))
- diffSign1(src_elem_at(src, y - m, src_step, x - l), srcVal));
}
dst_elem_at(dst, y, dst_step, x) = dstVal;
#elif cn == 3
__global const float * src0ptr = (__global const float *)(src + mad24(y, src_step, 3 * sz * x + src_offset));
float3 srcVal = (float3)(src0ptr[0], src0ptr[1], src0ptr[2]), dstVal = 0.f;
for (int m = 0, count = 0; m <= ksize; ++m)
{
for (int l = ksize; l + m >= 0; --l, ++count)
{
__global const float * src1ptr = (__global const float *)(src + mad24(y + m, src_step, 3 * sz * (x + l) + src_offset));
__global const float * src2ptr = (__global const float *)(src + mad24(y - m, src_step, 3 * sz * (x - l) + src_offset));
float3 src1 = (float3)(src1ptr[0], src1ptr[1], src1ptr[2]);
float3 src2 = (float3)(src2ptr[0], src2ptr[1], src2ptr[2]);
dstVal += c_btvRegWeights[count] * (diffSign3(srcVal, src1) - diffSign3(src2, srcVal));
}
}
__global float * dstptr = (__global float *)(dst + mad24(y, dst_step, 3 * sz * x + dst_offset + 0));
dstptr[0] = dstVal.x;
dstptr[1] = dstVal.y;
dstptr[2] = dstVal.z;
#else
#error "Number of channels should be either 1 of 3"
#endif
}
}
This diff is collapsed.
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_PRECOMP_H__
#define __OPENCV_PRECOMP_H__
#include <vector>
#include <limits>
#include "opencv2/opencv_modules.hpp"
#include "opencv2/core.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/core/opengl.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/video/tracking.hpp"
#include "opencv2/core/private.hpp"
#include "opencv2/core/private.cuda.hpp"
#include "opencv2/core/ocl.hpp"
#ifdef HAVE_OPENCV_CUDAARITHM
# include "opencv2/cudaarithm.hpp"
#endif
#ifdef HAVE_OPENCV_CUDAWARPING
# include "opencv2/cudawarping.hpp"
#endif
#ifdef HAVE_OPENCV_CUDAFILTERS
# include "opencv2/cudafilters.hpp"
#endif
#ifdef HAVE_OPENCV_CUDAIMGPROC
# include "opencv2/cudaimgproc.hpp"
#endif
#ifdef HAVE_OPENCV_CUDAOPTFLOW
# include "opencv2/cudaoptflow.hpp"
#endif
#ifdef HAVE_OPENCV_CUDACODEC
# include "opencv2/cudacodec.hpp"
#endif
#ifdef HAVE_OPENCV_VIDEOIO
#include "opencv2/videoio.hpp"
#endif
#include "opencv2/superres.hpp"
#include "opencv2/superres/optical_flow.hpp"
#include "input_array_utility.hpp"
#include "ring_buffer.hpp"
#include "opencv2/core/private.hpp"
#endif /* __OPENCV_PRECOMP_H__ */
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __RING_BUFFER_HPP__
#define __RING_BUFFER_HPP__
#include "precomp.hpp"
namespace cv
{
namespace superres
{
namespace detail
{
template <typename T, class A>
inline const T& at(int index, const std::vector<T, A>& items)
{
const int len = static_cast<int>(items.size());
if (index < 0)
index -= ((index - len + 1) / len) * len;
if (index >= len)
index %= len;
return items[index];
}
template <typename T, class A>
inline T& at(int index, std::vector<T, A>& items)
{
const int len = static_cast<int>(items.size());
if (index < 0)
index -= ((index - len + 1) / len) * len;
if (index >= len)
index %= len;
return items[index];
}
}
}
}
#endif // __RING_BUFFER_HPP__
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
using namespace cv;
using namespace cv::superres;
cv::superres::SuperResolution::SuperResolution()
{
frameSource_ = createFrameSource_Empty();
firstCall_ = true;
isUmat_ = false;
}
void cv::superres::SuperResolution::setInput(const Ptr<FrameSource>& frameSource)
{
frameSource_ = frameSource;
firstCall_ = true;
isUmat_ = false;
}
void cv::superres::SuperResolution::nextFrame(OutputArray frame)
{
CV_INSTRUMENT_REGION();
isUmat_ = frame.isUMat();
if (firstCall_)
{
initImpl(frameSource_);
firstCall_ = false;
}
processImpl(frameSource_, frame);
}
void cv::superres::SuperResolution::reset()
{
frameSource_->reset();
firstCall_ = true;
isUmat_ = false;
}
void cv::superres::SuperResolution::collectGarbage()
{
}
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "test_precomp.hpp"
#if defined(HAVE_HPX)
#include <hpx/hpx_main.hpp>
#endif
CV_TEST_MAIN("superres")
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_TEST_PRECOMP_HPP__
#define __OPENCV_TEST_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/superres.hpp"
#endif
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment