Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_resize_bitexact.cpp
16344 views
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
#include "test_precomp.hpp"
6
7
namespace opencv_test { namespace {
8
9
static const int fixedShiftU8 = 8;
10
11
template <typename T, int fixedShift>
12
void eval4(int64_t xcoeff0, int64_t xcoeff1, int64_t ycoeff0, int64_t ycoeff1, int cn,
13
uint8_t* src_pt00, uint8_t* src_pt01, uint8_t* src_pt10, uint8_t* src_pt11, uint8_t* dst_pt)
14
{
15
static const int64_t fixedRound = ((1LL << (fixedShift * 2)) >> 1);
16
int64_t val = (((T*)src_pt00)[cn] * xcoeff0 + ((T*)src_pt01)[cn] * xcoeff1) * ycoeff0 +
17
(((T*)src_pt10)[cn] * xcoeff0 + ((T*)src_pt11)[cn] * xcoeff1) * ycoeff1 ;
18
((T*)dst_pt)[cn] = saturate_cast<T>((val + fixedRound) >> (fixedShift * 2));
19
}
20
21
TEST(Resize_Bitexact, Linear8U)
22
{
23
static const int64_t fixedOne = (1L << fixedShiftU8);
24
25
struct testmode
26
{
27
int type;
28
Size sz;
29
} modes[] = {
30
{ CV_8UC1, Size( 512, 768) }, // 1/2 1
31
{ CV_8UC3, Size( 512, 768) },
32
{ CV_8UC1, Size(1024, 384) }, // 1 1/2
33
{ CV_8UC4, Size(1024, 384) },
34
{ CV_8UC1, Size( 512, 384) }, // 1/2 1/2
35
{ CV_8UC2, Size( 512, 384) },
36
{ CV_8UC3, Size( 512, 384) },
37
{ CV_8UC4, Size( 512, 384) },
38
{ CV_8UC1, Size( 256, 192) }, // 1/4 1/4
39
{ CV_8UC2, Size( 256, 192) },
40
{ CV_8UC3, Size( 256, 192) },
41
{ CV_8UC4, Size( 256, 192) },
42
{ CV_8UC1, Size( 4, 3) }, // 1/256 1/256
43
{ CV_8UC2, Size( 4, 3) },
44
{ CV_8UC3, Size( 4, 3) },
45
{ CV_8UC4, Size( 4, 3) },
46
{ CV_8UC1, Size( 342, 384) }, // 1/3 1/2
47
{ CV_8UC1, Size( 342, 256) }, // 1/3 1/3
48
{ CV_8UC1, Size( 342, 256) },
49
{ CV_8UC1, Size( 342, 256) },
50
{ CV_8UC1, Size( 342, 256) },
51
{ CV_8UC1, Size( 512, 256) }, // 1/2 1/3
52
{ CV_8UC1, Size( 146, 110) }, // 1/7 1/7
53
{ CV_8UC3, Size( 146, 110) },
54
{ CV_8UC4, Size( 146, 110) },
55
{ CV_8UC1, Size( 931, 698) }, // 10/11 10/11
56
{ CV_8UC2, Size( 931, 698) },
57
{ CV_8UC3, Size( 931, 698) },
58
{ CV_8UC4, Size( 931, 698) },
59
{ CV_8UC1, Size( 853, 640) }, // 10/12 10/12
60
{ CV_8UC3, Size( 853, 640) },
61
{ CV_8UC4, Size( 853, 640) },
62
{ CV_8UC1, Size(1004, 753) }, // 251/256 251/256
63
{ CV_8UC2, Size(1004, 753) },
64
{ CV_8UC3, Size(1004, 753) },
65
{ CV_8UC4, Size(1004, 753) },
66
{ CV_8UC1, Size(2048,1536) }, // 2 2
67
{ CV_8UC2, Size(2048,1536) },
68
{ CV_8UC4, Size(2048,1536) },
69
{ CV_8UC1, Size(3072,2304) }, // 3 3
70
{ CV_8UC3, Size(3072,2304) },
71
{ CV_8UC1, Size(7168,5376) } // 7 7
72
};
73
74
for (int modeind = 0, _modecnt = sizeof(modes) / sizeof(modes[0]); modeind < _modecnt; ++modeind)
75
{
76
int type = modes[modeind].type, depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
77
int dcols = modes[modeind].sz.width, drows = modes[modeind].sz.height;
78
int cols = 1024, rows = 768;
79
80
double inv_scale_x = (double)dcols / cols;
81
double inv_scale_y = (double)drows / rows;
82
softdouble scale_x = softdouble::one() / softdouble(inv_scale_x);
83
softdouble scale_y = softdouble::one() / softdouble(inv_scale_y);
84
85
Mat src(rows, cols, type), refdst(drows, dcols, type), dst;
86
for (int j = 0; j < rows; j++)
87
{
88
uint8_t* line = src.ptr(j);
89
for (int i = 0; i < cols; i++)
90
for (int c = 0; c < cn; c++)
91
{
92
RNG rnd(0x123456789abcdefULL);
93
double val = j < rows / 2 ? ( i < cols / 2 ? ((sin((i + 1)*CV_PI / 256.)*sin((j + 1)*CV_PI / 256.)*sin((cn + 4)*CV_PI / 8.) + 1.)*128.) :
94
(((i / 128 + j / 128) % 2) * 250 + (j / 128) % 2) ) :
95
( i < cols / 2 ? ((i / 128) * (85 - j / 256 * 40) * ((j / 128) % 2) + (7 - i / 128) * (85 - j / 256 * 40) * ((j / 128 + 1) % 2)) :
96
((uchar)rnd) ) ;
97
if (depth == CV_8U)
98
line[i*cn + c] = (uint8_t)val;
99
else if (depth == CV_16U)
100
((uint16_t*)line)[i*cn + c] = (uint16_t)val;
101
else if (depth == CV_16S)
102
((int16_t*)line)[i*cn + c] = (int16_t)val;
103
else if (depth == CV_32S)
104
((int32_t*)line)[i*cn + c] = (int32_t)val;
105
else
106
CV_Assert(0);
107
}
108
}
109
110
for (int j = 0; j < drows; j++)
111
{
112
softdouble src_row_flt = scale_y*(softdouble(j) + softdouble(0.5)) - softdouble(0.5);
113
int src_row = cvFloor(src_row_flt);
114
int64_t ycoeff1 = cvRound64((src_row_flt - softdouble(src_row))*softdouble(fixedOne));
115
int64_t ycoeff0 = fixedOne - ycoeff1;
116
117
for (int i = 0; i < dcols; i++)
118
{
119
softdouble src_col_flt = scale_x*(softdouble(i) + softdouble(0.5)) - softdouble(0.5);
120
int src_col = cvFloor(src_col_flt);
121
int64_t xcoeff1 = cvRound64((src_col_flt - softdouble(src_col))*softdouble(fixedOne));
122
int64_t xcoeff0 = fixedOne - xcoeff1;
123
124
uint8_t* dst_pt = refdst.ptr(j, i);
125
uint8_t* src_pt00 = src.ptr( src_row < 0 ? 0 : src_row >= rows ? rows - 1 : src_row ,
126
src_col < 0 ? 0 : src_col >= cols ? cols - 1 : src_col );
127
uint8_t* src_pt01 = src.ptr( src_row < 0 ? 0 : src_row >= rows ? rows - 1 : src_row ,
128
(src_col + 1) < 0 ? 0 : (src_col + 1) >= cols ? cols - 1 : (src_col + 1));
129
uint8_t* src_pt10 = src.ptr((src_row + 1) < 0 ? 0 : (src_row + 1) >= rows ? rows - 1 : (src_row + 1),
130
src_col < 0 ? 0 : src_col >= cols ? cols - 1 : src_col );
131
uint8_t* src_pt11 = src.ptr((src_row + 1) < 0 ? 0 : (src_row + 1) >= rows ? rows - 1 : (src_row + 1),
132
(src_col + 1) < 0 ? 0 : (src_col + 1) >= cols ? cols - 1 : (src_col + 1));
133
for (int c = 0; c < cn; c++)
134
{
135
if (depth == CV_8U)
136
eval4< uint8_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);
137
else if (depth == CV_16U)
138
eval4<uint16_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);
139
else if (depth == CV_16S)
140
eval4< int16_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);
141
else if (depth == CV_32S)
142
eval4< int32_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);
143
else
144
CV_Assert(0);
145
}
146
}
147
}
148
149
cv::resize(src, dst, Size(dcols, drows), 0, 0, cv::INTER_LINEAR_EXACT);
150
EXPECT_GE(0, cvtest::norm(refdst, dst, cv::NORM_L1))
151
<< "Resize " << cn << "-chan mat from " << cols << "x" << rows << " to " << dcols << "x" << drows << " failed with max diff " << cvtest::norm(refdst, dst, cv::NORM_INF);
152
}
153
}
154
155
}} // namespace
156
157