Path: blob/master/modules/imgproc/test/test_resize_bitexact.cpp
16344 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.34#include "test_precomp.hpp"56namespace opencv_test { namespace {78static const int fixedShiftU8 = 8;910template <typename T, int fixedShift>11void eval4(int64_t xcoeff0, int64_t xcoeff1, int64_t ycoeff0, int64_t ycoeff1, int cn,12uint8_t* src_pt00, uint8_t* src_pt01, uint8_t* src_pt10, uint8_t* src_pt11, uint8_t* dst_pt)13{14static const int64_t fixedRound = ((1LL << (fixedShift * 2)) >> 1);15int64_t val = (((T*)src_pt00)[cn] * xcoeff0 + ((T*)src_pt01)[cn] * xcoeff1) * ycoeff0 +16(((T*)src_pt10)[cn] * xcoeff0 + ((T*)src_pt11)[cn] * xcoeff1) * ycoeff1 ;17((T*)dst_pt)[cn] = saturate_cast<T>((val + fixedRound) >> (fixedShift * 2));18}1920TEST(Resize_Bitexact, Linear8U)21{22static const int64_t fixedOne = (1L << fixedShiftU8);2324struct testmode25{26int type;27Size sz;28} modes[] = {29{ CV_8UC1, Size( 512, 768) }, // 1/2 130{ CV_8UC3, Size( 512, 768) },31{ CV_8UC1, Size(1024, 384) }, // 1 1/232{ CV_8UC4, Size(1024, 384) },33{ CV_8UC1, Size( 512, 384) }, // 1/2 1/234{ CV_8UC2, Size( 512, 384) },35{ CV_8UC3, Size( 512, 384) },36{ CV_8UC4, Size( 512, 384) },37{ CV_8UC1, Size( 256, 192) }, // 1/4 1/438{ CV_8UC2, Size( 256, 192) },39{ CV_8UC3, Size( 256, 192) },40{ CV_8UC4, Size( 256, 192) },41{ CV_8UC1, Size( 4, 3) }, // 1/256 1/25642{ CV_8UC2, Size( 4, 3) },43{ CV_8UC3, Size( 4, 3) },44{ CV_8UC4, Size( 4, 3) },45{ CV_8UC1, Size( 342, 384) }, // 1/3 1/246{ CV_8UC1, Size( 342, 256) }, // 1/3 1/347{ CV_8UC1, Size( 342, 256) },48{ CV_8UC1, Size( 342, 256) },49{ CV_8UC1, Size( 342, 256) },50{ CV_8UC1, Size( 512, 256) }, // 1/2 1/351{ CV_8UC1, Size( 146, 110) }, // 1/7 1/752{ CV_8UC3, Size( 146, 110) },53{ CV_8UC4, Size( 146, 110) },54{ CV_8UC1, Size( 931, 698) }, // 10/11 10/1155{ CV_8UC2, Size( 931, 698) },56{ CV_8UC3, Size( 931, 698) },57{ CV_8UC4, Size( 931, 698) },58{ CV_8UC1, Size( 853, 640) }, // 10/12 10/1259{ CV_8UC3, Size( 853, 640) },60{ CV_8UC4, Size( 853, 640) },61{ CV_8UC1, Size(1004, 753) }, // 251/256 251/25662{ CV_8UC2, Size(1004, 753) },63{ CV_8UC3, Size(1004, 753) },64{ CV_8UC4, Size(1004, 753) },65{ CV_8UC1, Size(2048,1536) }, // 2 266{ CV_8UC2, Size(2048,1536) },67{ CV_8UC4, Size(2048,1536) },68{ CV_8UC1, Size(3072,2304) }, // 3 369{ CV_8UC3, Size(3072,2304) },70{ CV_8UC1, Size(7168,5376) } // 7 771};7273for (int modeind = 0, _modecnt = sizeof(modes) / sizeof(modes[0]); modeind < _modecnt; ++modeind)74{75int type = modes[modeind].type, depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);76int dcols = modes[modeind].sz.width, drows = modes[modeind].sz.height;77int cols = 1024, rows = 768;7879double inv_scale_x = (double)dcols / cols;80double inv_scale_y = (double)drows / rows;81softdouble scale_x = softdouble::one() / softdouble(inv_scale_x);82softdouble scale_y = softdouble::one() / softdouble(inv_scale_y);8384Mat src(rows, cols, type), refdst(drows, dcols, type), dst;85for (int j = 0; j < rows; j++)86{87uint8_t* line = src.ptr(j);88for (int i = 0; i < cols; i++)89for (int c = 0; c < cn; c++)90{91RNG rnd(0x123456789abcdefULL);92double 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.) :93(((i / 128 + j / 128) % 2) * 250 + (j / 128) % 2) ) :94( i < cols / 2 ? ((i / 128) * (85 - j / 256 * 40) * ((j / 128) % 2) + (7 - i / 128) * (85 - j / 256 * 40) * ((j / 128 + 1) % 2)) :95((uchar)rnd) ) ;96if (depth == CV_8U)97line[i*cn + c] = (uint8_t)val;98else if (depth == CV_16U)99((uint16_t*)line)[i*cn + c] = (uint16_t)val;100else if (depth == CV_16S)101((int16_t*)line)[i*cn + c] = (int16_t)val;102else if (depth == CV_32S)103((int32_t*)line)[i*cn + c] = (int32_t)val;104else105CV_Assert(0);106}107}108109for (int j = 0; j < drows; j++)110{111softdouble src_row_flt = scale_y*(softdouble(j) + softdouble(0.5)) - softdouble(0.5);112int src_row = cvFloor(src_row_flt);113int64_t ycoeff1 = cvRound64((src_row_flt - softdouble(src_row))*softdouble(fixedOne));114int64_t ycoeff0 = fixedOne - ycoeff1;115116for (int i = 0; i < dcols; i++)117{118softdouble src_col_flt = scale_x*(softdouble(i) + softdouble(0.5)) - softdouble(0.5);119int src_col = cvFloor(src_col_flt);120int64_t xcoeff1 = cvRound64((src_col_flt - softdouble(src_col))*softdouble(fixedOne));121int64_t xcoeff0 = fixedOne - xcoeff1;122123uint8_t* dst_pt = refdst.ptr(j, i);124uint8_t* src_pt00 = src.ptr( src_row < 0 ? 0 : src_row >= rows ? rows - 1 : src_row ,125src_col < 0 ? 0 : src_col >= cols ? cols - 1 : src_col );126uint8_t* src_pt01 = src.ptr( src_row < 0 ? 0 : src_row >= rows ? rows - 1 : src_row ,127(src_col + 1) < 0 ? 0 : (src_col + 1) >= cols ? cols - 1 : (src_col + 1));128uint8_t* src_pt10 = src.ptr((src_row + 1) < 0 ? 0 : (src_row + 1) >= rows ? rows - 1 : (src_row + 1),129src_col < 0 ? 0 : src_col >= cols ? cols - 1 : src_col );130uint8_t* src_pt11 = src.ptr((src_row + 1) < 0 ? 0 : (src_row + 1) >= rows ? rows - 1 : (src_row + 1),131(src_col + 1) < 0 ? 0 : (src_col + 1) >= cols ? cols - 1 : (src_col + 1));132for (int c = 0; c < cn; c++)133{134if (depth == CV_8U)135eval4< uint8_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);136else if (depth == CV_16U)137eval4<uint16_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);138else if (depth == CV_16S)139eval4< int16_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);140else if (depth == CV_32S)141eval4< int32_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt);142else143CV_Assert(0);144}145}146}147148cv::resize(src, dst, Size(dcols, drows), 0, 0, cv::INTER_LINEAR_EXACT);149EXPECT_GE(0, cvtest::norm(refdst, dst, cv::NORM_L1))150<< "Resize " << cn << "-chan mat from " << cols << "x" << rows << " to " << dcols << "x" << drows << " failed with max diff " << cvtest::norm(refdst, dst, cv::NORM_INF);151}152}153154}} // namespace155156157