Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added QualityMAE with tests #3691

Open
wants to merge 15 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/quality/include/opencv2/quality.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
#include "quality/qualityssim.hpp"
#include "quality/qualitygmsd.hpp"
#include "quality/qualitybrisque.hpp"
#include "quality/qualitymae.hpp"

#endif
75 changes: 75 additions & 0 deletions modules/quality/include/opencv2/quality/qualitymae.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef OPENCV_QUALITY_MAE_HPP
opencv-alalek marked this conversation as resolved.
Show resolved Hide resolved
#define OPENCV_QUALITY_MAE_HPP

#include "qualitybase.hpp"


namespace cv
{

namespace quality
{

/** @brief Flags to choose which algorithm MAE should use.
*/
enum MAEStatsFlags
{
MAE_Max,
MAE_Mean
opencv-alalek marked this conversation as resolved.
Show resolved Hide resolved
};

class CV_EXPORTS_W QualityMAE : public QualityBase
{
public:
/** @brief Computes MAE for reference images supplied in class constructor and provided comparison images
asmorkalov marked this conversation as resolved.
Show resolved Hide resolved
@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 Scalar compute( InputArray 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
@param statsProc statistical method to apply on the error
*/
CV_WRAP static Ptr<QualityMAE> create(InputArray ref, int statsProc = MAE_Mean);

/**
@brief static method for computing quality
@param ref reference image
@param cmp comparison image=
@param qualityMap output quality map, or cv::noArray()
@param statsProc which statistical method should be apply on the absolute error
@returns cv::Scalar with per-channel quality values. Values range from 0 (best) to max float (worst)
*/
CV_WRAP static Scalar compute( InputArray ref, InputArray cmp, OutputArray qualityMap, int statsProc = MAE_Mean );


protected:

/** @brief Reference image, converted to internal mat type */
QualityBase::_mat_type _ref;

/** @brief What statistics analysis to apply on the absolute error */
int _flag;

/**
@brief Constructor
@param ref reference image, converted to internal type
@param statsProc statistical method to apply on the error
*/
QualityMAE(QualityBase::_mat_type ref, int statsProc);

};

} // quality

} // cv

#endif // OPENCV_QUALITY_MAE_HPP
77 changes: 77 additions & 0 deletions modules/quality/src/qualitymae.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.

#include "precomp.hpp"
#include "opencv2/quality/qualitymae.hpp"
#include "opencv2/quality/quality_utils.hpp"

namespace cv
{

namespace quality
{

using namespace quality_utils;


// Static
Ptr<QualityMAE> QualityMAE::create(InputArray ref, int statsProc)
{
return Ptr<QualityMAE>(new QualityMAE(quality_utils::expand_mat<_mat_type>(ref), statsProc));
}

// Static
Scalar QualityMAE::compute(InputArray ref, InputArray cmp, OutputArray qualityMap, int statsProc)
{
CV_Assert_3(ref.channels() <= 4,
cmp.channels() <= 4,
(statsProc == MAE_Max) || (statsProc == MAE_Mean) );

_mat_type err;
int wdepth = std::max(std::max(ref.depth(), cmp.depth()), CV_32F);
int cn = ref.channels();
int wtype = CV_MAKETYPE(wdepth, cn);

absdiff(extract_mat<_mat_type>(ref, wtype), extract_mat<_mat_type>(cmp, wtype), err);

if(qualityMap.needed())
qualityMap.assign(statsProc == MAE_Max ? err : err.clone());

if(statsProc == MAE_Mean)
{
return mean(err);
}

Scalar scores;
_mat_type tmp = err.reshape(err.channels(), 1);

reduce(tmp, tmp, 1, REDUCE_MAX, wdepth);

tmp.convertTo(Mat(tmp.size(), CV_64FC(cn), scores.val), CV_64F);

return scores;
}

// Not static
Scalar QualityMAE::compute( InputArray cmpImg )
{
CV_Assert(cmpImg.isMat() || cmpImg.isUMat() || cmpImg.isMatx());

if(cmpImg.empty())
return Scalar();

// If the input is a set of images.
_mat_type cmp = extract_mat<_mat_type>(cmpImg, std::max(cmpImg.depth(), CV_32F));

return QualityMAE::compute(this->_ref, cmp, this->_qualityMap, this->_flag);
}

QualityMAE::QualityMAE(QualityBase::_mat_type ref, int flag)
: _ref(std::move(ref)),
_flag(flag)
{}

} // quality

} // cv
66 changes: 66 additions & 0 deletions modules/quality/test/test_mae.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "test_precomp.hpp"
opencv-alalek marked this conversation as resolved.
Show resolved Hide resolved

#define TEST_CASE_NAME CV_Quality_MAE

namespace opencv_test
{
namespace quality_test
{

const cv::Scalar

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static? to avoid symbols exporting

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it static, but I was thinking perhaps it can go in the test_precomp.hpp in the same place as MSE_EXPECTED_1 and MSE_EXPECTED_2?

MAE_MAX_EXPECTED_1 = { 203. },
MAE_MEAN_EXPECTED_1 = { 33.5824 },
MAE_MAX_EXPECTED_2 = { 138., 145., 156. },
MAE_MEAN_EXPECTED_2 = { 5.7918, 6.0645, 5.5609}
;

// static method
TEST(TEST_CASE_NAME, static_max )
{
// Max
cv::Mat qMat = {};
quality_expect_near(quality::QualityMAE::compute(get_testfile_1a(), get_testfile_1a(), qMat, quality::MAE_Max), cv::Scalar(0.)); // ref vs ref == 0
check_quality_map(qMat);
}

// static method
TEST(TEST_CASE_NAME, static_mean )
{
// Mean
cv::Mat qMat = {};
quality_expect_near(quality::QualityMAE::compute(get_testfile_1a(), get_testfile_1a(), qMat, quality::MAE_Mean), cv::Scalar(0.)); // ref vs ref == 0
check_quality_map(qMat);
}

// single channel, with and without opencl
TEST(TEST_CASE_NAME, single_channel_max )
{
auto fn = []() { quality_test(quality::QualityMAE::create(get_testfile_1a(), quality::MAE_Max), get_testfile_1b(), MAE_MAX_EXPECTED_1); };

OCL_OFF( fn() );
OCL_ON( fn() );
}

// single channel, with and without opencl
TEST(TEST_CASE_NAME, single_channel_mean )
{
auto fn = []() { quality_test(quality::QualityMAE::create(get_testfile_1a(), quality::MAE_Mean), get_testfile_1b(), MAE_MEAN_EXPECTED_1); };

OCL_OFF( fn() );
OCL_ON( fn() );
}

// multi-channel max
TEST(TEST_CASE_NAME, multi_channel_max)
{
quality_test(quality::QualityMAE::create(get_testfile_2a(), quality::MAE_Max), get_testfile_2b(), MAE_MAX_EXPECTED_2);
}

// multi-channel mean
TEST(TEST_CASE_NAME, multi_channel_mean)
{
quality_test(quality::QualityMAE::create(get_testfile_2a(), quality::MAE_Mean), get_testfile_2b(), MAE_MEAN_EXPECTED_2);
}

} // namespace quality_test
} // namespace opencv_test