OpenCV 4.5.3(日本語機械翻訳)
quality_utils.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4
5 #ifndef OPENCV_QUALITY_QUALITY_UTILS_HPP
6 #define OPENCV_QUALITY_QUALITY_UTILS_HPP
7
8 #include "qualitybase.hpp"
9
10 namespace cv
11{
12 namespace quality
13{
14 namespace quality_utils
15{
16
17 // default type of matrix to expand to
18 static CV_CONSTEXPR const int EXPANDED_MAT_DEFAULT_TYPE = CV_32F;
19
20 // convert inputarray to specified mat type. set type == -1 to preserve existing type
21 template <typename R>
22 inline R extract_mat(InputArray in, const int type = -1)
23{
24 R result = {};
25 if ( in.isMat() )
26 in.getMat().convertTo( result, (type != -1) ? type : in.getMat().type());
27 else if ( in.isUMat() )
28 in.getUMat().convertTo( result, (type != -1) ? type : in.getUMat().type());
29 else
30 CV_Error(Error::StsNotImplemented, "Unsupported input type");
31
32 return result;
33}
34
35 // extract and expand matrix to target type
36 template <typename R>
37 inline R expand_mat( InputArray src, int TYPE_DEFAULT = EXPANDED_MAT_DEFAULT_TYPE)
38{
39 auto result = extract_mat<R>(src, -1);
40
41 // by default, expand to 32F unless we already have >= 32 bits, then go to 64
42 // if/when we can detect OpenCL CV_16F support, opt for that when input depth == 8
43 // note that this may impact the precision of the algorithms and would need testing
44 int type = TYPE_DEFAULT;
45
46 switch (result.depth())
47 {
48 case CV_32F:
49 case CV_32S:
50 case CV_64F:
51 type = CV_64F;
52 }; // switch
53
54 result.convertTo(result, type);
55 return result;
56}
57
58 // return mat of observed min/max pair per column
59 // row 0: min per column
60 // row 1: max per column
61 // template <typename T>
62 inline cv::Mat get_column_range( const cv::Mat& data )
63{
64 CV_Assert(data.channels() == 1);
65 CV_Assert(data.rows > 0);
66
67 cv::Mat result( cv::Size( data.cols, 2 ), data.type() );
68
69 auto
70 row_min = result.row(0)
71 , row_max = result.row(1)
72 ;
73
74 // set initial min/max
75 data.row(0).copyTo(row_min);
76 data.row(0).copyTo(row_max);
77
78 for (int y = 1; y < data.rows; ++y)
79 {
80 auto row = data.row(y);
81 cv::min(row,row_min, row_min);
82 cv::max(row, row_max, row_max);
83 }
84 return result;
85} // get_column_range
86
87 // linear scale of each column from min to max
88 // range is column-wise pair of observed min/max. See get_column_range
89 template <typename T>
90 inline void scale( cv::Mat& mat, const cv::Mat& range, const T min, const T max )
91{
92 // value = lower + (upper - lower) * (value - feature_min[index]) / (feature_max[index] - feature_min[index]);
93 // where [lower] = lower bound, [upper] = upper bound
94
95 for (int y = 0; y < mat.rows; ++y)
96 {
97 auto row = mat.row(y);
98 auto row_min = range.row(0);
99 auto row_max = range.row(1);
100
101 for (int x = 0; x < mat.cols; ++x)
102 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));
103 }
104}
105
106} // quality_utils
107} // quality
108} // cv
109 #endif
n-dimensional dense array class
Definition: mat.hpp:802
Template class for specifying the size of an image or rectangle.
Definition: core/types.hpp:316
CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element maximum of two arrays or an array and a scalar.
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element minimum of two arrays or an array and a scalar.
#define CV_Error(code, msg)
Call the error handler.
Definition: base.hpp:320
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition: base.hpp:342
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75