添加项目文件。

This commit is contained in:
CaiXiang
2025-06-09 09:09:25 +08:00
parent 75b909652e
commit 88acb23465
1054 changed files with 615623 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_QUALITY_QUALITY_UTILS_HPP
#define OPENCV_QUALITY_QUALITY_UTILS_HPP
#include "qualitybase.hpp"
namespace cv
{
namespace quality
{
namespace quality_utils
{
// default type of matrix to expand to
static CV_CONSTEXPR const int EXPANDED_MAT_DEFAULT_TYPE = CV_32F;
// convert inputarray to specified mat type. set type == -1 to preserve existing type
template <typename R>
inline R extract_mat(InputArray in, const int type = -1)
{
R result = {};
if ( in.isMat() )
in.getMat().convertTo( result, (type != -1) ? type : in.getMat().type());
else if ( in.isUMat() )
in.getUMat().convertTo( result, (type != -1) ? type : in.getUMat().type());
else
CV_Error(Error::StsNotImplemented, "Unsupported input type");
return result;
}
// extract and expand matrix to target type
template <typename R>
inline R expand_mat( InputArray src, int TYPE_DEFAULT = EXPANDED_MAT_DEFAULT_TYPE)
{
auto result = extract_mat<R>(src, -1);
// by default, expand to 32F unless we already have >= 32 bits, then go to 64
// if/when we can detect OpenCL CV_16F support, opt for that when input depth == 8
// note that this may impact the precision of the algorithms and would need testing
int type = TYPE_DEFAULT;
switch (result.depth())
{
case CV_32F:
case CV_32S:
case CV_64F:
type = CV_64F;
}; // switch
result.convertTo(result, type);
return result;
}
// return mat of observed min/max pair per column
// row 0: min per column
// row 1: max per column
// template <typename T>
inline cv::Mat get_column_range( const cv::Mat& data )
{
CV_Assert(data.channels() == 1);
CV_Assert(data.rows > 0);
cv::Mat result( cv::Size( data.cols, 2 ), data.type() );
auto
row_min = result.row(0)
, row_max = result.row(1)
;
// set initial min/max
data.row(0).copyTo(row_min);
data.row(0).copyTo(row_max);
for (int y = 1; y < data.rows; ++y)
{
auto row = data.row(y);
cv::min(row,row_min, row_min);
cv::max(row, row_max, row_max);
}
return result;
} // get_column_range
// linear scale of each column from min to max
// range is column-wise pair of observed min/max. See get_column_range
template <typename T>
inline void scale( cv::Mat& mat, const cv::Mat& range, const T min, const T max )
{
// value = lower + (upper - lower) * (value - feature_min[index]) / (feature_max[index] - feature_min[index]);
// where [lower] = lower bound, [upper] = upper bound
for (int y = 0; y < mat.rows; ++y)
{
auto row = mat.row(y);
auto row_min = range.row(0);
auto row_max = range.row(1);
for (int x = 0; x < mat.cols; ++x)
row.at<T>(x) = min + (max - min) * (row.at<T>(x) - row_min.at<T>(x) ) / (row_max.at<T>(x) - row_min.at<T>(x));
}
}
} // quality_utils
} // quality
} // cv
#endif

View File

@@ -0,0 +1,63 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_QUALITYBASE_HPP
#define OPENCV_QUALITYBASE_HPP
#include <opencv2/core.hpp>
/**
@defgroup quality Image Quality Analysis (IQA) API
*/
namespace cv
{
namespace quality
{
//! @addtogroup quality
//! @{
/************************************ Quality Base Class ************************************/
class CV_EXPORTS_W QualityBase
: public virtual Algorithm
{
public:
/** @brief Destructor */
virtual ~QualityBase() = default;
/**
@brief Compute quality score per channel with the per-channel score in each element of the resulting cv::Scalar. See specific algorithm for interpreting result scores
@param img comparison image, or image to evalute for no-reference quality algorithms
*/
virtual CV_WRAP cv::Scalar compute( InputArray img ) = 0;
/** @brief Returns output quality map that was generated during computation, if supported by the algorithm */
virtual CV_WRAP void getQualityMap(OutputArray dst) const
{
if (!dst.needed() || _qualityMap.empty() )
return;
dst.assign(_qualityMap);
}
/** @brief Implements Algorithm::clear() */
CV_WRAP void clear() CV_OVERRIDE { _qualityMap = _mat_type(); Algorithm::clear(); }
/** @brief Implements Algorithm::empty() */
CV_WRAP bool empty() const CV_OVERRIDE { return _qualityMap.empty(); }
protected:
/** @brief internal mat type default */
using _mat_type = cv::UMat;
/** @brief Output quality maps if generated by algorithm */
_mat_type _qualityMap;
}; // QualityBase
//! @}
} // quality
} // cv
#endif

View File

@@ -0,0 +1,82 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_QUALITY_QUALITYBRISQUE_HPP
#define OPENCV_QUALITY_QUALITYBRISQUE_HPP
#include "qualitybase.hpp"
#include "opencv2/ml.hpp"
namespace cv
{
namespace quality
{
/**
@brief BRISQUE (Blind/Referenceless Image Spatial Quality Evaluator) is a No Reference Image Quality Assessment (NR-IQA) algorithm.
BRISQUE computes a score based on extracting Natural Scene Statistics (https://en.wikipedia.org/wiki/Scene_statistics)
and calculating feature vectors. See Mittal et al. @cite Mittal2 for original paper and original implementation @cite Mittal2_software .
A trained model is provided in the /samples/ directory and is trained on the LIVE-R2 database @cite Sheikh as in the original implementation.
When evaluated against the TID2008 database @cite Ponomarenko , the SROCC is -0.8424 versus the SROCC of -0.8354 in the original implementation.
C++ code for the BRISQUE LIVE-R2 trainer and TID2008 evaluator are also provided in the /samples/ directory.
*/
class CV_EXPORTS_W QualityBRISQUE : public QualityBase {
public:
/** @brief Computes BRISQUE quality score for input image
@param img Image for which to compute quality
@returns cv::Scalar with the score in the first element. The score ranges from 0 (best quality) to 100 (worst quality)
*/
CV_WRAP cv::Scalar compute( InputArray img ) CV_OVERRIDE;
/**
@brief Create an object which calculates quality
@param model_file_path cv::String which contains a path to the BRISQUE model data, eg. /path/to/brisque_model_live.yml
@param range_file_path cv::String which contains a path to the BRISQUE range data, eg. /path/to/brisque_range_live.yml
*/
CV_WRAP static Ptr<QualityBRISQUE> create( const cv::String& model_file_path, const cv::String& range_file_path );
/**
@brief Create an object which calculates quality
@param model cv::Ptr<cv::ml::SVM> which contains a loaded BRISQUE model
@param range cv::Mat which contains BRISQUE range data
*/
CV_WRAP static Ptr<QualityBRISQUE> create( const cv::Ptr<cv::ml::SVM>& model, const cv::Mat& range );
/**
@brief static method for computing quality
@param img image for which to compute quality
@param model_file_path cv::String which contains a path to the BRISQUE model data, eg. /path/to/brisque_model_live.yml
@param range_file_path cv::String which contains a path to the BRISQUE range data, eg. /path/to/brisque_range_live.yml
@returns cv::Scalar with the score in the first element. The score ranges from 0 (best quality) to 100 (worst quality)
*/
CV_WRAP static cv::Scalar compute( InputArray img, const cv::String& model_file_path, const cv::String& range_file_path );
/**
@brief static method for computing image features used by the BRISQUE algorithm
@param img image (BGR(A) or grayscale) for which to compute features
@param features output row vector of features to cv::Mat or cv::UMat
*/
CV_WRAP static void computeFeatures(InputArray img, OutputArray features);
protected:
cv::Ptr<cv::ml::SVM> _model = nullptr;
cv::Mat _range;
/** @brief Internal constructor */
QualityBRISQUE( const cv::String& model_file_path, const cv::String& range_file_path );
/** @brief Internal constructor */
QualityBRISQUE(const cv::Ptr<cv::ml::SVM>& model, const cv::Mat& range )
: _model{ model }
, _range{ range }
{}
}; // QualityBRISQUE
} // quality
} // cv
#endif

View File

@@ -0,0 +1,92 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_QUALITY_QUALITYGMSD_HPP
#define OPENCV_QUALITY_QUALITYGMSD_HPP
#include "qualitybase.hpp"
namespace cv
{
namespace quality
{
/**
@brief Full reference GMSD algorithm
http://www4.comp.polyu.edu.hk/~cslzhang/IQA/GMSD/GMSD.htm
*/
class CV_EXPORTS_W QualityGMSD
: public QualityBase {
public:
/**
@brief Compute GMSD
@param cmp comparison image
@returns cv::Scalar with per-channel quality value. Values range from 0 (worst) to 1 (best)
*/
CV_WRAP cv::Scalar compute( InputArray cmp ) CV_OVERRIDE;
/** @brief Implements Algorithm::empty() */
CV_WRAP bool empty() const CV_OVERRIDE { return _refImgData.empty() && QualityBase::empty(); }
/** @brief Implements Algorithm::clear() */
CV_WRAP void clear() CV_OVERRIDE { _refImgData = _mat_data(); QualityBase::clear(); }
/**
@brief Create an object which calculates image quality
@param ref reference image
*/
CV_WRAP static Ptr<QualityGMSD> create( InputArray ref );
/**
@brief static method for computing quality
@param ref reference image
@param cmp comparison image
@param qualityMap output quality map, or cv::noArray()
@returns cv::Scalar with per-channel quality value. Values range from 0 (worst) to 1 (best)
*/
CV_WRAP static cv::Scalar compute( InputArray ref, InputArray cmp, OutputArray qualityMap );
protected:
// holds computed values for a mat
struct _mat_data
{
// internal mat type
using mat_type = QualityBase::_mat_type;
mat_type
gradient_map
, gradient_map_squared
;
// allow default construction
_mat_data() = default;
// construct from mat_type
_mat_data(const mat_type&);
// construct from inputarray
_mat_data(InputArray);
// returns flag if empty
bool empty() const { return this->gradient_map.empty() && this->gradient_map_squared.empty(); }
// compute for a single frame
static std::pair<cv::Scalar, mat_type> compute(const _mat_data& lhs, const _mat_data& rhs);
}; // mat_data
/** @brief Reference image data */
_mat_data _refImgData;
// internal constructor
QualityGMSD(_mat_data refImgData)
: _refImgData(std::move(refImgData))
{}
}; // QualityGMSD
} // quality
} // cv
#endif

View File

@@ -0,0 +1,64 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_QUALITY_QUALITYMSE_HPP
#define OPENCV_QUALITY_QUALITYMSE_HPP
#include "qualitybase.hpp"
namespace cv
{
namespace quality
{
/**
@brief Full reference mean square error algorithm https://en.wikipedia.org/wiki/Mean_squared_error
*/
class CV_EXPORTS_W QualityMSE : public QualityBase {
public:
/** @brief Computes MSE for reference images supplied in class constructor and provided comparison images
@param cmpImgs Comparison image(s)
@returns cv::Scalar with per-channel quality values. Values range from 0 (best) to potentially max float (worst)
*/
CV_WRAP cv::Scalar compute( InputArrayOfArrays cmpImgs ) CV_OVERRIDE;
/** @brief Implements Algorithm::empty() */
CV_WRAP bool empty() const CV_OVERRIDE { return _ref.empty() && QualityBase::empty(); }
/** @brief Implements Algorithm::clear() */
CV_WRAP void clear() CV_OVERRIDE { _ref = _mat_type(); QualityBase::clear(); }
/**
@brief Create an object which calculates quality
@param ref input image to use as the reference for comparison
*/
CV_WRAP static Ptr<QualityMSE> create(InputArray ref);
/**
@brief static method for computing quality
@param ref reference image
@param cmp comparison image=
@param qualityMap output quality map, or cv::noArray()
@returns cv::Scalar with per-channel quality values. Values range from 0 (best) to max float (worst)
*/
CV_WRAP static cv::Scalar compute( InputArray ref, InputArray cmp, OutputArray qualityMap );
protected:
/** @brief Reference image, converted to internal mat type */
QualityBase::_mat_type _ref;
/**
@brief Constructor
@param ref reference image, converted to internal type
*/
QualityMSE(QualityBase::_mat_type ref)
: _ref(std::move(ref))
{}
}; // QualityMSE
} // quality
} // cv
#endif

View File

@@ -0,0 +1,120 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_QUALITY_QUALITYPSNR_HPP
#define OPENCV_QUALITY_QUALITYPSNR_HPP
#include <limits> // numeric_limits
#include "qualitybase.hpp"
#include "qualitymse.hpp"
namespace cv
{
namespace quality
{
/**
@brief Full reference peak signal to noise ratio (PSNR) algorithm https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
*/
class CV_EXPORTS_W QualityPSNR
: public QualityBase {
public:
/** @brief Default maximum pixel value */
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
static constexpr double MAX_PIXEL_VALUE_DEFAULT = 255.;
#else
// support MSVS 2013
static const int MAX_PIXEL_VALUE_DEFAULT = 255;
#endif
/**
@brief Create an object which calculates quality
@param ref input image to use as the source for comparison
@param maxPixelValue maximum per-channel value for any individual pixel; eg 255 for uint8 image
*/
CV_WRAP static Ptr<QualityPSNR> create( InputArray ref, double maxPixelValue = QualityPSNR::MAX_PIXEL_VALUE_DEFAULT )
{
return Ptr<QualityPSNR>(new QualityPSNR(QualityMSE::create(ref), maxPixelValue));
}
/**
@brief Compute the PSNR
@param cmp Comparison image
@returns Per-channel PSNR value, or std::numeric_limits<double>::infinity() if the MSE between the two images == 0
*/
CV_WRAP cv::Scalar compute( InputArray cmp ) CV_OVERRIDE
{
auto result = _qualityMSE->compute( cmp );
_qualityMSE->getQualityMap(_qualityMap); // copy from internal obj to this obj
return _mse_to_psnr(
result
, _maxPixelValue
);
}
/** @brief Implements Algorithm::empty() */
CV_WRAP bool empty() const CV_OVERRIDE { return _qualityMSE->empty() && QualityBase::empty(); }
/** @brief Implements Algorithm::clear() */
CV_WRAP void clear() CV_OVERRIDE { _qualityMSE->clear(); QualityBase::clear(); }
/**
@brief static method for computing quality
@param ref reference image
@param cmp comparison image
@param qualityMap output quality map, or cv::noArray()
@param maxPixelValue maximum per-channel value for any individual pixel; eg 255 for uint8 image
@returns PSNR value, or std::numeric_limits<double>::infinity() if the MSE between the two images == 0
*/
CV_WRAP static cv::Scalar compute( InputArray ref, InputArray cmp, OutputArray qualityMap, double maxPixelValue = QualityPSNR::MAX_PIXEL_VALUE_DEFAULT)
{
return _mse_to_psnr(
QualityMSE::compute(ref, cmp, qualityMap)
, maxPixelValue
);
}
/** @brief return the maximum pixel value used for PSNR computation */
CV_WRAP double getMaxPixelValue() const { return _maxPixelValue; }
/**
@brief sets the maximum pixel value used for PSNR computation
@param val Maximum pixel value
*/
CV_WRAP void setMaxPixelValue(double val) { this->_maxPixelValue = val; }
protected:
Ptr<QualityMSE> _qualityMSE;
double _maxPixelValue = QualityPSNR::MAX_PIXEL_VALUE_DEFAULT;
/** @brief Constructor */
QualityPSNR( Ptr<QualityMSE> qualityMSE, double maxPixelValue )
: _qualityMSE(std::move(qualityMSE))
, _maxPixelValue(maxPixelValue)
{}
// convert mse to psnr
static double _mse_to_psnr(double mse, double max_pixel_value)
{
return (mse == 0.)
? std::numeric_limits<double>::infinity()
: 10. * std::log10((max_pixel_value * max_pixel_value) / mse)
;
}
// convert scalar of mses to psnrs
static cv::Scalar _mse_to_psnr(cv::Scalar mse, double max_pixel_value)
{
for (int i = 0; i < mse.rows; ++i)
mse(i) = _mse_to_psnr(mse(i), max_pixel_value);
return mse;
}
}; // QualityPSNR
} // quality
} // cv
#endif

View File

@@ -0,0 +1,97 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_QUALITY_QUALITYSSIM_HPP
#define OPENCV_QUALITY_QUALITYSSIM_HPP
#include "qualitybase.hpp"
namespace cv
{
namespace quality
{
/**
@brief Full reference structural similarity algorithm https://en.wikipedia.org/wiki/Structural_similarity
*/
class CV_EXPORTS_W QualitySSIM
: public QualityBase {
public:
/**
@brief Computes SSIM
@param cmp Comparison image
@returns cv::Scalar with per-channel quality values. Values range from 0 (worst) to 1 (best)
*/
CV_WRAP cv::Scalar compute( InputArray cmp ) CV_OVERRIDE;
/** @brief Implements Algorithm::empty() */
CV_WRAP bool empty() const CV_OVERRIDE { return _refImgData.empty() && QualityBase::empty(); }
/** @brief Implements Algorithm::clear() */
CV_WRAP void clear() CV_OVERRIDE { _refImgData = _mat_data(); QualityBase::clear(); }
/**
@brief Create an object which calculates quality
@param ref input image to use as the reference image for comparison
*/
CV_WRAP static Ptr<QualitySSIM> create( InputArray ref );
/**
@brief static method for computing quality
@param ref reference image
@param cmp comparison image
@param qualityMap output quality map, or cv::noArray()
@returns cv::Scalar with per-channel quality values. Values range from 0 (worst) to 1 (best)
*/
CV_WRAP static cv::Scalar compute( InputArray ref, InputArray cmp, OutputArray qualityMap );
protected:
// holds computed values for a mat
struct _mat_data
{
// internal mat type
using mat_type = QualityBase::_mat_type;
mat_type
I
, I_2
, mu
, mu_2
, sigma_2
;
// allow default construction
_mat_data() = default;
// construct from mat_type
_mat_data(const mat_type&);
// construct from inputarray
_mat_data(InputArray);
// return flag if this is empty
bool empty() const { return I.empty() && I_2.empty() && mu.empty() && mu_2.empty() && sigma_2.empty(); }
// computes ssim and quality map for single frame
static std::pair<cv::Scalar, mat_type> compute(const _mat_data& lhs, const _mat_data& rhs);
}; // mat_data
/** @brief Reference image data */
_mat_data _refImgData;
/**
@brief Constructor
@param refImgData reference image, converted to internal type
*/
QualitySSIM( _mat_data refImgData )
: _refImgData( std::move(refImgData) )
{}
}; // QualitySSIM
} // quality
} // cv
#endif