/*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"4344namespace opencv_test { namespace {4546/// phase correlation47class CV_PhaseCorrelatorTest : public cvtest::ArrayTest48{49public:50CV_PhaseCorrelatorTest();51protected:52void run( int );53};5455CV_PhaseCorrelatorTest::CV_PhaseCorrelatorTest() {}5657void CV_PhaseCorrelatorTest::run( int )58{59ts->set_failed_test_info(cvtest::TS::OK);6061Mat r1 = Mat::ones(Size(129, 128), CV_64F);62Mat r2 = Mat::ones(Size(129, 128), CV_64F);6364double expectedShiftX = -10.0;65double expectedShiftY = -20.0;6667// draw 10x10 rectangles @ (100, 100) and (90, 80) should see ~(-10, -20) shift here...68cv::rectangle(r1, Point(100, 100), Point(110, 110), Scalar(0, 0, 0), CV_FILLED);69cv::rectangle(r2, Point(90, 80), Point(100, 90), Scalar(0, 0, 0), CV_FILLED);7071Mat hann;72createHanningWindow(hann, r1.size(), CV_64F);73Point2d phaseShift = phaseCorrelate(r1, r2, hann);7475// test accuracy should be less than 1 pixel...76if(std::abs(expectedShiftX - phaseShift.x) >= 1 || std::abs(expectedShiftY - phaseShift.y) >= 1)77{78ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );79}80}8182TEST(Imgproc_PhaseCorrelatorTest, accuracy) { CV_PhaseCorrelatorTest test; test.safe_run(); }8384TEST(Imgproc_PhaseCorrelatorTest, accuracy_real_img)85{86Mat img = imread(cvtest::TS::ptr()->get_data_path() + "shared/airplane.png", IMREAD_GRAYSCALE);87img.convertTo(img, CV_64FC1);8889const int xLen = 129;90const int yLen = 129;91const int xShift = 40;92const int yShift = 14;9394Mat roi1 = img(Rect(xShift, yShift, xLen, yLen));95Mat roi2 = img(Rect(0, 0, xLen, yLen));9697Mat hann;98createHanningWindow(hann, roi1.size(), CV_64F);99Point2d phaseShift = phaseCorrelate(roi1, roi2, hann);100101ASSERT_NEAR(phaseShift.x, (double)xShift, 1.);102ASSERT_NEAR(phaseShift.y, (double)yShift, 1.);103}104105TEST(Imgproc_PhaseCorrelatorTest, accuracy_1d_odd_fft) {106Mat r1 = Mat::ones(Size(129, 1), CV_64F)*255; // 129 will be completed to 135 before FFT107Mat r2 = Mat::ones(Size(129, 1), CV_64F)*255;108109const int xShift = 10;110111for(int i = 6; i < 20; i++)112{113r1.at<double>(i) = 1;114r2.at<double>(i + xShift) = 1;115}116117Point2d phaseShift = phaseCorrelate(r1, r2);118119ASSERT_NEAR(phaseShift.x, (double)xShift, 1.);120}121122}} // namespace123124125