Path: blob/master/modules/core/test/test_operations.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"43#include "opencv2/ts/ocl_test.hpp" // T-API like tests4445namespace opencv_test {46namespace {4748class CV_OperationsTest : public cvtest::BaseTest49{50public:51CV_OperationsTest();52~CV_OperationsTest();53protected:54void run(int);5556struct test_excep57{58test_excep(const string& _s=string("")) : s(_s) { }59string s;60};6162bool SomeMatFunctions();63bool TestMat();64template<typename _Tp> void TestType(Size sz, _Tp value);65bool TestTemplateMat();66bool TestMatND();67bool TestSparseMat();68bool TestVec();69bool TestMatxMultiplication();70bool TestMatxElementwiseDivison();71bool TestSubMatAccess();72bool TestExp();73bool TestSVD();74bool operations1();7576void checkDiff(const Mat& m1, const Mat& m2, const string& s)77{78if (cvtest::norm(m1, m2, NORM_INF) != 0) throw test_excep(s);79}80void checkDiffF(const Mat& m1, const Mat& m2, const string& s)81{82if (cvtest::norm(m1, m2, NORM_INF) > 1e-5) throw test_excep(s);83}84};8586CV_OperationsTest::CV_OperationsTest()87{88}8990CV_OperationsTest::~CV_OperationsTest() {}9192#define STR(a) STR2(a)93#define STR2(a) #a9495#define CHECK_DIFF(a, b) checkDiff(a, b, "(" #a ") != (" #b ") at l." STR(__LINE__))96#define CHECK_DIFF_FLT(a, b) checkDiffF(a, b, "(" #a ") !=(eps) (" #b ") at l." STR(__LINE__))9798#if defined _MSC_VER && _MSC_VER < 140099#define MSVC_OLD 1100#else101#define MSVC_OLD 0102#endif103104template<typename _Tp> void CV_OperationsTest::TestType(Size sz, _Tp value)105{106cv::Mat_<_Tp> m(sz);107CV_Assert(m.cols == sz.width && m.rows == sz.height && m.depth() == cv::traits::Depth<_Tp>::value &&108m.channels() == DataType<_Tp>::channels &&109m.elemSize() == sizeof(_Tp) && m.step == m.elemSize()*m.cols);110for( int y = 0; y < sz.height; y++ )111for( int x = 0; x < sz.width; x++ )112{113m(y,x) = value;114}115116double s = sum(Mat(m).reshape(1))[0];117CV_Assert( s == (double)sz.width*sz.height );118}119120bool CV_OperationsTest::TestMat()121{122try123{124Mat one_3x1(3, 1, CV_32F, Scalar(1.0));125Mat shi_3x1(3, 1, CV_32F, Scalar(1.2));126Mat shi_2x1(2, 1, CV_32F, Scalar(-1));127Scalar shift = Scalar::all(15);128129float data[] = { sqrt(2.f)/2, -sqrt(2.f)/2, 1.f, sqrt(2.f)/2, sqrt(2.f)/2, 10.f };130Mat rot_2x3(2, 3, CV_32F, data);131132Mat res = one_3x1 + shi_3x1 + shi_3x1 + shi_3x1;133res = Mat(Mat(2 * rot_2x3) * res - shi_2x1) + shift;134135Mat tmp, res2;136cv::add(one_3x1, shi_3x1, tmp);137cv::add(tmp, shi_3x1, tmp);138cv::add(tmp, shi_3x1, tmp);139cv::gemm(rot_2x3, tmp, 2, shi_2x1, -1, res2, 0);140cv::add(res2, Mat(2, 1, CV_32F, shift), res2);141142CHECK_DIFF(res, res2);143144Mat mat4x4(4, 4, CV_32F);145cv::randu(mat4x4, Scalar(0), Scalar(10));146147Mat roi1 = mat4x4(Rect(Point(1, 1), Size(2, 2)));148Mat roi2 = mat4x4(Range(1, 3), Range(1, 3));149150CHECK_DIFF(roi1, roi2);151CHECK_DIFF(mat4x4, mat4x4(Rect(Point(0,0), mat4x4.size())));152153Mat intMat10(3, 3, CV_32S, Scalar(10));154Mat intMat11(3, 3, CV_32S, Scalar(11));155Mat resMat(3, 3, CV_8U, Scalar(255));156157CHECK_DIFF(resMat, intMat10 == intMat10);158CHECK_DIFF(resMat, intMat10 < intMat11);159CHECK_DIFF(resMat, intMat11 > intMat10);160CHECK_DIFF(resMat, intMat10 <= intMat11);161CHECK_DIFF(resMat, intMat11 >= intMat10);162CHECK_DIFF(resMat, intMat11 != intMat10);163164CHECK_DIFF(resMat, intMat10 == 10.0);165CHECK_DIFF(resMat, 10.0 == intMat10);166CHECK_DIFF(resMat, intMat10 < 11.0);167CHECK_DIFF(resMat, 11.0 > intMat10);168CHECK_DIFF(resMat, 10.0 < intMat11);169CHECK_DIFF(resMat, 11.0 >= intMat10);170CHECK_DIFF(resMat, 10.0 <= intMat11);171CHECK_DIFF(resMat, 10.0 != intMat11);172CHECK_DIFF(resMat, intMat11 != 10.0);173174Mat eye = Mat::eye(3, 3, CV_16S);175Mat maskMat4(3, 3, CV_16S, Scalar(4));176Mat maskMat1(3, 3, CV_16S, Scalar(1));177Mat maskMat5(3, 3, CV_16S, Scalar(5));178Mat maskMat0(3, 3, CV_16S, Scalar(0));179180CHECK_DIFF(maskMat0, maskMat4 & maskMat1);181CHECK_DIFF(maskMat0, Scalar(1) & maskMat4);182CHECK_DIFF(maskMat0, maskMat4 & Scalar(1));183184Mat m;185m = maskMat4.clone(); m &= maskMat1; CHECK_DIFF(maskMat0, m);186m = maskMat4.clone(); m &= maskMat1 | maskMat1; CHECK_DIFF(maskMat0, m);187m = maskMat4.clone(); m &= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat0, m);188189m = maskMat4.clone(); m &= Scalar(1); CHECK_DIFF(maskMat0, m);190m = maskMat4.clone(); m |= maskMat1; CHECK_DIFF(maskMat5, m);191m = maskMat5.clone(); m ^= maskMat1; CHECK_DIFF(maskMat4, m);192m = maskMat4.clone(); m |= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat5, m);193m = maskMat5.clone(); m ^= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat4, m);194195m = maskMat4.clone(); m |= Scalar(1); CHECK_DIFF(maskMat5, m);196m = maskMat5.clone(); m ^= Scalar(1); CHECK_DIFF(maskMat4, m);197198199200CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & (maskMat1 | maskMat1));201CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & maskMat1);202CHECK_DIFF(maskMat0, maskMat4 & (maskMat1 | maskMat1));203CHECK_DIFF(maskMat0, (maskMat1 | maskMat1) & Scalar(4));204CHECK_DIFF(maskMat0, Scalar(4) & (maskMat1 | maskMat1));205206CHECK_DIFF(maskMat0, maskMat5 ^ (maskMat4 | maskMat1));207CHECK_DIFF(maskMat0, (maskMat4 | maskMat1) ^ maskMat5);208CHECK_DIFF(maskMat0, (maskMat4 + maskMat1) ^ (maskMat4 + maskMat1));209CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 | Scalar(1)));210CHECK_DIFF(maskMat1, Scalar(5) ^ maskMat4);211CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 + maskMat1));212CHECK_DIFF(maskMat5, Scalar(5) | (maskMat4 + maskMat1));213CHECK_DIFF(maskMat0, (maskMat4 + maskMat1) ^ Scalar(5));214215CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ maskMat1));216CHECK_DIFF(maskMat5, (maskMat4 ^ maskMat1) | maskMat5);217CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ Scalar(1)));218CHECK_DIFF(maskMat5, (maskMat4 | maskMat4) | Scalar(1));219CHECK_DIFF(maskMat5, Scalar(1) | (maskMat4 | maskMat4));220CHECK_DIFF(maskMat5, Scalar(1) | maskMat4);221CHECK_DIFF(maskMat5, (maskMat5 | maskMat5) | (maskMat4 ^ maskMat1));222223CHECK_DIFF(maskMat1, min(maskMat1, maskMat5));224CHECK_DIFF(maskMat1, min(Mat(maskMat1 | maskMat1), maskMat5 | maskMat5));225CHECK_DIFF(maskMat5, max(maskMat1, maskMat5));226CHECK_DIFF(maskMat5, max(Mat(maskMat1 | maskMat1), maskMat5 | maskMat5));227228CHECK_DIFF(maskMat1, min(maskMat1, maskMat5 | maskMat5));229CHECK_DIFF(maskMat1, min(maskMat1 | maskMat1, maskMat5));230CHECK_DIFF(maskMat5, max(maskMat1 | maskMat1, maskMat5));231CHECK_DIFF(maskMat5, max(maskMat1, maskMat5 | maskMat5));232233CHECK_DIFF(~maskMat1, maskMat1 ^ -1);234CHECK_DIFF(~(maskMat1 | maskMat1), maskMat1 ^ -1);235236CHECK_DIFF(maskMat1, maskMat4/4.0);237238/////////////////////////////239240CHECK_DIFF(1.0 - (maskMat5 | maskMat5), -maskMat4);241CHECK_DIFF((maskMat4 | maskMat4) * 1.0 + 1.0, maskMat5);242CHECK_DIFF(1.0 + (maskMat4 | maskMat4) * 1.0, maskMat5);243CHECK_DIFF((maskMat5 | maskMat5) * 1.0 - 1.0, maskMat4);244CHECK_DIFF(5.0 - (maskMat4 | maskMat4) * 1.0, maskMat1);245CHECK_DIFF((maskMat4 | maskMat4) * 1.0 + 0.5 + 0.5, maskMat5);246CHECK_DIFF(0.5 + ((maskMat4 | maskMat4) * 1.0 + 0.5), maskMat5);247CHECK_DIFF(((maskMat4 | maskMat4) * 1.0 + 2.0) - 1.0, maskMat5);248CHECK_DIFF(5.0 - ((maskMat1 | maskMat1) * 1.0 + 3.0), maskMat1);249CHECK_DIFF( ( (maskMat1 | maskMat1) * 2.0 + 2.0) * 1.25, maskMat5);250CHECK_DIFF( 1.25 * ( (maskMat1 | maskMat1) * 2.0 + 2.0), maskMat5);251CHECK_DIFF( -( (maskMat1 | maskMat1) * (-2.0) + 1.0), maskMat1);252CHECK_DIFF( maskMat1 * 1.0 + maskMat4 * 0.5 + 2.0, maskMat5);253CHECK_DIFF( 1.0 + (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat5);254CHECK_DIFF( (maskMat1 * 1.0 + maskMat4 * 0.5 + 2.0) - 1.0, maskMat4);255CHECK_DIFF(5.0 - (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat1);256CHECK_DIFF((maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0)*1.25, maskMat5);257CHECK_DIFF(1.25 * (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat5);258CHECK_DIFF(-(maskMat1 * 2.0 + maskMat4 * (-1) + 1.0), maskMat1);259CHECK_DIFF((maskMat1 * 1.0 + maskMat4), maskMat5);260CHECK_DIFF((maskMat4 + maskMat1 * 1.0), maskMat5);261CHECK_DIFF((maskMat1 * 3.0 + 1.0) + maskMat1, maskMat5);262CHECK_DIFF(maskMat1 + (maskMat1 * 3.0 + 1.0), maskMat5);263CHECK_DIFF(maskMat1*4.0 + (maskMat1 | maskMat1), maskMat5);264CHECK_DIFF((maskMat1 | maskMat1) + maskMat1*4.0, maskMat5);265CHECK_DIFF((maskMat1*3.0 + 1.0) + (maskMat1 | maskMat1), maskMat5);266CHECK_DIFF((maskMat1 | maskMat1) + (maskMat1*3.0 + 1.0), maskMat5);267CHECK_DIFF(maskMat1*4.0 + maskMat4*2.0, maskMat1 * 12);268CHECK_DIFF((maskMat1*3.0 + 1.0) + maskMat4*2.0, maskMat1 * 12);269CHECK_DIFF(maskMat4*2.0 + (maskMat1*3.0 + 1.0), maskMat1 * 12);270CHECK_DIFF((maskMat1*3.0 + 1.0) + (maskMat1*2.0 + 2.0), maskMat1 * 8);271272CHECK_DIFF(maskMat5*1.0 - maskMat4, maskMat1);273CHECK_DIFF(maskMat5 - maskMat1 * 4.0, maskMat1);274CHECK_DIFF((maskMat4 * 1.0 + 4.0)- maskMat4, maskMat4);275CHECK_DIFF(maskMat5 - (maskMat1 * 2.0 + 2.0), maskMat1);276CHECK_DIFF(maskMat5*1.0 - (maskMat4 | maskMat4), maskMat1);277CHECK_DIFF((maskMat5 | maskMat5) - maskMat1 * 4.0, maskMat1);278CHECK_DIFF((maskMat4 * 1.0 + 4.0)- (maskMat4 | maskMat4), maskMat4);279CHECK_DIFF((maskMat5 | maskMat5) - (maskMat1 * 2.0 + 2.0), maskMat1);280CHECK_DIFF(maskMat1*5.0 - maskMat4 * 1.0, maskMat1);281CHECK_DIFF((maskMat1*5.0 + 3.0)- maskMat4 * 1.0, maskMat4);282CHECK_DIFF(maskMat4 * 2.0 - (maskMat1*4.0 + 3.0), maskMat1);283CHECK_DIFF((maskMat1 * 2.0 + 3.0) - (maskMat1*3.0 + 1.0), maskMat1);284285CHECK_DIFF((maskMat5 - maskMat4)* 4.0, maskMat4);286CHECK_DIFF(4.0 * (maskMat5 - maskMat4), maskMat4);287288CHECK_DIFF(-((maskMat4 | maskMat4) - (maskMat5 | maskMat5)), maskMat1);289290CHECK_DIFF(4.0 * (maskMat1 | maskMat1), maskMat4);291CHECK_DIFF((maskMat4 | maskMat4)/4.0, maskMat1);292293#if !MSVC_OLD294CHECK_DIFF(2.0 * (maskMat1 * 2.0) , maskMat4);295#endif296CHECK_DIFF((maskMat4 / 2.0) / 2.0 , maskMat1);297CHECK_DIFF(-(maskMat4 - maskMat5) , maskMat1);298CHECK_DIFF(-((maskMat4 - maskMat5) * 1.0), maskMat1);299300301/////////////////////////////302CHECK_DIFF(maskMat4 / maskMat4, maskMat1);303304///// Element-wise multiplication305306CHECK_DIFF(maskMat4.mul(maskMat4, 0.25), maskMat4);307CHECK_DIFF(maskMat4.mul(maskMat1 * 4, 0.25), maskMat4);308CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);309CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);310CHECK_DIFF(maskMat4.mul(maskMat4) * 0.25, maskMat4);311CHECK_DIFF(0.25 * maskMat4.mul(maskMat4), maskMat4);312313////// Element-wise division314315CHECK_DIFF(maskMat4 / maskMat4, maskMat1);316CHECK_DIFF((maskMat4 & maskMat4) / (maskMat1 * 4), maskMat1);317318CHECK_DIFF((maskMat4 & maskMat4) / maskMat4, maskMat1);319CHECK_DIFF(maskMat4 / (maskMat4 & maskMat4), maskMat1);320CHECK_DIFF((maskMat1 * 4) / maskMat4, maskMat1);321322CHECK_DIFF(maskMat4 / (maskMat1 * 4), maskMat1);323CHECK_DIFF((maskMat4 * 0.5 )/ (maskMat1 * 2), maskMat1);324325CHECK_DIFF(maskMat4 / maskMat4.mul(maskMat1), maskMat1);326CHECK_DIFF((maskMat4 & maskMat4) / maskMat4.mul(maskMat1), maskMat1);327328CHECK_DIFF(4.0 / maskMat4, maskMat1);329CHECK_DIFF(4.0 / (maskMat4 | maskMat4), maskMat1);330CHECK_DIFF(4.0 / (maskMat1 * 4.0), maskMat1);331CHECK_DIFF(4.0 / (maskMat4 / maskMat1), maskMat1);332333m = maskMat4.clone(); m/=4.0; CHECK_DIFF(m, maskMat1);334m = maskMat4.clone(); m/=maskMat4; CHECK_DIFF(m, maskMat1);335m = maskMat4.clone(); m/=(maskMat1 * 4.0); CHECK_DIFF(m, maskMat1);336m = maskMat4.clone(); m/=(maskMat4 / maskMat1); CHECK_DIFF(m, maskMat1);337338/////////////////////////////339float matrix_data[] = { 3, 1, -4, -5, 1, 0, 0, 1.1f, 1.5f};340Mat mt(3, 3, CV_32F, matrix_data);341Mat mi = mt.inv();342Mat d1 = Mat::eye(3, 3, CV_32F);343Mat d2 = d1 * 2;344MatExpr mt_tr = mt.t();345MatExpr mi_tr = mi.t();346Mat mi2 = mi * 2;347348349CHECK_DIFF_FLT( mi2 * mt, d2 );350CHECK_DIFF_FLT( mi * mt, d1 );351CHECK_DIFF_FLT( mt_tr * mi_tr, d1 );352353m = mi.clone(); m*=mt; CHECK_DIFF_FLT(m, d1);354m = mi.clone(); m*= (2 * mt - mt) ; CHECK_DIFF_FLT(m, d1);355356m = maskMat4.clone(); m+=(maskMat1 * 1.0); CHECK_DIFF(m, maskMat5);357m = maskMat5.clone(); m-=(maskMat1 * 4.0); CHECK_DIFF(m, maskMat1);358359m = maskMat1.clone(); m+=(maskMat1 * 3.0 + 1.0); CHECK_DIFF(m, maskMat5);360m = maskMat5.clone(); m-=(maskMat1 * 3.0 + 1.0); CHECK_DIFF(m, maskMat1);361#if !MSVC_OLD362m = mi.clone(); m+=(3.0 * mi * mt + d1); CHECK_DIFF_FLT(m, mi + d1 * 4);363m = mi.clone(); m-=(3.0 * mi * mt + d1); CHECK_DIFF_FLT(m, mi - d1 * 4);364m = mi.clone(); m*=(mt * 1.0); CHECK_DIFF_FLT(m, d1);365m = mi.clone(); m*=(mt * 1.0 + Mat::eye(m.size(), m.type())); CHECK_DIFF_FLT(m, d1 + mi);366m = mi.clone(); m*=mt_tr.t(); CHECK_DIFF_FLT(m, d1);367368CHECK_DIFF_FLT( (mi * 2) * mt, d2);369CHECK_DIFF_FLT( mi * (2 * mt), d2);370CHECK_DIFF_FLT( mt.t() * mi_tr, d1 );371CHECK_DIFF_FLT( mt_tr * mi.t(), d1 );372CHECK_DIFF_FLT( (mi * 0.4) * (mt * 5), d2);373374CHECK_DIFF_FLT( mt.t() * (mi_tr * 2), d2 );375CHECK_DIFF_FLT( (mt_tr * 2) * mi.t(), d2 );376377CHECK_DIFF_FLT(mt.t() * mi.t(), d1);378CHECK_DIFF_FLT( (mi * mt) * 2.0, d2);379CHECK_DIFF_FLT( 2.0 * (mi * mt), d2);380CHECK_DIFF_FLT( -(mi * mt), -d1);381382CHECK_DIFF_FLT( (mi * mt) / 2.0, d1 / 2);383384Mat mt_mul_2_plus_1;385gemm(mt, d1, 2, Mat::ones(3, 3, CV_32F), 1, mt_mul_2_plus_1);386387CHECK_DIFF( (mt * 2.0 + 1.0) * mi, mt_mul_2_plus_1 * mi); // (A*alpha + beta)*B388CHECK_DIFF( mi * (mt * 2.0 + 1.0), mi * mt_mul_2_plus_1); // A*(B*alpha + beta)389CHECK_DIFF( (mt * 2.0 + 1.0) * (mi * 2), mt_mul_2_plus_1 * mi2); // (A*alpha + beta)*(B*gamma)390CHECK_DIFF( (mi *2)* (mt * 2.0 + 1.0), mi2 * mt_mul_2_plus_1); // (A*gamma)*(B*alpha + beta)391CHECK_DIFF_FLT( (mt * 2.0 + 1.0) * mi.t(), mt_mul_2_plus_1 * mi_tr); // (A*alpha + beta)*B^t392CHECK_DIFF_FLT( mi.t() * (mt * 2.0 + 1.0), mi_tr * mt_mul_2_plus_1); // A^t*(B*alpha + beta)393394CHECK_DIFF_FLT( (mi * mt + d2)*5, d1 * 3 * 5);395CHECK_DIFF_FLT( mi * mt + d2, d1 * 3);396CHECK_DIFF_FLT( -(mi * mt) + d2, d1);397CHECK_DIFF_FLT( (mi * mt) + d1, d2);398CHECK_DIFF_FLT( d1 + (mi * mt), d2);399CHECK_DIFF_FLT( (mi * mt) - d2, -d1);400CHECK_DIFF_FLT( d2 - (mi * mt), d1);401402CHECK_DIFF_FLT( (mi * mt) + d2 * 0.5, d2);403CHECK_DIFF_FLT( d2 * 0.5 + (mi * mt), d2);404CHECK_DIFF_FLT( (mi * mt) - d1 * 2, -d1);405CHECK_DIFF_FLT( d1 * 2 - (mi * mt), d1);406407CHECK_DIFF_FLT( (mi * mt) + mi.t(), mi_tr + d1);408CHECK_DIFF_FLT( mi.t() + (mi * mt), mi_tr + d1);409CHECK_DIFF_FLT( (mi * mt) - mi.t(), d1 - mi_tr);410CHECK_DIFF_FLT( mi.t() - (mi * mt), mi_tr - d1);411412CHECK_DIFF_FLT( 2.0 *(mi * mt + d2), d1 * 6);413CHECK_DIFF_FLT( -(mi * mt + d2), d1 * -3);414415CHECK_DIFF_FLT(mt.inv() * mt, d1);416417CHECK_DIFF_FLT(mt.inv() * (2*mt - mt), d1);418#endif419}420catch (const test_excep& e)421{422ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());423ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);424return false;425}426return true;427}428429bool CV_OperationsTest::SomeMatFunctions()430{431try432{433Mat rgba( 10, 10, CV_8UC4, Scalar(1,2,3,4) );434Mat bgr( rgba.rows, rgba.cols, CV_8UC3 );435Mat alpha( rgba.rows, rgba.cols, CV_8UC1 );436Mat out[] = { bgr, alpha };437// rgba[0] -> bgr[2], rgba[1] -> bgr[1],438// rgba[2] -> bgr[0], rgba[3] -> alpha[0]439int from_to[] = { 0,2, 1,1, 2,0, 3,3 };440mixChannels( &rgba, 1, out, 2, from_to, 4 );441442Mat bgr_exp( rgba.size(), CV_8UC3, Scalar(3,2,1));443Mat alpha_exp( rgba.size(), CV_8UC1, Scalar(4));444445CHECK_DIFF(bgr_exp, bgr);446CHECK_DIFF(alpha_exp, alpha);447}448catch (const test_excep& e)449{450ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());451ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);452return false;453}454return true;455456}457458459bool CV_OperationsTest::TestSubMatAccess()460{461try462{463Mat_<float> T_bs(4,4);464Vec3f cdir(1.f, 1.f, 0.f);465Vec3f ydir(1.f, 0.f, 1.f);466Vec3f fpt(0.1f, 0.7f, 0.2f);467T_bs.setTo(0);468T_bs(Range(0,3),Range(2,3)) = 1.0*Mat(cdir); // weird OpenCV stuff, need to do multiply469T_bs(Range(0,3),Range(1,2)) = 1.0*Mat(ydir);470T_bs(Range(0,3),Range(0,1)) = 1.0*Mat(cdir.cross(ydir));471T_bs(Range(0,3),Range(3,4)) = 1.0*Mat(fpt);472T_bs(3,3) = 1.0;473//std::cout << "[Nav Grok] S frame =" << std::endl << T_bs << std::endl;474475// set up display coords, really just the S frame476std::vector<float>coords;477478for (int i=0; i<16; i++)479{480coords.push_back(T_bs(i));481//std::cout << T_bs1(i) << std::endl;482}483CV_Assert( cvtest::norm(coords, T_bs.reshape(1,1), NORM_INF) == 0 );484}485catch (const test_excep& e)486{487ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());488ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);489return false;490}491return true;492}493494bool CV_OperationsTest::TestTemplateMat()495{496try497{498Mat_<float> one_3x1(3, 1, 1.0f);499Mat_<float> shi_3x1(3, 1, 1.2f);500Mat_<float> shi_2x1(2, 1, -2);501Scalar shift = Scalar::all(15);502503float data[] = { sqrt(2.f)/2, -sqrt(2.f)/2, 1.f, sqrt(2.f)/2, sqrt(2.f)/2, 10.f };504Mat_<float> rot_2x3(2, 3, data);505506Mat_<float> res = Mat(Mat(2 * rot_2x3) * Mat(one_3x1 + shi_3x1 + shi_3x1 + shi_3x1) - shi_2x1) + shift;507Mat_<float> resS = rot_2x3 * one_3x1;508509Mat_<float> tmp, res2, resS2;510cv::add(one_3x1, shi_3x1, tmp);511cv::add(tmp, shi_3x1, tmp);512cv::add(tmp, shi_3x1, tmp);513cv::gemm(rot_2x3, tmp, 2, shi_2x1, -1, res2, 0);514cv::add(res2, Mat(2, 1, CV_32F, shift), res2);515516cv::gemm(rot_2x3, one_3x1, 1, shi_2x1, 0, resS2, 0);517CHECK_DIFF(res, res2);518CHECK_DIFF(resS, resS2);519520521Mat_<float> mat4x4(4, 4);522cv::randu(mat4x4, Scalar(0), Scalar(10));523524Mat_<float> roi1 = mat4x4(Rect(Point(1, 1), Size(2, 2)));525Mat_<float> roi2 = mat4x4(Range(1, 3), Range(1, 3));526527CHECK_DIFF(roi1, roi2);528CHECK_DIFF(mat4x4, mat4x4(Rect(Point(0,0), mat4x4.size())));529530Mat_<int> intMat10(3, 3, 10);531Mat_<int> intMat11(3, 3, 11);532Mat_<uchar> resMat(3, 3, 255);533534CHECK_DIFF(resMat, intMat10 == intMat10);535CHECK_DIFF(resMat, intMat10 < intMat11);536CHECK_DIFF(resMat, intMat11 > intMat10);537CHECK_DIFF(resMat, intMat10 <= intMat11);538CHECK_DIFF(resMat, intMat11 >= intMat10);539540CHECK_DIFF(resMat, intMat10 == 10.0);541CHECK_DIFF(resMat, intMat10 < 11.0);542CHECK_DIFF(resMat, intMat11 > 10.0);543CHECK_DIFF(resMat, intMat10 <= 11.0);544CHECK_DIFF(resMat, intMat11 >= 10.0);545546Mat_<uchar> maskMat4(3, 3, 4);547Mat_<uchar> maskMat1(3, 3, 1);548Mat_<uchar> maskMat5(3, 3, 5);549Mat_<uchar> maskMat0(3, 3, (uchar)0);550551CHECK_DIFF(maskMat0, maskMat4 & maskMat1);552CHECK_DIFF(maskMat0, Scalar(1) & maskMat4);553CHECK_DIFF(maskMat0, maskMat4 & Scalar(1));554555Mat_<uchar> m;556m = maskMat4.clone(); m&=maskMat1; CHECK_DIFF(maskMat0, m);557m = maskMat4.clone(); m&=Scalar(1); CHECK_DIFF(maskMat0, m);558559m = maskMat4.clone(); m|=maskMat1; CHECK_DIFF(maskMat5, m);560m = maskMat4.clone(); m^=maskMat1; CHECK_DIFF(maskMat5, m);561562CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & (maskMat1 | maskMat1));563CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & maskMat1);564CHECK_DIFF(maskMat0, maskMat4 & (maskMat1 | maskMat1));565566CHECK_DIFF(maskMat0, maskMat5 ^ (maskMat4 | maskMat1));567CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 | Scalar(1)));568569CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ maskMat1));570CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ Scalar(1)));571572CHECK_DIFF(~maskMat1, maskMat1 ^ 0xFF);573CHECK_DIFF(~(maskMat1 | maskMat1), maskMat1 ^ 0xFF);574575CHECK_DIFF(maskMat1 + maskMat4, maskMat5);576CHECK_DIFF(maskMat1 + Scalar(4), maskMat5);577CHECK_DIFF(Scalar(4) + maskMat1, maskMat5);578CHECK_DIFF(Scalar(4) + (maskMat1 & maskMat1), maskMat5);579580CHECK_DIFF(maskMat1 + 4.0, maskMat5);581CHECK_DIFF((maskMat1 & 0xFF) + 4.0, maskMat5);582CHECK_DIFF(4.0 + maskMat1, maskMat5);583584m = maskMat4.clone(); m+=Scalar(1); CHECK_DIFF(m, maskMat5);585m = maskMat4.clone(); m+=maskMat1; CHECK_DIFF(m, maskMat5);586m = maskMat4.clone(); m+=(maskMat1 | maskMat1); CHECK_DIFF(m, maskMat5);587588CHECK_DIFF(maskMat5 - maskMat1, maskMat4);589CHECK_DIFF(maskMat5 - Scalar(1), maskMat4);590CHECK_DIFF((maskMat5 | maskMat5) - Scalar(1), maskMat4);591CHECK_DIFF(maskMat5 - 1, maskMat4);592CHECK_DIFF((maskMat5 | maskMat5) - 1, maskMat4);593CHECK_DIFF((maskMat5 | maskMat5) - (maskMat1 | maskMat1), maskMat4);594595CHECK_DIFF(maskMat1, min(maskMat1, maskMat5));596CHECK_DIFF(maskMat5, max(maskMat1, maskMat5));597598m = maskMat5.clone(); m-=Scalar(1); CHECK_DIFF(m, maskMat4);599m = maskMat5.clone(); m-=maskMat1; CHECK_DIFF(m, maskMat4);600m = maskMat5.clone(); m-=(maskMat1 | maskMat1); CHECK_DIFF(m, maskMat4);601602m = maskMat4.clone(); m |= Scalar(1); CHECK_DIFF(maskMat5, m);603m = maskMat5.clone(); m ^= Scalar(1); CHECK_DIFF(maskMat4, m);604605CHECK_DIFF(maskMat1, maskMat4/4.0);606607Mat_<float> negf(3, 3, -3.0);608Mat_<float> posf = -negf;609Mat_<float> posf2 = posf * 2;610Mat_<int> negi(3, 3, -3);611612CHECK_DIFF(abs(negf), -negf);613CHECK_DIFF(abs(posf - posf2), -negf);614CHECK_DIFF(abs(negi), -(negi & negi));615616CHECK_DIFF(5.0 - maskMat4, maskMat1);617618619CHECK_DIFF(maskMat4.mul(maskMat4, 0.25), maskMat4);620CHECK_DIFF(maskMat4.mul(maskMat1 * 4, 0.25), maskMat4);621CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);622623624////// Element-wise division625626CHECK_DIFF(maskMat4 / maskMat4, maskMat1);627CHECK_DIFF(4.0 / maskMat4, maskMat1);628m = maskMat4.clone(); m/=4.0; CHECK_DIFF(m, maskMat1);629630////////////////////////////////631632typedef Mat_<int> TestMat_t;633634const TestMat_t cnegi = negi.clone();635636TestMat_t::iterator beg = negi.begin();637TestMat_t::iterator end = negi.end();638639TestMat_t::const_iterator cbeg = cnegi.begin();640TestMat_t::const_iterator cend = cnegi.end();641642int sum = 0;643for(; beg!=end; ++beg)644sum+=*beg;645646for(; cbeg!=cend; ++cbeg)647sum-=*cbeg;648649if (sum != 0) throw test_excep();650651CHECK_DIFF(negi.col(1), negi.col(2));652CHECK_DIFF(negi.row(1), negi.row(2));653CHECK_DIFF(negi.col(1), negi.diag());654655if (Mat_<Point2f>(1, 1).elemSize1() != sizeof(float)) throw test_excep();656if (Mat_<Point2f>(1, 1).elemSize() != 2 * sizeof(float)) throw test_excep();657if (Mat_<Point2f>(1, 1).depth() != CV_32F) throw test_excep();658if (Mat_<float>(1, 1).depth() != CV_32F) throw test_excep();659if (Mat_<int>(1, 1).depth() != CV_32S) throw test_excep();660if (Mat_<double>(1, 1).depth() != CV_64F) throw test_excep();661if (Mat_<Point3d>(1, 1).depth() != CV_64F) throw test_excep();662if (Mat_<signed char>(1, 1).depth() != CV_8S) throw test_excep();663if (Mat_<unsigned short>(1, 1).depth() != CV_16U) throw test_excep();664if (Mat_<unsigned short>(1, 1).channels() != 1) throw test_excep();665if (Mat_<Point2f>(1, 1).channels() != 2) throw test_excep();666if (Mat_<Point3f>(1, 1).channels() != 3) throw test_excep();667if (Mat_<Point3d>(1, 1).channels() != 3) throw test_excep();668669Mat_<uchar> eye = Mat_<uchar>::zeros(2, 2); CHECK_DIFF(Mat_<uchar>::zeros(Size(2, 2)), eye);670eye.at<uchar>(Point(0,0)) = 1; eye.at<uchar>(1, 1) = 1;671672CHECK_DIFF(Mat_<uchar>::eye(2, 2), eye);673CHECK_DIFF(eye, Mat_<uchar>::eye(Size(2,2)));674675Mat_<uchar> ones(2, 2, (uchar)1);676CHECK_DIFF(ones, Mat_<uchar>::ones(Size(2,2)));677CHECK_DIFF(Mat_<uchar>::ones(2, 2), ones);678679Mat_<Point2f> pntMat(2, 2, Point2f(1, 0));680if(pntMat.stepT() != 2) throw test_excep();681682uchar uchar_data[] = {1, 0, 0, 1};683684Mat_<uchar> matFromData(1, 4, uchar_data);685const Mat_<uchar> mat2 = matFromData.clone();686CHECK_DIFF(matFromData, eye.reshape(1, 1));687if (matFromData(Point(0,0)) != uchar_data[0])throw test_excep();688if (mat2(Point(0,0)) != uchar_data[0]) throw test_excep();689690if (matFromData(0,0) != uchar_data[0])throw test_excep();691if (mat2(0,0) != uchar_data[0]) throw test_excep();692693Mat_<uchar> rect(eye, Rect(0, 0, 1, 1));694if (rect.cols != 1 || rect.rows != 1 || rect(0,0) != uchar_data[0]) throw test_excep();695696//cv::Mat_<_Tp>::adjustROI(int,int,int,int)697//cv::Mat_<_Tp>::cross(const Mat_&) const698//cv::Mat_<_Tp>::Mat_(const vector<_Tp>&,bool)699//cv::Mat_<_Tp>::Mat_(int,int,_Tp*,size_t)700//cv::Mat_<_Tp>::Mat_(int,int,const _Tp&)701//cv::Mat_<_Tp>::Mat_(Size,const _Tp&)702//cv::Mat_<_Tp>::mul(const Mat_<_Tp>&,double) const703//cv::Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>,double,Mat_<_Tp>,MatOp_DivRS_<Mat> >,Mat_<_Tp> >&,double) const704//cv::Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>,double,Mat_<_Tp>,MatOp_Scale_<Mat> >,Mat_<_Tp> >&,double) const705//cv::Mat_<_Tp>::operator Mat_<T2>() const706//cv::Mat_<_Tp>::operator MatExpr_<Mat_<_Tp>,Mat_<_Tp> >() const707//cv::Mat_<_Tp>::operator()(const Range&,const Range&) const708//cv::Mat_<_Tp>::operator()(const Rect&) const709710//cv::Mat_<_Tp>::operator=(const MatExpr_Base&)711//cv::Mat_<_Tp>::operator[](int) const712713714///////////////////////////////715716float matrix_data[] = { 3, 1, -4, -5, 1, 0, 0, 1.1f, 1.5f};717Mat_<float> mt(3, 3, matrix_data);718Mat_<float> mi = mt.inv();719Mat_<float> d1 = Mat_<float>::eye(3, 3);720Mat_<float> d2 = d1 * 2;721Mat_<float> mt_tr = mt.t();722Mat_<float> mi_tr = mi.t();723Mat_<float> mi2 = mi * 2;724725CHECK_DIFF_FLT( mi2 * mt, d2 );726CHECK_DIFF_FLT( mi * mt, d1 );727CHECK_DIFF_FLT( mt_tr * mi_tr, d1 );728729Mat_<float> mf;730mf = mi.clone(); mf*=mt; CHECK_DIFF_FLT(mf, d1);731732////// typedefs //////733734if (Mat1b(1, 1).elemSize() != sizeof(uchar)) throw test_excep();735if (Mat2b(1, 1).elemSize() != 2 * sizeof(uchar)) throw test_excep();736if (Mat3b(1, 1).elemSize() != 3 * sizeof(uchar)) throw test_excep();737if (Mat1f(1, 1).elemSize() != sizeof(float)) throw test_excep();738if (Mat2f(1, 1).elemSize() != 2 * sizeof(float)) throw test_excep();739if (Mat3f(1, 1).elemSize() != 3 * sizeof(float)) throw test_excep();740if (Mat1f(1, 1).depth() != CV_32F) throw test_excep();741if (Mat3f(1, 1).depth() != CV_32F) throw test_excep();742if (Mat3f(1, 1).type() != CV_32FC3) throw test_excep();743if (Mat1i(1, 1).depth() != CV_32S) throw test_excep();744if (Mat1d(1, 1).depth() != CV_64F) throw test_excep();745if (Mat1b(1, 1).depth() != CV_8U) throw test_excep();746if (Mat3b(1, 1).type() != CV_8UC3) throw test_excep();747if (Mat1w(1, 1).depth() != CV_16U) throw test_excep();748if (Mat1s(1, 1).depth() != CV_16S) throw test_excep();749if (Mat1f(1, 1).channels() != 1) throw test_excep();750if (Mat1b(1, 1).channels() != 1) throw test_excep();751if (Mat1i(1, 1).channels() != 1) throw test_excep();752if (Mat1w(1, 1).channels() != 1) throw test_excep();753if (Mat1s(1, 1).channels() != 1) throw test_excep();754if (Mat2f(1, 1).channels() != 2) throw test_excep();755if (Mat2b(1, 1).channels() != 2) throw test_excep();756if (Mat2i(1, 1).channels() != 2) throw test_excep();757if (Mat2w(1, 1).channels() != 2) throw test_excep();758if (Mat2s(1, 1).channels() != 2) throw test_excep();759if (Mat3f(1, 1).channels() != 3) throw test_excep();760if (Mat3b(1, 1).channels() != 3) throw test_excep();761if (Mat3i(1, 1).channels() != 3) throw test_excep();762if (Mat3w(1, 1).channels() != 3) throw test_excep();763if (Mat3s(1, 1).channels() != 3) throw test_excep();764765vector<Mat_<float> > mvf, mvf2;766Mat_<Vec2f> mf2;767mvf.push_back(Mat_<float>::ones(4, 3));768mvf.push_back(Mat_<float>::zeros(4, 3));769merge(mvf, mf2);770split(mf2, mvf2);771CV_Assert( cvtest::norm(mvf2[0], mvf[0], CV_C) == 0 &&772cvtest::norm(mvf2[1], mvf[1], CV_C) == 0 );773774{775Mat a(2,2,CV_32F,1.f);776Mat b(1,2,CV_32F,1.f);777Mat c = (a*b.t()).t();778CV_Assert( cvtest::norm(c, CV_L1) == 4. );779}780781bool badarg_catched = false;782try783{784Mat m1 = Mat::zeros(1, 10, CV_8UC1);785Mat m2 = Mat::zeros(10, 10, CV_8UC3);786m1.copyTo(m2.row(1));787}788catch(const Exception&)789{790badarg_catched = true;791}792CV_Assert( badarg_catched );793794Size size(2, 5);795TestType<float>(size, 1.f);796cv::Vec3f val1(1.f);797TestType<cv::Vec3f>(size, val1);798cv::Matx31f val2(1.f);799TestType<cv::Matx31f>(size, val2);800cv::Matx41f val3(1.f);801TestType<cv::Matx41f>(size, val3);802cv::Matx32f val4(1.f);803TestType<cv::Matx32f>(size, val4);804}805catch (const test_excep& e)806{807ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());808ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);809return false;810}811return true;812}813814bool CV_OperationsTest::TestMatND()815{816int sizes[] = { 3, 3, 3};817cv::MatND nd(3, sizes, CV_32F);818819return true;820}821822bool CV_OperationsTest::TestSparseMat()823{824try825{826int sizes[] = { 10, 10, 10};827int dims = sizeof(sizes)/sizeof(sizes[0]);828SparseMat mat(dims, sizes, CV_32FC2);829830if (mat.dims() != dims) throw test_excep();831if (mat.channels() != 2) throw test_excep();832if (mat.depth() != CV_32F) throw test_excep();833834SparseMat mat2 = mat.clone();835}836catch (const test_excep&)837{838ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);839return false;840}841return true;842}843844845bool CV_OperationsTest::TestMatxMultiplication()846{847try848{849Matx33f mat(1, 1, 1, 0, 1, 1, 0, 0, 1); // Identity matrix850Point2f pt(3, 4);851Point3f res = mat * pt; // Correctly assumes homogeneous coordinates852853Vec3f res2 = mat*Vec3f(res.x, res.y, res.z);854855if(res.x != 8.0) throw test_excep();856if(res.y != 5.0) throw test_excep();857if(res.z != 1.0) throw test_excep();858859if(res2[0] != 14.0) throw test_excep();860if(res2[1] != 6.0) throw test_excep();861if(res2[2] != 1.0) throw test_excep();862863Matx44f mat44f(1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1);864Matx44d mat44d(1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1);865Scalar s(4, 3, 2, 1);866Scalar sf = mat44f*s;867Scalar sd = mat44d*s;868869if(sf[0] != 10.0) throw test_excep();870if(sf[1] != 6.0) throw test_excep();871if(sf[2] != 3.0) throw test_excep();872if(sf[3] != 1.0) throw test_excep();873874if(sd[0] != 10.0) throw test_excep();875if(sd[1] != 6.0) throw test_excep();876if(sd[2] != 3.0) throw test_excep();877if(sd[3] != 1.0) throw test_excep();878}879catch(const test_excep&)880{881ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);882return false;883}884return true;885}886887bool CV_OperationsTest::TestMatxElementwiseDivison()888{889try890{891Matx22f mat(2, 4, 6, 8);892Matx22f mat2(2, 2, 2, 2);893894Matx22f res = mat.div(mat2);895896if(res(0, 0) != 1.0) throw test_excep();897if(res(0, 1) != 2.0) throw test_excep();898if(res(1, 0) != 3.0) throw test_excep();899if(res(1, 1) != 4.0) throw test_excep();900}901catch(const test_excep&)902{903ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);904return false;905}906return true;907}908909910bool CV_OperationsTest::TestVec()911{912try913{914cv::Mat hsvImage_f(5, 5, CV_32FC3), hsvImage_b(5, 5, CV_8UC3);915int i = 0,j = 0;916cv::Vec3f a;917918//these compile919cv::Vec3b b = a;920hsvImage_f.at<cv::Vec3f>(i,j) = cv::Vec3f((float)i,0,1);921hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3b(cv::Vec3f((float)i,0,1));922923//these don't924b = cv::Vec3f(1,0,0);925cv::Vec3b c;926c = cv::Vec3f(0,0,1);927hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3f((float)i,0,1);928hsvImage_b.at<cv::Vec3b>(i,j) = a;929hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3f(1,2,3);930}931catch(const test_excep&)932{933ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);934return false;935}936return true;937}938939bool CV_OperationsTest::operations1()940{941try942{943Point3d p1(1, 1, 1), p2(2, 2, 2), p4(4, 4, 4);944p1*=2;945if (!(p1 == p2)) throw test_excep();946if (!(p2 * 2 == p4)) throw test_excep();947if (!(p2 * 2.f == p4)) throw test_excep();948if (!(p2 * 2.f == p4)) throw test_excep();949950Point2d pi1(1, 1), pi2(2, 2), pi4(4, 4);951pi1*=2;952if (!(pi1 == pi2)) throw test_excep();953if (!(pi2 * 2 == pi4)) throw test_excep();954if (!(pi2 * 2.f == pi4)) throw test_excep();955if (!(pi2 * 2.f == pi4)) throw test_excep();956957Vec2d v12(1, 1), v22(2, 2);958v12*=2.0;959if (!(v12 == v22)) throw test_excep();960961Vec3d v13(1, 1, 1), v23(2, 2, 2);962v13*=2.0;963if (!(v13 == v23)) throw test_excep();964965Vec4d v14(1, 1, 1, 1), v24(2, 2, 2, 2);966v14*=2.0;967if (!(v14 == v24)) throw test_excep();968969Size sz(10, 20);970if (sz.area() != 200) throw test_excep();971if (sz.width != 10 || sz.height != 20) throw test_excep();972if (cvSize(sz).width != 10 || cvSize(sz).height != 20) throw test_excep();973974Vec<double, 5> v5d(1, 1, 1, 1, 1);975Vec<double, 6> v6d(1, 1, 1, 1, 1, 1);976Vec<double, 7> v7d(1, 1, 1, 1, 1, 1, 1);977Vec<double, 8> v8d(1, 1, 1, 1, 1, 1, 1, 1);978Vec<double, 9> v9d(1, 1, 1, 1, 1, 1, 1, 1, 1);979Vec<double,10> v10d(1, 1, 1, 1, 1, 1, 1, 1, 1, 1);980981Vec<double,10> v10dzero;982for (int ii = 0; ii < 10; ++ii) {983if (v10dzero[ii] != 0.0)984throw test_excep();985}986987Mat A(1, 32, CV_32F), B;988for( int i = 0; i < A.cols; i++ )989A.at<float>(i) = (float)(i <= 12 ? i : 24 - i);990cv::transpose(A, B);991992int minidx[2] = {0, 0}, maxidx[2] = {0, 0};993double minval = 0, maxval = 0;994cv::minMaxIdx(A, &minval, &maxval, minidx, maxidx);995996if( !(minidx[0] == 0 && minidx[1] == 31 && maxidx[0] == 0 && maxidx[1] == 12 &&997minval == -7 && maxval == 12))998throw test_excep();9991000cv::minMaxIdx(B, &minval, &maxval, minidx, maxidx);10011002if( !(minidx[0] == 31 && minidx[1] == 0 && maxidx[0] == 12 && maxidx[1] == 0 &&1003minval == -7 && maxval == 12))1004throw test_excep();10051006Matx33f b(1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f);1007Mat c;1008cv::add(Mat::zeros(3, 3, CV_32F), b, c);1009CV_Assert( cvtest::norm(b, c, CV_C) == 0 );10101011cv::add(Mat::zeros(3, 3, CV_64F), b, c, noArray(), c.type());1012CV_Assert( cvtest::norm(b, c, CV_C) == 0 );10131014cv::add(Mat::zeros(6, 1, CV_64F), 1, c, noArray(), c.type());1015CV_Assert( cvtest::norm(Matx61f(1.f, 1.f, 1.f, 1.f, 1.f, 1.f), c, CV_C) == 0 );10161017vector<Point2f> pt2d(3);1018vector<Point3d> pt3d(2);10191020CV_Assert( Mat(pt2d).checkVector(2) == 3 && Mat(pt2d).checkVector(3) < 0 &&1021Mat(pt3d).checkVector(2) < 0 && Mat(pt3d).checkVector(3) == 2 );10221023Matx44f m44(0.8147f, 0.6324f, 0.9575f, 0.9572f,10240.9058f, 0.0975f, 0.9649f, 0.4854f,10250.1270f, 0.2785f, 0.1576f, 0.8003f,10260.9134f, 0.5469f, 0.9706f, 0.1419f);1027double d = cv::determinant(m44);1028CV_Assert( fabs(d - (-0.0262)) <= 0.001 );10291030Cv32suf z;1031z.i = 0x80000000;1032CV_Assert( cvFloor(z.f) == 0 && cvCeil(z.f) == 0 && cvRound(z.f) == 0 );1033}1034catch(const test_excep&)1035{1036ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);1037return false;1038}1039return true;1040}104110421043bool CV_OperationsTest::TestExp()1044{1045Mat1f tt = Mat1f::ones(4,2);1046Mat1f outs;1047exp(-tt, outs);1048Mat1f tt2 = Mat1f::ones(4,1), outs2;1049exp(-tt2, outs2);1050return true;1051}105210531054bool CV_OperationsTest::TestSVD()1055{1056try1057{1058Mat A = (Mat_<double>(3,4) << 1, 2, -1, 4, 2, 4, 3, 5, -1, -2, 6, 7);1059Mat x;1060SVD::solveZ(A,x);1061if( cvtest::norm(A*x, CV_C) > FLT_EPSILON )1062throw test_excep();10631064SVD svd(A, SVD::FULL_UV);1065if( cvtest::norm(A*svd.vt.row(3).t(), CV_C) > FLT_EPSILON )1066throw test_excep();10671068Mat Dp(3,3,CV_32FC1);1069Mat Dc(3,3,CV_32FC1);1070Mat Q(3,3,CV_32FC1);1071Mat U,Vt,R,T,W;10721073Dp.at<float>(0,0)=0.86483884f; Dp.at<float>(0,1)= -0.3077251f; Dp.at<float>(0,2)=-0.55711365f;1074Dp.at<float>(1,0)=0.49294353f; Dp.at<float>(1,1)=-0.24209651f; Dp.at<float>(1,2)=-0.25084701f;1075Dp.at<float>(2,0)=0; Dp.at<float>(2,1)=0; Dp.at<float>(2,2)=0;10761077Dc.at<float>(0,0)=0.75632739f; Dc.at<float>(0,1)= -0.38859656f; Dc.at<float>(0,2)=-0.36773083f;1078Dc.at<float>(1,0)=0.9699229f; Dc.at<float>(1,1)=-0.49858192f; Dc.at<float>(1,2)=-0.47134098f;1079Dc.at<float>(2,0)=0.10566688f; Dc.at<float>(2,1)=-0.060333252f; Dc.at<float>(2,2)=-0.045333147f;10801081Q=Dp*Dc.t();1082SVD decomp;1083decomp=SVD(Q);1084U=decomp.u;1085Vt=decomp.vt;1086W=decomp.w;1087Mat I = Mat::eye(3, 3, CV_32F);10881089if( cvtest::norm(U*U.t(), I, CV_C) > FLT_EPSILON ||1090cvtest::norm(Vt*Vt.t(), I, CV_C) > FLT_EPSILON ||1091W.at<float>(2) < 0 || W.at<float>(1) < W.at<float>(2) ||1092W.at<float>(0) < W.at<float>(1) ||1093cvtest::norm(U*Mat::diag(W)*Vt, Q, CV_C) > FLT_EPSILON )1094throw test_excep();1095}1096catch(const test_excep&)1097{1098ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);1099return false;1100}1101return true;1102}11031104void CV_OperationsTest::run( int /* start_from */)1105{1106if (!TestMat())1107return;11081109if (!SomeMatFunctions())1110return;11111112if (!TestTemplateMat())1113return;11141115if (!TestMatND())1116return;11171118if (!TestSparseMat())1119return;11201121if (!TestVec())1122return;11231124if (!TestMatxMultiplication())1125return;11261127if (!TestMatxElementwiseDivison())1128return;11291130if (!TestSubMatAccess())1131return;11321133if (!TestExp())1134return;11351136if (!TestSVD())1137return;11381139if (!operations1())1140return;11411142ts->set_failed_test_info(cvtest::TS::OK);1143}11441145TEST(Core_Array, expressions) { CV_OperationsTest test; test.safe_run(); }11461147class CV_SparseMatTest : public cvtest::BaseTest1148{1149public:1150CV_SparseMatTest() {}1151~CV_SparseMatTest() {}1152protected:1153void run(int)1154{1155try1156{1157RNG& rng = theRNG();1158const int MAX_DIM=3;1159int sizes[MAX_DIM], idx[MAX_DIM];1160for( int iter = 0; iter < 100; iter++ )1161{1162ts->printf(cvtest::TS::LOG, ".");1163ts->update_context(this, iter, true);1164int k, dims = rng.uniform(1, MAX_DIM+1), p = 1;1165for( k = 0; k < dims; k++ )1166{1167sizes[k] = rng.uniform(1, 30);1168p *= sizes[k];1169}1170int j, nz = rng.uniform(0, (p+2)/2), nz0 = 0;1171SparseMat_<int> v(dims,sizes);11721173CV_Assert( (int)v.nzcount() == 0 );11741175SparseMatIterator_<int> it = v.begin();1176SparseMatIterator_<int> it_end = v.end();11771178for( k = 0; it != it_end; ++it, ++k )1179;1180CV_Assert( k == 0 );11811182int sum0 = 0, sum = 0;1183for( j = 0; j < nz; j++ )1184{1185int val = rng.uniform(1, 100);1186for( k = 0; k < dims; k++ )1187idx[k] = rng.uniform(0, sizes[k]);1188if( dims == 1 )1189{1190CV_Assert( v.ref(idx[0]) == v(idx[0]) );1191}1192else if( dims == 2 )1193{1194CV_Assert( v.ref(idx[0], idx[1]) == v(idx[0], idx[1]) );1195}1196else if( dims == 3 )1197{1198CV_Assert( v.ref(idx[0], idx[1], idx[2]) == v(idx[0], idx[1], idx[2]) );1199}1200CV_Assert( v.ref(idx) == v(idx) );1201v.ref(idx) += val;1202if( v(idx) == val )1203nz0++;1204sum0 += val;1205}12061207CV_Assert( (int)v.nzcount() == nz0 );12081209it = v.begin();1210it_end = v.end();12111212for( k = 0; it != it_end; ++it, ++k )1213sum += *it;1214CV_Assert( k == nz0 && sum == sum0 );12151216v.clear();1217CV_Assert( (int)v.nzcount() == 0 );12181219it = v.begin();1220it_end = v.end();12211222for( k = 0; it != it_end; ++it, ++k )1223;1224CV_Assert( k == 0 );1225}1226}1227catch(...)1228{1229ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);1230}1231}1232};12331234TEST(Core_SparseMat, iterations) { CV_SparseMatTest test; test.safe_run(); }12351236TEST(MatTestRoi, adjustRoiOverflow)1237{1238Mat m(15, 10, CV_32S);1239Mat roi(m, cv::Range(2, 10), cv::Range(3,6));1240int rowsInROI = roi.rows;1241roi.adjustROI(1, 0, 0, 0);12421243ASSERT_EQ(roi.rows, rowsInROI + 1);12441245roi.adjustROI(-m.rows, -m.rows, 0, 0);12461247ASSERT_EQ(roi.rows, m.rows);1248}124912501251CV_ENUM(SortRowCol, SORT_EVERY_COLUMN, SORT_EVERY_ROW)1252CV_ENUM(SortOrder, SORT_ASCENDING, SORT_DESCENDING)12531254PARAM_TEST_CASE(sortIdx, MatDepth, SortRowCol, SortOrder, Size, bool)1255{1256int type;1257Size size;1258int flags;1259bool use_roi;12601261Mat src, src_roi;1262Mat dst, dst_roi;12631264virtual void SetUp()1265{1266int depth = GET_PARAM(0);1267int rowFlags = GET_PARAM(1);1268int orderFlags = GET_PARAM(2);1269size = GET_PARAM(3);1270use_roi = GET_PARAM(4);12711272type = CV_MAKE_TYPE(depth, 1);12731274flags = rowFlags | orderFlags;1275}12761277void generateTestData()1278{1279Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);1280randomSubMat(src, src_roi, size, srcBorder, type, -100, 100);12811282Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);1283randomSubMat(dst, dst_roi, size, dstBorder, CV_32S, 5, 16);1284}12851286template<typename T>1287void check_(const cv::Mat& values_, const cv::Mat_<int>& idx_)1288{1289cv::Mat_<T>& values = (cv::Mat_<T>&)values_;1290cv::Mat_<int>& idx = (cv::Mat_<int>&)idx_;1291size_t N = values.total();1292std::vector<bool> processed(N, false);1293int prevIdx = idx(0);1294T prevValue = values(prevIdx);1295processed[prevIdx] = true;1296for (size_t i = 1; i < N; i++)1297{1298int nextIdx = idx((int)i);1299T value = values(nextIdx);1300ASSERT_EQ(false, processed[nextIdx]) << "Indexes must be unique. i=" << i << " idx=" << nextIdx << std::endl << idx;1301processed[nextIdx] = true;1302if ((flags & SORT_DESCENDING) == SORT_DESCENDING)1303ASSERT_GE(prevValue, value) << "i=" << i << " prevIdx=" << prevIdx << " idx=" << nextIdx;1304else1305ASSERT_LE(prevValue, value) << "i=" << i << " prevIdx=" << prevIdx << " idx=" << nextIdx;1306prevValue = value;1307prevIdx = nextIdx;1308}1309}13101311void validate()1312{1313ASSERT_EQ(CV_32SC1, dst_roi.type());1314ASSERT_EQ(size, dst_roi.size());1315bool isColumn = (flags & SORT_EVERY_COLUMN) == SORT_EVERY_COLUMN;1316size_t N = isColumn ? src_roi.cols : src_roi.rows;1317Mat values_row((int)N, 1, type), idx_row((int)N, 1, CV_32S);1318for (size_t i = 0; i < N; i++)1319{1320SCOPED_TRACE(cv::format("row/col=%d", (int)i));1321if (isColumn)1322{1323src_roi.col((int)i).copyTo(values_row);1324dst_roi.col((int)i).copyTo(idx_row);1325}1326else1327{1328src_roi.row((int)i).copyTo(values_row);1329dst_roi.row((int)i).copyTo(idx_row);1330}1331switch(type)1332{1333case CV_8U: check_<uchar>(values_row, idx_row); break;1334case CV_8S: check_<char>(values_row, idx_row); break;1335case CV_16S: check_<short>(values_row, idx_row); break;1336case CV_32S: check_<int>(values_row, idx_row); break;1337case CV_32F: check_<float>(values_row, idx_row); break;1338case CV_64F: check_<double>(values_row, idx_row); break;1339default: ASSERT_FALSE(true) << "Unsupported type: " << type;1340}1341}1342}1343};13441345TEST_P(sortIdx, simple)1346{1347for (int j = 0; j < 5; j++)1348{1349generateTestData();13501351cv::sortIdx(src_roi, dst_roi, flags);1352validate();1353}1354}13551356INSTANTIATE_TEST_CASE_P(Core, sortIdx, Combine(1357Values(CV_8U, CV_8S, CV_16S, CV_32S, CV_32F, CV_64F), // depth1358Values(SORT_EVERY_COLUMN, SORT_EVERY_ROW),1359Values(SORT_ASCENDING, SORT_DESCENDING),1360Values(Size(3, 3), Size(16, 8)),1361::testing::Bool()1362));136313641365TEST(Core_sortIdx, regression_8941)1366{1367cv::Mat src = (cv::Mat_<int>(3, 3) <<13681, 2, 3,13690, 9, 5,13708, 1, 61371);1372cv::Mat expected = (cv::Mat_<int>(3, 1) <<13731,13740,137521376);13771378cv::Mat result;1379cv::sortIdx(src.col(0), result, CV_SORT_EVERY_COLUMN | CV_SORT_ASCENDING);1380#if 01381std::cout << src.col(0) << std::endl;1382std::cout << result << std::endl;1383#endif1384ASSERT_EQ(expected.size(), result.size());1385EXPECT_EQ(0, cvtest::norm(expected, result, NORM_INF)) <<1386"result=" << std::endl << result << std::endl <<1387"expected=" << std::endl << expected;1388}13891390}} // namespace139113921393