Path: blob/master/modules/calib3d/test/test_cornerssubpix.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// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "test_precomp.hpp"42#include "opencv2/imgproc/imgproc_c.h"43#include "test_chessboardgenerator.hpp"4445namespace opencv_test { namespace {4647class CV_ChessboardSubpixelTest : public cvtest::BaseTest48{49public:50CV_ChessboardSubpixelTest();5152protected:53Mat intrinsic_matrix_;54Mat distortion_coeffs_;55Size image_size_;5657void run(int);58void generateIntrinsicParams();59};606162int calcDistance(const vector<Point2f>& set1, const vector<Point2f>& set2, double& mean_dist)63{64if(set1.size() != set2.size())65{66return 0;67}6869std::vector<int> indices;70double sum_dist = 0.0;71for(size_t i = 0; i < set1.size(); i++)72{73double min_dist = std::numeric_limits<double>::max();74int min_idx = -1;7576for(int j = 0; j < (int)set2.size(); j++)77{78double dist = cv::norm(set1[i] - set2[j]); // TODO cvtest79if(dist < min_dist)80{81min_idx = j;82min_dist = dist;83}84}8586// check validity of min_idx87if(min_idx == -1)88{89return 0;90}91std::vector<int>::iterator it = std::find(indices.begin(), indices.end(), min_idx);92if(it != indices.end())93{94// there are two points in set1 corresponding to the same point in set295return 0;96}97indices.push_back(min_idx);9899// printf("dist %d = %f\n", (int)i, min_dist);100101sum_dist += min_dist*min_dist;102}103104mean_dist = sqrt(sum_dist/set1.size());105// printf("sum_dist = %f, set1.size() = %d, mean_dist = %f\n", sum_dist, (int)set1.size(), mean_dist);106107return 1;108}109110CV_ChessboardSubpixelTest::CV_ChessboardSubpixelTest() :111intrinsic_matrix_(Size(3, 3), CV_64FC1), distortion_coeffs_(Size(1, 4), CV_64FC1),112image_size_(640, 480)113{114}115116/* ///////////////////// chess_corner_test ///////////////////////// */117void CV_ChessboardSubpixelTest::run( int )118{119int code = cvtest::TS::OK;120int progress = 0;121122RNG& rng = ts->get_rng();123124const int runs_count = 20;125const int max_pattern_size = 8;126const int min_pattern_size = 5;127Mat bg(image_size_, CV_8UC1);128bg = Scalar(0);129130double sum_dist = 0.0;131int count = 0;132for(int i = 0; i < runs_count; i++)133{134const int pattern_width = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);135const int pattern_height = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);136Size pattern_size;137if(pattern_width > pattern_height)138{139pattern_size = Size(pattern_height, pattern_width);140}141else142{143pattern_size = Size(pattern_width, pattern_height);144}145ChessBoardGenerator gen_chessboard(Size(pattern_size.width + 1, pattern_size.height + 1));146147// generates intrinsic camera and distortion matrices148generateIntrinsicParams();149150vector<Point2f> corners;151Mat chessboard_image = gen_chessboard(bg, intrinsic_matrix_, distortion_coeffs_, corners);152153vector<Point2f> test_corners;154bool result = findChessboardCorners(chessboard_image, pattern_size, test_corners, 15);155if(!result)156{157#if 0158ts->printf(cvtest::TS::LOG, "Warning: chessboard was not detected! Writing image to test.png\n");159ts->printf(cvtest::TS::LOG, "Size = %d, %d\n", pattern_size.width, pattern_size.height);160ts->printf(cvtest::TS::LOG, "Intrinsic params: fx = %f, fy = %f, cx = %f, cy = %f\n",161intrinsic_matrix_.at<double>(0, 0), intrinsic_matrix_.at<double>(1, 1),162intrinsic_matrix_.at<double>(0, 2), intrinsic_matrix_.at<double>(1, 2));163ts->printf(cvtest::TS::LOG, "Distortion matrix: %f, %f, %f, %f, %f\n",164distortion_coeffs_.at<double>(0, 0), distortion_coeffs_.at<double>(0, 1),165distortion_coeffs_.at<double>(0, 2), distortion_coeffs_.at<double>(0, 3),166distortion_coeffs_.at<double>(0, 4));167168imwrite("test.png", chessboard_image);169#endif170continue;171}172173double dist1 = 0.0;174int ret = calcDistance(corners, test_corners, dist1);175if(ret == 0)176{177ts->printf(cvtest::TS::LOG, "findChessboardCorners returns invalid corner coordinates!\n");178code = cvtest::TS::FAIL_INVALID_OUTPUT;179break;180}181182IplImage chessboard_image_header = cvIplImage(chessboard_image);183cvFindCornerSubPix(&chessboard_image_header, (CvPoint2D32f*)&test_corners[0],184(int)test_corners.size(), cvSize(3, 3), cvSize(1, 1), cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,300,0.1));185find4QuadCornerSubpix(chessboard_image, test_corners, Size(5, 5));186187double dist2 = 0.0;188ret = calcDistance(corners, test_corners, dist2);189if(ret == 0)190{191ts->printf(cvtest::TS::LOG, "findCornerSubpix returns invalid corner coordinates!\n");192code = cvtest::TS::FAIL_INVALID_OUTPUT;193break;194}195196ts->printf(cvtest::TS::LOG, "Error after findChessboardCorners: %f, after findCornerSubPix: %f\n",197dist1, dist2);198sum_dist += dist2;199count++;200201const double max_reduce_factor = 0.8;202if(dist1 < dist2*max_reduce_factor)203{204ts->printf(cvtest::TS::LOG, "findCornerSubPix increases average error!\n");205code = cvtest::TS::FAIL_INVALID_OUTPUT;206break;207}208209progress = update_progress( progress, i-1, runs_count, 0 );210}211ASSERT_NE(0, count);212sum_dist /= count;213ts->printf(cvtest::TS::LOG, "Average error after findCornerSubpix: %f\n", sum_dist);214215if( code < 0 )216ts->set_failed_test_info( code );217}218219void CV_ChessboardSubpixelTest::generateIntrinsicParams()220{221RNG& rng = ts->get_rng();222const double max_focus_length = 1000.0;223const double max_focus_diff = 5.0;224225double fx = cvtest::randReal(rng)*max_focus_length;226double fy = fx + cvtest::randReal(rng)*max_focus_diff;227double cx = image_size_.width/2;228double cy = image_size_.height/2;229230double k1 = 0.5*cvtest::randReal(rng);231double k2 = 0.05*cvtest::randReal(rng);232double p1 = 0.05*cvtest::randReal(rng);233double p2 = 0.05*cvtest::randReal(rng);234double k3 = 0.0;235236intrinsic_matrix_ = (Mat_<double>(3, 3) << fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);237distortion_coeffs_ = (Mat_<double>(1, 5) << k1, k2, p1, p2, k3);238}239240TEST(Calib3d_ChessboardSubPixDetector, accuracy) { CV_ChessboardSubpixelTest test; test.safe_run(); }241242TEST(Calib3d_CornerSubPix, regression_7204)243{244cv::Mat image(cv::Size(70, 38), CV_8UC1, cv::Scalar::all(0));245image(cv::Rect(65, 26, 5, 5)).setTo(cv::Scalar::all(255));246image(cv::Rect(55, 31, 8, 1)).setTo(cv::Scalar::all(255));247image(cv::Rect(56, 35, 14, 2)).setTo(cv::Scalar::all(255));248image(cv::Rect(66, 24, 4, 2)).setTo(cv::Scalar::all(255));249image.at<uchar>(24, 69) = 0;250std::vector<cv::Point2f> corners;251corners.push_back(cv::Point2f(65, 30));252cv::cornerSubPix(image, corners, cv::Size(3, 3), cv::Size(-1, -1),253cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));254}255256}} // namespace257/* End of file. */258259260