diff --git a/modules/calib3d/include/opencv2/calib3d.hpp b/modules/calib3d/include/opencv2/calib3d.hpp index 36261a6fe1..06fa957e4b 100644 --- a/modules/calib3d/include/opencv2/calib3d.hpp +++ b/modules/calib3d/include/opencv2/calib3d.hpp @@ -255,7 +255,8 @@ enum { CALIB_CB_SYMMETRIC_GRID = 1, CALIB_CB_CLUSTERING = 4 }; -enum { CALIB_USE_INTRINSIC_GUESS = 0x00001, +enum { CALIB_NINTRINSIC = 18, + CALIB_USE_INTRINSIC_GUESS = 0x00001, CALIB_FIX_ASPECT_RATIO = 0x00002, CALIB_FIX_PRINCIPAL_POINT = 0x00004, CALIB_ZERO_TANGENT_DIST = 0x00008, @@ -316,6 +317,68 @@ An example program about pose estimation from coplanar points Check @ref tutorial_homography "the corresponding tutorial" for more details */ +/** Levenberg-Marquardt solver. Starting with the specified vector of parameters it + optimizes the target vector criteria "err" + (finds local minima of each target vector component absolute value). + + When needed, it calls user-provided callback. +*/ +class CV_EXPORTS LMSolver : public Algorithm +{ +public: + class CV_EXPORTS Callback + { + public: + virtual ~Callback() {} + /** + computes error and Jacobian for the specified vector of parameters + + @param param the current vector of parameters + @param err output vector of errors: err_i = actual_f_i - ideal_f_i + @param J output Jacobian: J_ij = d(err_i)/d(param_j) + + when J=noArray(), it means that it does not need to be computed. + Dimensionality of error vector and param vector can be different. + The callback should explicitly allocate (with "create" method) each output array + (unless it's noArray()). + */ + virtual bool compute(InputArray param, OutputArray err, OutputArray J) const = 0; + }; + + /** + Runs Levenberg-Marquardt algorithm using the passed vector of parameters as the start point. + The final vector of parameters (whether the algorithm converged or not) is stored at the same + vector. The method returns the number of iterations used. If it's equal to the previously specified + maxIters, there is a big chance the algorithm did not converge. + + @param param initial/final vector of parameters. + + Note that the dimensionality of parameter space is defined by the size of param vector, + and the dimensionality of optimized criteria is defined by the size of err vector + computed by the callback. + */ + virtual int run(InputOutputArray param) const = 0; + + /** + Sets the maximum number of iterations + @param maxIters the number of iterations + */ + virtual void setMaxIters(int maxIters) = 0; + /** + Retrieves the current maximum number of iterations + */ + virtual int getMaxIters() const = 0; + + /** + Creates Levenberg-Marquard solver + + @param cb callback + @param maxIters maximum number of iterations that can be further + modified using setMaxIters() method. + */ + static Ptr create(const Ptr& cb, int maxIters); +}; + /** @brief Finds a perspective transformation between two planes. @param srcPoints Coordinates of the points in the original plane, a matrix of the type CV_32FC2 @@ -2779,4 +2842,44 @@ optimization. It stays at the center or at a different location specified when C } //end namespace cv +#if 0 //def __cplusplus +////////////////////////////////////////////////////////////////////////////////////////// +class CV_EXPORTS CvLevMarq +{ +public: + CvLevMarq(); + CvLevMarq( int nparams, int nerrs, CvTermCriteria criteria= + cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,DBL_EPSILON), + bool completeSymmFlag=false ); + ~CvLevMarq(); + void init( int nparams, int nerrs, CvTermCriteria criteria= + cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,DBL_EPSILON), + bool completeSymmFlag=false ); + bool update( const CvMat*& param, CvMat*& J, CvMat*& err ); + bool updateAlt( const CvMat*& param, CvMat*& JtJ, CvMat*& JtErr, double*& errNorm ); + + void clear(); + void step(); + enum { DONE=0, STARTED=1, CALC_J=2, CHECK_ERR=3 }; + + cv::Ptr mask; + cv::Ptr prevParam; + cv::Ptr param; + cv::Ptr J; + cv::Ptr err; + cv::Ptr JtJ; + cv::Ptr JtJN; + cv::Ptr JtErr; + cv::Ptr JtJV; + cv::Ptr JtJW; + double prevErrNorm, errNorm; + int lambdaLg10; + CvTermCriteria criteria; + int state; + int iters; + bool completeSymmFlag; + int solveMethod; +}; +#endif + #endif diff --git a/modules/calib3d/include/opencv2/calib3d/calib3d_c.h b/modules/calib3d/include/opencv2/calib3d/calib3d_c.h index f934f707b6..e2af07b2e2 100644 --- a/modules/calib3d/include/opencv2/calib3d/calib3d_c.h +++ b/modules/calib3d/include/opencv2/calib3d/calib3d_c.h @@ -44,41 +44,12 @@ #ifndef OPENCV_CALIB3D_C_H #define OPENCV_CALIB3D_C_H -#include "opencv2/core/core_c.h" +#include "opencv2/core/types_c.h" #ifdef __cplusplus extern "C" { #endif -/** @addtogroup calib3d_c - @{ - */ - -/****************************************************************************************\ -* Camera Calibration, Pose Estimation and Stereo * -\****************************************************************************************/ - -typedef struct CvPOSITObject CvPOSITObject; - -/* Allocates and initializes CvPOSITObject structure before doing cvPOSIT */ -CVAPI(CvPOSITObject*) cvCreatePOSITObject( CvPoint3D32f* points, int point_count ); - - -/* Runs POSIT (POSe from ITeration) algorithm for determining 3d position of - an object given its model and projection in a weak-perspective case */ -CVAPI(void) cvPOSIT( CvPOSITObject* posit_object, CvPoint2D32f* image_points, - double focal_length, CvTermCriteria criteria, - float* rotation_matrix, float* translation_vector); - -/* Releases CvPOSITObject structure */ -CVAPI(void) cvReleasePOSITObject( CvPOSITObject** posit_object ); - -/* updates the number of RANSAC iterations */ -CVAPI(int) cvRANSACUpdateNumIters( double p, double err_prob, - int model_points, int max_iters ); - -CVAPI(void) cvConvertPointsHomogeneous( const CvMat* src, CvMat* dst ); - /* Calculates fundamental matrix given a set of corresponding points */ #define CV_FM_7POINT 1 #define CV_FM_8POINT 2 @@ -99,136 +70,11 @@ enum CV_DLS = 3 // Joel A. Hesch and Stergios I. Roumeliotis. "A Direct Least-Squares (DLS) Method for PnP" }; -CVAPI(int) cvFindFundamentalMat( const CvMat* points1, const CvMat* points2, - CvMat* fundamental_matrix, - int method CV_DEFAULT(CV_FM_RANSAC), - double param1 CV_DEFAULT(3.), double param2 CV_DEFAULT(0.99), - CvMat* status CV_DEFAULT(NULL) ); - -/* For each input point on one of images - computes parameters of the corresponding - epipolar line on the other image */ -CVAPI(void) cvComputeCorrespondEpilines( const CvMat* points, - int which_image, - const CvMat* fundamental_matrix, - CvMat* correspondent_lines ); - -/* Triangulation functions */ - -CVAPI(void) cvTriangulatePoints(CvMat* projMatr1, CvMat* projMatr2, - CvMat* projPoints1, CvMat* projPoints2, - CvMat* points4D); - -CVAPI(void) cvCorrectMatches(CvMat* F, CvMat* points1, CvMat* points2, - CvMat* new_points1, CvMat* new_points2); - - -/* Computes the optimal new camera matrix according to the free scaling parameter alpha: - alpha=0 - only valid pixels will be retained in the undistorted image - alpha=1 - all the source image pixels will be retained in the undistorted image -*/ -CVAPI(void) cvGetOptimalNewCameraMatrix( const CvMat* camera_matrix, - const CvMat* dist_coeffs, - CvSize image_size, double alpha, - CvMat* new_camera_matrix, - CvSize new_imag_size CV_DEFAULT(cvSize(0,0)), - CvRect* valid_pixel_ROI CV_DEFAULT(0), - int center_principal_point CV_DEFAULT(0)); - -/* Converts rotation vector to rotation matrix or vice versa */ -CVAPI(int) cvRodrigues2( const CvMat* src, CvMat* dst, - CvMat* jacobian CV_DEFAULT(0) ); - -/* Finds perspective transformation between the object plane and image (view) plane */ -CVAPI(int) cvFindHomography( const CvMat* src_points, - const CvMat* dst_points, - CvMat* homography, - int method CV_DEFAULT(0), - double ransacReprojThreshold CV_DEFAULT(3), - CvMat* mask CV_DEFAULT(0), - int maxIters CV_DEFAULT(2000), - double confidence CV_DEFAULT(0.995)); - -/* Computes RQ decomposition for 3x3 matrices */ -CVAPI(void) cvRQDecomp3x3( const CvMat *matrixM, CvMat *matrixR, CvMat *matrixQ, - CvMat *matrixQx CV_DEFAULT(NULL), - CvMat *matrixQy CV_DEFAULT(NULL), - CvMat *matrixQz CV_DEFAULT(NULL), - CvPoint3D64f *eulerAngles CV_DEFAULT(NULL)); - -/* Computes projection matrix decomposition */ -CVAPI(void) cvDecomposeProjectionMatrix( const CvMat *projMatr, CvMat *calibMatr, - CvMat *rotMatr, CvMat *posVect, - CvMat *rotMatrX CV_DEFAULT(NULL), - CvMat *rotMatrY CV_DEFAULT(NULL), - CvMat *rotMatrZ CV_DEFAULT(NULL), - CvPoint3D64f *eulerAngles CV_DEFAULT(NULL)); - -/* Computes d(AB)/dA and d(AB)/dB */ -CVAPI(void) cvCalcMatMulDeriv( const CvMat* A, const CvMat* B, CvMat* dABdA, CvMat* dABdB ); - -/* Computes r3 = rodrigues(rodrigues(r2)*rodrigues(r1)), - t3 = rodrigues(r2)*t1 + t2 and the respective derivatives */ -CVAPI(void) cvComposeRT( const CvMat* _rvec1, const CvMat* _tvec1, - const CvMat* _rvec2, const CvMat* _tvec2, - CvMat* _rvec3, CvMat* _tvec3, - CvMat* dr3dr1 CV_DEFAULT(0), CvMat* dr3dt1 CV_DEFAULT(0), - CvMat* dr3dr2 CV_DEFAULT(0), CvMat* dr3dt2 CV_DEFAULT(0), - CvMat* dt3dr1 CV_DEFAULT(0), CvMat* dt3dt1 CV_DEFAULT(0), - CvMat* dt3dr2 CV_DEFAULT(0), CvMat* dt3dt2 CV_DEFAULT(0) ); - -/* Projects object points to the view plane using - the specified extrinsic and intrinsic camera parameters */ -CVAPI(void) cvProjectPoints2( const CvMat* object_points, const CvMat* rotation_vector, - const CvMat* translation_vector, const CvMat* camera_matrix, - const CvMat* distortion_coeffs, CvMat* image_points, - CvMat* dpdrot CV_DEFAULT(NULL), CvMat* dpdt CV_DEFAULT(NULL), - CvMat* dpdf CV_DEFAULT(NULL), CvMat* dpdc CV_DEFAULT(NULL), - CvMat* dpddist CV_DEFAULT(NULL), - double aspect_ratio CV_DEFAULT(0)); - -/* Finds extrinsic camera parameters from - a few known corresponding point pairs and intrinsic parameters */ -CVAPI(void) cvFindExtrinsicCameraParams2( const CvMat* object_points, - const CvMat* image_points, - const CvMat* camera_matrix, - const CvMat* distortion_coeffs, - CvMat* rotation_vector, - CvMat* translation_vector, - int use_extrinsic_guess CV_DEFAULT(0) ); - -/* Computes initial estimate of the intrinsic camera parameters - in case of planar calibration target (e.g. chessboard) */ -CVAPI(void) cvInitIntrinsicParams2D( const CvMat* object_points, - const CvMat* image_points, - const CvMat* npoints, CvSize image_size, - CvMat* camera_matrix, - double aspect_ratio CV_DEFAULT(1.) ); - #define CV_CALIB_CB_ADAPTIVE_THRESH 1 #define CV_CALIB_CB_NORMALIZE_IMAGE 2 #define CV_CALIB_CB_FILTER_QUADS 4 #define CV_CALIB_CB_FAST_CHECK 8 -// Performs a fast check if a chessboard is in the input image. This is a workaround to -// a problem of cvFindChessboardCorners being slow on images with no chessboard -// - src: input image -// - size: chessboard size -// Returns 1 if a chessboard can be in this image and findChessboardCorners should be called, -// 0 if there is no chessboard, -1 in case of error -CVAPI(int) cvCheckChessboard(IplImage* src, CvSize size); - - /* Detects corners on a chessboard calibration pattern */ -CVAPI(int) cvFindChessboardCorners( const void* image, CvSize pattern_size, - CvPoint2D32f* corners, - int* corner_count CV_DEFAULT(NULL), - int flags CV_DEFAULT(CV_CALIB_CB_ADAPTIVE_THRESH+CV_CALIB_CB_NORMALIZE_IMAGE) ); - -/* Draws individual chessboard corners or the whole chessboard detected */ -CVAPI(void) cvDrawChessboardCorners( CvArr* image, CvSize pattern_size, - CvPoint2D32f* corners, - int count, int pattern_was_found ); - #define CV_CALIB_USE_INTRINSIC_GUESS 1 #define CV_CALIB_FIX_ASPECT_RATIO 2 #define CV_CALIB_FIX_PRINCIPAL_POINT 4 @@ -249,187 +95,15 @@ CVAPI(void) cvDrawChessboardCorners( CvArr* image, CvSize pattern_size, #define CV_CALIB_NINTRINSIC 18 -/* Finds intrinsic and extrinsic camera parameters - from a few views of known calibration pattern */ -CVAPI(double) cvCalibrateCamera2( const CvMat* object_points, - const CvMat* image_points, - const CvMat* point_counts, - CvSize image_size, - CvMat* camera_matrix, - CvMat* distortion_coeffs, - CvMat* rotation_vectors CV_DEFAULT(NULL), - CvMat* translation_vectors CV_DEFAULT(NULL), - int flags CV_DEFAULT(0), - CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria( - CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,DBL_EPSILON)) ); - -/* Finds intrinsic and extrinsic camera parameters - from a few views of known calibration pattern */ -CVAPI(double) cvCalibrateCamera4( const CvMat* object_points, - const CvMat* image_points, - const CvMat* point_counts, - CvSize image_size, - int iFixedPoint, - CvMat* camera_matrix, - CvMat* distortion_coeffs, - CvMat* rotation_vectors CV_DEFAULT(NULL), - CvMat* translation_vectors CV_DEFAULT(NULL), - CvMat* newObjPoints CV_DEFAULT(NULL), - int flags CV_DEFAULT(0), - CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria( - CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,DBL_EPSILON)) ); - -/* Computes various useful characteristics of the camera from the data computed by - cvCalibrateCamera2 */ -CVAPI(void) cvCalibrationMatrixValues( const CvMat *camera_matrix, - CvSize image_size, - double aperture_width CV_DEFAULT(0), - double aperture_height CV_DEFAULT(0), - double *fovx CV_DEFAULT(NULL), - double *fovy CV_DEFAULT(NULL), - double *focal_length CV_DEFAULT(NULL), - CvPoint2D64f *principal_point CV_DEFAULT(NULL), - double *pixel_aspect_ratio CV_DEFAULT(NULL)); - #define CV_CALIB_FIX_INTRINSIC 256 #define CV_CALIB_SAME_FOCAL_LENGTH 512 -/* Computes the transformation from one camera coordinate system to another one - from a few correspondent views of the same calibration target. Optionally, calibrates - both cameras */ -CVAPI(double) cvStereoCalibrate( const CvMat* object_points, const CvMat* image_points1, - const CvMat* image_points2, const CvMat* npoints, - CvMat* camera_matrix1, CvMat* dist_coeffs1, - CvMat* camera_matrix2, CvMat* dist_coeffs2, - CvSize image_size, CvMat* R, CvMat* T, - CvMat* E CV_DEFAULT(0), CvMat* F CV_DEFAULT(0), - int flags CV_DEFAULT(CV_CALIB_FIX_INTRINSIC), - CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria( - CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,1e-6)) ); - #define CV_CALIB_ZERO_DISPARITY 1024 -/* Computes 3D rotations (+ optional shift) for each camera coordinate system to make both - views parallel (=> to make all the epipolar lines horizontal or vertical) */ -CVAPI(void) cvStereoRectify( const CvMat* camera_matrix1, const CvMat* camera_matrix2, - const CvMat* dist_coeffs1, const CvMat* dist_coeffs2, - CvSize image_size, const CvMat* R, const CvMat* T, - CvMat* R1, CvMat* R2, CvMat* P1, CvMat* P2, - CvMat* Q CV_DEFAULT(0), - int flags CV_DEFAULT(CV_CALIB_ZERO_DISPARITY), - double alpha CV_DEFAULT(-1), - CvSize new_image_size CV_DEFAULT(cvSize(0,0)), - CvRect* valid_pix_ROI1 CV_DEFAULT(0), - CvRect* valid_pix_ROI2 CV_DEFAULT(0)); - -/* Computes rectification transformations for uncalibrated pair of images using a set - of point correspondences */ -CVAPI(int) cvStereoRectifyUncalibrated( const CvMat* points1, const CvMat* points2, - const CvMat* F, CvSize img_size, - CvMat* H1, CvMat* H2, - double threshold CV_DEFAULT(5)); - - - /* stereo correspondence parameters and functions */ - #define CV_STEREO_BM_NORMALIZED_RESPONSE 0 #define CV_STEREO_BM_XSOBEL 1 -/* Block matching algorithm structure */ -typedef struct CvStereoBMState -{ - // pre-filtering (normalization of input images) - int preFilterType; // =CV_STEREO_BM_NORMALIZED_RESPONSE now - int preFilterSize; // averaging window size: ~5x5..21x21 - int preFilterCap; // the output of pre-filtering is clipped by [-preFilterCap,preFilterCap] - - // correspondence using Sum of Absolute Difference (SAD) - int SADWindowSize; // ~5x5..21x21 - int minDisparity; // minimum disparity (can be negative) - int numberOfDisparities; // maximum disparity - minimum disparity (> 0) - - // post-filtering - int textureThreshold; // the disparity is only computed for pixels - // with textured enough neighborhood - int uniquenessRatio; // accept the computed disparity d* only if - // SAD(d) >= SAD(d*)*(1 + uniquenessRatio/100.) - // for any d != d*+/-1 within the search range. - int speckleWindowSize; // disparity variation window - int speckleRange; // acceptable range of variation in window - - int trySmallerWindows; // if 1, the results may be more accurate, - // at the expense of slower processing - CvRect roi1, roi2; - int disp12MaxDiff; - - // temporary buffers - CvMat* preFilteredImg0; - CvMat* preFilteredImg1; - CvMat* slidingSumBuf; - CvMat* cost; - CvMat* disp; -} CvStereoBMState; - -#define CV_STEREO_BM_BASIC 0 -#define CV_STEREO_BM_FISH_EYE 1 -#define CV_STEREO_BM_NARROW 2 - -CVAPI(CvStereoBMState*) cvCreateStereoBMState(int preset CV_DEFAULT(CV_STEREO_BM_BASIC), - int numberOfDisparities CV_DEFAULT(0)); - -CVAPI(void) cvReleaseStereoBMState( CvStereoBMState** state ); - -CVAPI(void) cvFindStereoCorrespondenceBM( const CvArr* left, const CvArr* right, - CvArr* disparity, CvStereoBMState* state ); - -CVAPI(CvRect) cvGetValidDisparityROI( CvRect roi1, CvRect roi2, int minDisparity, - int numberOfDisparities, int SADWindowSize ); - -CVAPI(void) cvValidateDisparity( CvArr* disparity, const CvArr* cost, - int minDisparity, int numberOfDisparities, - int disp12MaxDiff CV_DEFAULT(1) ); - -/* Reprojects the computed disparity image to the 3D space using the specified 4x4 matrix */ -CVAPI(void) cvReprojectImageTo3D( const CvArr* disparityImage, - CvArr* _3dImage, const CvMat* Q, - int handleMissingValues CV_DEFAULT(0) ); - -/** @brief Transforms the input image to compensate lens distortion -@see cv::undistort -*/ -CVAPI(void) cvUndistort2( const CvArr* src, CvArr* dst, - const CvMat* camera_matrix, - const CvMat* distortion_coeffs, - const CvMat* new_camera_matrix CV_DEFAULT(0) ); - -/** @brief Computes transformation map from intrinsic camera parameters - that can used by cvRemap -*/ -CVAPI(void) cvInitUndistortMap( const CvMat* camera_matrix, - const CvMat* distortion_coeffs, - CvArr* mapx, CvArr* mapy ); - -/** @brief Computes undistortion+rectification map for a head of stereo camera -@see cv::initUndistortRectifyMap -*/ -CVAPI(void) cvInitUndistortRectifyMap( const CvMat* camera_matrix, - const CvMat* dist_coeffs, - const CvMat *R, const CvMat* new_camera_matrix, - CvArr* mapx, CvArr* mapy ); - -/** @brief Computes the original (undistorted) feature coordinates - from the observed (distorted) coordinates -@see cv::undistortPoints -*/ -CVAPI(void) cvUndistortPoints( const CvMat* src, CvMat* dst, - const CvMat* camera_matrix, - const CvMat* dist_coeffs, - const CvMat* R CV_DEFAULT(0), - const CvMat* P CV_DEFAULT(0)); - -/** @} calib3d_c */ - #ifdef __cplusplus } // extern "C" diff --git a/modules/calib3d/src/calib3d_c_api.cpp b/modules/calib3d/src/calib3d_c_api.cpp deleted file mode 100644 index 284c8a074b..0000000000 --- a/modules/calib3d/src/calib3d_c_api.cpp +++ /dev/null @@ -1,47 +0,0 @@ -// This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html. - -// This file contains wrappers for legacy OpenCV C API - -#include "precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" - -using namespace cv; - -CV_IMPL void -cvDrawChessboardCorners(CvArr* _image, CvSize pattern_size, - CvPoint2D32f* corners, int count, int found) -{ - CV_Assert(corners != NULL); //CV_CheckNULL(corners, "NULL is not allowed for 'corners' parameter"); - Mat image = cvarrToMat(_image); - CV_StaticAssert(sizeof(CvPoint2D32f) == sizeof(Point2f), ""); - drawChessboardCorners(image, pattern_size, Mat(1, count, traits::Type::value, corners), found != 0); -} - -CV_IMPL int -cvFindChessboardCorners(const void* arr, CvSize pattern_size, - CvPoint2D32f* out_corners_, int* out_corner_count, - int flags) -{ - if (!out_corners_) - CV_Error( CV_StsNullPtr, "Null pointer to corners" ); - - Mat image = cvarrToMat(arr); - std::vector out_corners; - - if (out_corner_count) - *out_corner_count = 0; - - bool res = cv::findChessboardCorners(image, pattern_size, out_corners, flags); - - int corner_count = (int)out_corners.size(); - if (out_corner_count) - *out_corner_count = corner_count; - CV_CheckLE(corner_count, Size(pattern_size).area(), "Unexpected number of corners"); - for (int i = 0; i < corner_count; ++i) - { - out_corners_[i] = cvPoint2D32f(out_corners[i]); - } - return res ? 1 : 0; -} diff --git a/modules/calib3d/src/calib3d_c_api.h b/modules/calib3d/src/calib3d_c_api.h new file mode 100644 index 0000000000..c9ac9b49f5 --- /dev/null +++ b/modules/calib3d/src/calib3d_c_api.h @@ -0,0 +1,425 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef OPENCV_CALIB3D_C_API_H +#define OPENCV_CALIB3D_C_API_H + +#include "opencv2/core/core_c.h" +#include "opencv2/calib3d/calib3d_c.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @addtogroup calib3d_c + @{ + */ + +/****************************************************************************************\ +* Camera Calibration, Pose Estimation and Stereo * +\****************************************************************************************/ + +typedef struct CvPOSITObject CvPOSITObject; + +/* Allocates and initializes CvPOSITObject structure before doing cvPOSIT */ +CvPOSITObject* cvCreatePOSITObject( CvPoint3D32f* points, int point_count ); + + +/* Runs POSIT (POSe from ITeration) algorithm for determining 3d position of + an object given its model and projection in a weak-perspective case */ +void cvPOSIT( CvPOSITObject* posit_object, CvPoint2D32f* image_points, + double focal_length, CvTermCriteria criteria, + float* rotation_matrix, float* translation_vector); + +/* Releases CvPOSITObject structure */ +void cvReleasePOSITObject( CvPOSITObject** posit_object ); + +/* updates the number of RANSAC iterations */ +int cvRANSACUpdateNumIters( double p, double err_prob, + int model_points, int max_iters ); + +void cvConvertPointsHomogeneous( const CvMat* src, CvMat* dst ); + +/* Calculates fundamental matrix given a set of corresponding points */ +/*#define CV_FM_7POINT 1 +#define CV_FM_8POINT 2 + +#define CV_LMEDS 4 +#define CV_RANSAC 8 + +#define CV_FM_LMEDS_ONLY CV_LMEDS +#define CV_FM_RANSAC_ONLY CV_RANSAC +#define CV_FM_LMEDS CV_LMEDS +#define CV_FM_RANSAC CV_RANSAC*/ + +int cvFindFundamentalMat( const CvMat* points1, const CvMat* points2, + CvMat* fundamental_matrix, + int method CV_DEFAULT(CV_FM_RANSAC), + double param1 CV_DEFAULT(3.), double param2 CV_DEFAULT(0.99), + CvMat* status CV_DEFAULT(NULL) ); + +/* For each input point on one of images + computes parameters of the corresponding + epipolar line on the other image */ +void cvComputeCorrespondEpilines( const CvMat* points, + int which_image, + const CvMat* fundamental_matrix, + CvMat* correspondent_lines ); + +/* Triangulation functions */ + +void cvTriangulatePoints(CvMat* projMatr1, CvMat* projMatr2, + CvMat* projPoints1, CvMat* projPoints2, + CvMat* points4D); + +void cvCorrectMatches(CvMat* F, CvMat* points1, CvMat* points2, + CvMat* new_points1, CvMat* new_points2); + + +/* Computes the optimal new camera matrix according to the free scaling parameter alpha: + alpha=0 - only valid pixels will be retained in the undistorted image + alpha=1 - all the source image pixels will be retained in the undistorted image +*/ +void cvGetOptimalNewCameraMatrix( const CvMat* camera_matrix, + const CvMat* dist_coeffs, + CvSize image_size, double alpha, + CvMat* new_camera_matrix, + CvSize new_imag_size CV_DEFAULT(cvSize(0,0)), + CvRect* valid_pixel_ROI CV_DEFAULT(0), + int center_principal_point CV_DEFAULT(0)); + +/* Converts rotation vector to rotation matrix or vice versa */ +int cvRodrigues2( const CvMat* src, CvMat* dst, + CvMat* jacobian CV_DEFAULT(0) ); + +/* Finds perspective transformation between the object plane and image (view) plane */ +int cvFindHomography( const CvMat* src_points, + const CvMat* dst_points, + CvMat* homography, + int method CV_DEFAULT(0), + double ransacReprojThreshold CV_DEFAULT(3), + CvMat* mask CV_DEFAULT(0), + int maxIters CV_DEFAULT(2000), + double confidence CV_DEFAULT(0.995)); + +/* Computes RQ decomposition for 3x3 matrices */ +void cvRQDecomp3x3( const CvMat *matrixM, CvMat *matrixR, CvMat *matrixQ, + CvMat *matrixQx CV_DEFAULT(NULL), + CvMat *matrixQy CV_DEFAULT(NULL), + CvMat *matrixQz CV_DEFAULT(NULL), + CvPoint3D64f *eulerAngles CV_DEFAULT(NULL)); + +/* Computes projection matrix decomposition */ +void cvDecomposeProjectionMatrix( const CvMat *projMatr, CvMat *calibMatr, + CvMat *rotMatr, CvMat *posVect, + CvMat *rotMatrX CV_DEFAULT(NULL), + CvMat *rotMatrY CV_DEFAULT(NULL), + CvMat *rotMatrZ CV_DEFAULT(NULL), + CvPoint3D64f *eulerAngles CV_DEFAULT(NULL)); + +/* Computes d(AB)/dA and d(AB)/dB */ +void cvCalcMatMulDeriv( const CvMat* A, const CvMat* B, CvMat* dABdA, CvMat* dABdB ); + +/* Computes r3 = rodrigues(rodrigues(r2)*rodrigues(r1)), + t3 = rodrigues(r2)*t1 + t2 and the respective derivatives */ +void cvComposeRT( const CvMat* _rvec1, const CvMat* _tvec1, + const CvMat* _rvec2, const CvMat* _tvec2, + CvMat* _rvec3, CvMat* _tvec3, + CvMat* dr3dr1 CV_DEFAULT(0), CvMat* dr3dt1 CV_DEFAULT(0), + CvMat* dr3dr2 CV_DEFAULT(0), CvMat* dr3dt2 CV_DEFAULT(0), + CvMat* dt3dr1 CV_DEFAULT(0), CvMat* dt3dt1 CV_DEFAULT(0), + CvMat* dt3dr2 CV_DEFAULT(0), CvMat* dt3dt2 CV_DEFAULT(0) ); + +/* Projects object points to the view plane using + the specified extrinsic and intrinsic camera parameters */ +void cvProjectPoints2( const CvMat* object_points, const CvMat* rotation_vector, + const CvMat* translation_vector, const CvMat* camera_matrix, + const CvMat* distortion_coeffs, CvMat* image_points, + CvMat* dpdrot CV_DEFAULT(NULL), CvMat* dpdt CV_DEFAULT(NULL), + CvMat* dpdf CV_DEFAULT(NULL), CvMat* dpdc CV_DEFAULT(NULL), + CvMat* dpddist CV_DEFAULT(NULL), + double aspect_ratio CV_DEFAULT(0)); + +/* Finds extrinsic camera parameters from + a few known corresponding point pairs and intrinsic parameters */ +void cvFindExtrinsicCameraParams2( const CvMat* object_points, + const CvMat* image_points, + const CvMat* camera_matrix, + const CvMat* distortion_coeffs, + CvMat* rotation_vector, + CvMat* translation_vector, + int use_extrinsic_guess CV_DEFAULT(0) ); + +/* Computes initial estimate of the intrinsic camera parameters + in case of planar calibration target (e.g. chessboard) */ +void cvInitIntrinsicParams2D( const CvMat* object_points, + const CvMat* image_points, + const CvMat* npoints, CvSize image_size, + CvMat* camera_matrix, + double aspect_ratio CV_DEFAULT(1.) ); + +// Performs a fast check if a chessboard is in the input image. This is a workaround to +// a problem of cvFindChessboardCorners being slow on images with no chessboard +// - src: input image +// - size: chessboard size +// Returns 1 if a chessboard can be in this image and findChessboardCorners should be called, +// 0 if there is no chessboard, -1 in case of error +int cvCheckChessboard(IplImage* src, CvSize size); + + /* Detects corners on a chessboard calibration pattern */ +/*int cvFindChessboardCorners( const void* image, CvSize pattern_size, + CvPoint2D32f* corners, + int* corner_count CV_DEFAULT(NULL), + int flags CV_DEFAULT(CV_CALIB_CB_ADAPTIVE_THRESH+CV_CALIB_CB_NORMALIZE_IMAGE) );*/ + +/* Draws individual chessboard corners or the whole chessboard detected */ +/*void cvDrawChessboardCorners( CvArr* image, CvSize pattern_size, + CvPoint2D32f* corners, + int count, int pattern_was_found );*/ + +/*#define CV_CALIB_USE_INTRINSIC_GUESS 1 +#define CV_CALIB_FIX_ASPECT_RATIO 2 +#define CV_CALIB_FIX_PRINCIPAL_POINT 4 +#define CV_CALIB_ZERO_TANGENT_DIST 8 +#define CV_CALIB_FIX_FOCAL_LENGTH 16 +#define CV_CALIB_FIX_K1 32 +#define CV_CALIB_FIX_K2 64 +#define CV_CALIB_FIX_K3 128 +#define CV_CALIB_FIX_K4 2048 +#define CV_CALIB_FIX_K5 4096 +#define CV_CALIB_FIX_K6 8192 +#define CV_CALIB_RATIONAL_MODEL 16384 +#define CV_CALIB_THIN_PRISM_MODEL 32768 +#define CV_CALIB_FIX_S1_S2_S3_S4 65536 +#define CV_CALIB_TILTED_MODEL 262144 +#define CV_CALIB_FIX_TAUX_TAUY 524288 +#define CV_CALIB_FIX_TANGENT_DIST 2097152 + +#define CV_CALIB_NINTRINSIC 18*/ + +/* Finds intrinsic and extrinsic camera parameters + from a few views of known calibration pattern */ +double cvCalibrateCamera2( const CvMat* object_points, + const CvMat* image_points, + const CvMat* point_counts, + CvSize image_size, + CvMat* camera_matrix, + CvMat* distortion_coeffs, + CvMat* rotation_vectors CV_DEFAULT(NULL), + CvMat* translation_vectors CV_DEFAULT(NULL), + int flags CV_DEFAULT(0), + CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria( + CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,DBL_EPSILON)) ); + +/* Finds intrinsic and extrinsic camera parameters + from a few views of known calibration pattern */ +double cvCalibrateCamera4( const CvMat* object_points, + const CvMat* image_points, + const CvMat* point_counts, + CvSize image_size, + int iFixedPoint, + CvMat* camera_matrix, + CvMat* distortion_coeffs, + CvMat* rotation_vectors CV_DEFAULT(NULL), + CvMat* translation_vectors CV_DEFAULT(NULL), + CvMat* newObjPoints CV_DEFAULT(NULL), + int flags CV_DEFAULT(0), + CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria( + CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,DBL_EPSILON)) ); + +/* Computes various useful characteristics of the camera from the data computed by + cvCalibrateCamera2 */ +void cvCalibrationMatrixValues( const CvMat *camera_matrix, + CvSize image_size, + double aperture_width CV_DEFAULT(0), + double aperture_height CV_DEFAULT(0), + double *fovx CV_DEFAULT(NULL), + double *fovy CV_DEFAULT(NULL), + double *focal_length CV_DEFAULT(NULL), + CvPoint2D64f *principal_point CV_DEFAULT(NULL), + double *pixel_aspect_ratio CV_DEFAULT(NULL)); + +/*#define CV_CALIB_FIX_INTRINSIC 256 +#define CV_CALIB_SAME_FOCAL_LENGTH 512*/ + +/* Computes the transformation from one camera coordinate system to another one + from a few correspondent views of the same calibration target. Optionally, calibrates + both cameras */ +double cvStereoCalibrate( const CvMat* object_points, const CvMat* image_points1, + const CvMat* image_points2, const CvMat* npoints, + CvMat* camera_matrix1, CvMat* dist_coeffs1, + CvMat* camera_matrix2, CvMat* dist_coeffs2, + CvSize image_size, CvMat* R, CvMat* T, + CvMat* E CV_DEFAULT(0), CvMat* F CV_DEFAULT(0), + int flags CV_DEFAULT(CV_CALIB_FIX_INTRINSIC), + CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria( + CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,1e-6)) ); + +#define CV_CALIB_ZERO_DISPARITY 1024 + +/* Computes 3D rotations (+ optional shift) for each camera coordinate system to make both + views parallel (=> to make all the epipolar lines horizontal or vertical) */ +void cvStereoRectify( const CvMat* camera_matrix1, const CvMat* camera_matrix2, + const CvMat* dist_coeffs1, const CvMat* dist_coeffs2, + CvSize image_size, const CvMat* R, const CvMat* T, + CvMat* R1, CvMat* R2, CvMat* P1, CvMat* P2, + CvMat* Q CV_DEFAULT(0), + int flags CV_DEFAULT(CV_CALIB_ZERO_DISPARITY), + double alpha CV_DEFAULT(-1), + CvSize new_image_size CV_DEFAULT(cvSize(0,0)), + CvRect* valid_pix_ROI1 CV_DEFAULT(0), + CvRect* valid_pix_ROI2 CV_DEFAULT(0)); + +/* Computes rectification transformations for uncalibrated pair of images using a set + of point correspondences */ +int cvStereoRectifyUncalibrated( const CvMat* points1, const CvMat* points2, + const CvMat* F, CvSize img_size, + CvMat* H1, CvMat* H2, + double threshold CV_DEFAULT(5)); + + + +/* stereo correspondence parameters and functions */ + +#define CV_STEREO_BM_NORMALIZED_RESPONSE 0 +#define CV_STEREO_BM_XSOBEL 1 + +/* Block matching algorithm structure */ +typedef struct CvStereoBMState +{ + // pre-filtering (normalization of input images) + int preFilterType; // =CV_STEREO_BM_NORMALIZED_RESPONSE now + int preFilterSize; // averaging window size: ~5x5..21x21 + int preFilterCap; // the output of pre-filtering is clipped by [-preFilterCap,preFilterCap] + + // correspondence using Sum of Absolute Difference (SAD) + int SADWindowSize; // ~5x5..21x21 + int minDisparity; // minimum disparity (can be negative) + int numberOfDisparities; // maximum disparity - minimum disparity (> 0) + + // post-filtering + int textureThreshold; // the disparity is only computed for pixels + // with textured enough neighborhood + int uniquenessRatio; // accept the computed disparity d* only if + // SAD(d) >= SAD(d*)*(1 + uniquenessRatio/100.) + // for any d != d*+/-1 within the search range. + int speckleWindowSize; // disparity variation window + int speckleRange; // acceptable range of variation in window + + int trySmallerWindows; // if 1, the results may be more accurate, + // at the expense of slower processing + CvRect roi1, roi2; + int disp12MaxDiff; + + // temporary buffers + CvMat* preFilteredImg0; + CvMat* preFilteredImg1; + CvMat* slidingSumBuf; + CvMat* cost; + CvMat* disp; +} CvStereoBMState; + +#define CV_STEREO_BM_BASIC 0 +#define CV_STEREO_BM_FISH_EYE 1 +#define CV_STEREO_BM_NARROW 2 + +CvStereoBMState* cvCreateStereoBMState(int preset CV_DEFAULT(CV_STEREO_BM_BASIC), + int numberOfDisparities CV_DEFAULT(0)); + +void cvReleaseStereoBMState( CvStereoBMState** state ); + +void cvFindStereoCorrespondenceBM( const CvArr* left, const CvArr* right, + CvArr* disparity, CvStereoBMState* state ); + +CvRect cvGetValidDisparityROI( CvRect roi1, CvRect roi2, int minDisparity, + int numberOfDisparities, int SADWindowSize ); + +void cvValidateDisparity( CvArr* disparity, const CvArr* cost, + int minDisparity, int numberOfDisparities, + int disp12MaxDiff CV_DEFAULT(1) ); + +/* Reprojects the computed disparity image to the 3D space using the specified 4x4 matrix */ +void cvReprojectImageTo3D( const CvArr* disparityImage, + CvArr* _3dImage, const CvMat* Q, + int handleMissingValues CV_DEFAULT(0) ); + +/** @brief Transforms the input image to compensate lens distortion +@see cv::undistort +*/ +void cvUndistort2( const CvArr* src, CvArr* dst, + const CvMat* camera_matrix, + const CvMat* distortion_coeffs, + const CvMat* new_camera_matrix CV_DEFAULT(0) ); + +/** @brief Computes transformation map from intrinsic camera parameters + that can used by cvRemap +*/ +void cvInitUndistortMap( const CvMat* camera_matrix, + const CvMat* distortion_coeffs, + CvArr* mapx, CvArr* mapy ); + +/** @brief Computes undistortion+rectification map for a head of stereo camera +@see cv::initUndistortRectifyMap +*/ +void cvInitUndistortRectifyMap( const CvMat* camera_matrix, + const CvMat* dist_coeffs, + const CvMat *R, const CvMat* new_camera_matrix, + CvArr* mapx, CvArr* mapy ); + +/** @brief Computes the original (undistorted) feature coordinates + from the observed (distorted) coordinates +@see cv::undistortPoints +*/ +void cvUndistortPoints( const CvMat* src, CvMat* dst, + const CvMat* camera_matrix, + const CvMat* dist_coeffs, + const CvMat* R CV_DEFAULT(0), + const CvMat* P CV_DEFAULT(0)); + +/** @} calib3d_c */ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* OPENCV_CALIB3D_C_API_H */ diff --git a/modules/calib3d/src/calibration.cpp b/modules/calib3d/src/calibration.cpp index df1a536cb7..6602a1bc9d 100644 --- a/modules/calib3d/src/calibration.cpp +++ b/modules/calib3d/src/calibration.cpp @@ -43,7 +43,7 @@ #include "precomp.hpp" #include "opencv2/imgproc/imgproc_c.h" #include "distortion_model.hpp" -#include "opencv2/calib3d/calib3d_c.h" +#include "calib3d_c_api.h" #include #include @@ -3003,15 +3003,22 @@ void cv::reprojectImageTo3D( InputArray _disparity, stype == CV_32SC1 || stype == CV_32FC1 ); CV_Assert( Q.size() == Size(4,4) ); + if( dtype >= 0 ) + dtype = CV_MAKETYPE(CV_MAT_DEPTH(dtype), 3); + + if( __3dImage.fixedType() ) + { + int dtype_ = __3dImage.type(); + CV_Assert( dtype == -1 || dtype == dtype_ ); + dtype = dtype_; + } + if( dtype < 0 ) dtype = CV_32FC3; else - { - dtype = CV_MAKETYPE(CV_MAT_DEPTH(dtype), 3); CV_Assert( dtype == CV_16SC3 || dtype == CV_32SC3 || dtype == CV_32FC3 ); - } - __3dImage.create(disparity.size(), CV_MAKETYPE(dtype, 3)); + __3dImage.create(disparity.size(), dtype); Mat _3dImage = __3dImage.getMat(); const float bigZ = 10000.f; @@ -3423,11 +3430,13 @@ static void collectCalibrationData( InputArrayOfArrays objectPoints, imgPtMat2, npoints ); } -static Mat prepareCameraMatrix(Mat& cameraMatrix0, int rtype) +static Mat prepareCameraMatrix(Mat& cameraMatrix0, int rtype, int flags) { Mat cameraMatrix = Mat::eye(3, 3, rtype); if( cameraMatrix0.size() == cameraMatrix.size() ) cameraMatrix0.convertTo(cameraMatrix, rtype); + else if( flags & CALIB_USE_INTRINSIC_GUESS ) + CV_Error(Error::StsBadArg, "CALIB_USE_INTRINSIC_GUESS flag is set, but the camera matrix is not 3x3"); return cameraMatrix; } @@ -3544,6 +3553,8 @@ void cv::projectPoints( InputArray _opoints, CvMat dpdrot, dpdt, dpdf, dpdc, dpddist; CvMat *pdpdrot=0, *pdpdt=0, *pdpdf=0, *pdpdc=0, *pdpddist=0; + CV_Assert( _ipoints.needed() ); + _ipoints.create(npoints, 1, CV_MAKETYPE(depth, 2), -1, true); Mat imagePoints = _ipoints.getMat(); CvMat c_imagePoints = cvMat(imagePoints); @@ -3650,8 +3661,12 @@ double cv::calibrateCameraRO(InputArrayOfArrays _objectPoints, CV_INSTRUMENT_REGION(); int rtype = CV_64F; + + CV_Assert( _cameraMatrix.needed() ); + CV_Assert( _distCoeffs.needed() ); + Mat cameraMatrix = _cameraMatrix.getMat(); - cameraMatrix = prepareCameraMatrix(cameraMatrix, rtype); + cameraMatrix = prepareCameraMatrix(cameraMatrix, rtype, flags); Mat distCoeffs = _distCoeffs.getMat(); distCoeffs = (flags & CALIB_THIN_PRISM_MODEL) && !(flags & CALIB_TILTED_MODEL) ? prepareDistCoeffs(distCoeffs, rtype, 12) : prepareDistCoeffs(distCoeffs, rtype); @@ -3864,8 +3879,8 @@ double cv::stereoCalibrate( InputArrayOfArrays _objectPoints, Mat cameraMatrix2 = _cameraMatrix2.getMat(); Mat distCoeffs1 = _distCoeffs1.getMat(); Mat distCoeffs2 = _distCoeffs2.getMat(); - cameraMatrix1 = prepareCameraMatrix(cameraMatrix1, rtype); - cameraMatrix2 = prepareCameraMatrix(cameraMatrix2, rtype); + cameraMatrix1 = prepareCameraMatrix(cameraMatrix1, rtype, flags); + cameraMatrix2 = prepareCameraMatrix(cameraMatrix2, rtype, flags); distCoeffs1 = prepareDistCoeffs(distCoeffs1, rtype); distCoeffs2 = prepareDistCoeffs(distCoeffs2, rtype); diff --git a/modules/calib3d/src/checkchessboard.cpp b/modules/calib3d/src/checkchessboard.cpp index 817fd82577..904604562d 100644 --- a/modules/calib3d/src/checkchessboard.cpp +++ b/modules/calib3d/src/checkchessboard.cpp @@ -41,7 +41,7 @@ #include "precomp.hpp" #include "opencv2/imgproc/imgproc_c.h" -#include "opencv2/calib3d/calib3d_c.h" +#include "calib3d_c_api.h" #include #include diff --git a/modules/calib3d/src/compat_ptsetreg.cpp b/modules/calib3d/src/compat_ptsetreg.cpp index 6e67000b3b..05c71cc409 100644 --- a/modules/calib3d/src/compat_ptsetreg.cpp +++ b/modules/calib3d/src/compat_ptsetreg.cpp @@ -41,7 +41,8 @@ //M*/ #include "precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" +#include "opencv2/core/core_c.h" +#include "calib3d_c_api.h" /************************************************************************************\ Some backward compatibility stuff, to be moved to legacy or compat module @@ -321,7 +322,6 @@ void CvLevMarq::step() param->data.db[i] = prevParam->data.db[i] - (mask->data.ptr[i] ? nonzero_param(j++) : 0); } - CV_IMPL int cvRANSACUpdateNumIters( double p, double ep, int modelPoints, int maxIters ) { return cv::RANSACUpdateNumIters(p, ep, modelPoints, maxIters); diff --git a/modules/calib3d/src/compat_stereo.cpp b/modules/calib3d/src/compat_stereo.cpp deleted file mode 100644 index 69e22a2d71..0000000000 --- a/modules/calib3d/src/compat_stereo.cpp +++ /dev/null @@ -1,123 +0,0 @@ -//M*////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000, Intel Corporation, all rights reserved. -// Copyright (C) 2013, OpenCV Foundation, all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" - -CvStereoBMState* cvCreateStereoBMState( int /*preset*/, int numberOfDisparities ) -{ - CvStereoBMState* state = (CvStereoBMState*)cvAlloc( sizeof(*state) ); - if( !state ) - return 0; - - state->preFilterType = CV_STEREO_BM_XSOBEL; //CV_STEREO_BM_NORMALIZED_RESPONSE; - state->preFilterSize = 9; - state->preFilterCap = 31; - state->SADWindowSize = 15; - state->minDisparity = 0; - state->numberOfDisparities = numberOfDisparities > 0 ? numberOfDisparities : 64; - state->textureThreshold = 10; - state->uniquenessRatio = 15; - state->speckleRange = state->speckleWindowSize = 0; - state->trySmallerWindows = 0; - state->roi1 = state->roi2 = cvRect(0,0,0,0); - state->disp12MaxDiff = -1; - - state->preFilteredImg0 = state->preFilteredImg1 = state->slidingSumBuf = - state->disp = state->cost = 0; - - return state; -} - -void cvReleaseStereoBMState( CvStereoBMState** state ) -{ - if( !state ) - CV_Error( CV_StsNullPtr, "" ); - - if( !*state ) - return; - - cvReleaseMat( &(*state)->preFilteredImg0 ); - cvReleaseMat( &(*state)->preFilteredImg1 ); - cvReleaseMat( &(*state)->slidingSumBuf ); - cvReleaseMat( &(*state)->disp ); - cvReleaseMat( &(*state)->cost ); - cvFree( state ); -} - -void cvFindStereoCorrespondenceBM( const CvArr* leftarr, const CvArr* rightarr, - CvArr* disparr, CvStereoBMState* state ) -{ - cv::Mat left = cv::cvarrToMat(leftarr), right = cv::cvarrToMat(rightarr); - const cv::Mat disp = cv::cvarrToMat(disparr); - - CV_Assert( state != 0 ); - - cv::Ptr sm = cv::StereoBM::create(state->numberOfDisparities, - state->SADWindowSize); - sm->setPreFilterType(state->preFilterType); - sm->setPreFilterSize(state->preFilterSize); - sm->setPreFilterCap(state->preFilterCap); - sm->setBlockSize(state->SADWindowSize); - sm->setNumDisparities(state->numberOfDisparities > 0 ? state->numberOfDisparities : 64); - sm->setTextureThreshold(state->textureThreshold); - sm->setUniquenessRatio(state->uniquenessRatio); - sm->setSpeckleRange(state->speckleRange); - sm->setSpeckleWindowSize(state->speckleWindowSize); - sm->setDisp12MaxDiff(state->disp12MaxDiff); - - sm->compute(left, right, disp); -} - -CvRect cvGetValidDisparityROI( CvRect roi1, CvRect roi2, int minDisparity, - int numberOfDisparities, int SADWindowSize ) -{ - return cvRect(cv::getValidDisparityROI( roi1, roi2, minDisparity, - numberOfDisparities, SADWindowSize)); -} - -void cvValidateDisparity( CvArr* _disp, const CvArr* _cost, int minDisparity, - int numberOfDisparities, int disp12MaxDiff ) -{ - cv::Mat disp = cv::cvarrToMat(_disp), cost = cv::cvarrToMat(_cost); - cv::validateDisparity( disp, cost, minDisparity, numberOfDisparities, disp12MaxDiff ); -} diff --git a/modules/calib3d/src/fundam.cpp b/modules/calib3d/src/fundam.cpp index e0693497ad..1cb6527305 100644 --- a/modules/calib3d/src/fundam.cpp +++ b/modules/calib3d/src/fundam.cpp @@ -411,7 +411,7 @@ cv::Mat cv::findHomography( InputArray _points1, InputArray _points2, if( method == RANSAC || method == LMEDS ) cb->runKernel( src, dst, H ); Mat H8(8, 1, CV_64F, H.ptr()); - createLMSolver(makePtr(src, dst), 10)->run(H8); + LMSolver::create(makePtr(src, dst), 10)->run(H8); } } diff --git a/modules/calib3d/src/levmarq.cpp b/modules/calib3d/src/levmarq.cpp index 0bec8680c6..4e59f043a8 100644 --- a/modules/calib3d/src/levmarq.cpp +++ b/modules/calib3d/src/levmarq.cpp @@ -198,7 +198,8 @@ public: return iter; } - void setCallback(const Ptr& _cb) CV_OVERRIDE { cb = _cb; } + void setMaxIters(int iters) CV_OVERRIDE { CV_Assert(iters > 0); maxIters = iters; } + int getMaxIters() const CV_OVERRIDE { return maxIters; } Ptr cb; @@ -209,7 +210,7 @@ public: }; -Ptr createLMSolver(const Ptr& cb, int maxIters) +Ptr LMSolver::create(const Ptr& cb, int maxIters) { return makePtr(cb, maxIters); } diff --git a/modules/calib3d/src/posit.cpp b/modules/calib3d/src/posit.cpp index 33384ef9ba..22043121d0 100644 --- a/modules/calib3d/src/posit.cpp +++ b/modules/calib3d/src/posit.cpp @@ -39,7 +39,7 @@ // //M*/ #include "precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" +#include "calib3d_c_api.h" /* POSIT structure */ struct CvPOSITObject diff --git a/modules/calib3d/src/precomp.hpp b/modules/calib3d/src/precomp.hpp index 1119107f0c..f2cb7123c0 100644 --- a/modules/calib3d/src/precomp.hpp +++ b/modules/calib3d/src/precomp.hpp @@ -76,22 +76,6 @@ namespace cv */ int RANSACUpdateNumIters( double p, double ep, int modelPoints, int maxIters ); -class CV_EXPORTS LMSolver : public Algorithm -{ -public: - class CV_EXPORTS Callback - { - public: - virtual ~Callback() {} - virtual bool compute(InputArray param, OutputArray err, OutputArray J) const = 0; - }; - - virtual void setCallback(const Ptr& cb) = 0; - virtual int run(InputOutputArray _param0) const = 0; -}; - -CV_EXPORTS Ptr createLMSolver(const Ptr& cb, int maxIters); - class CV_EXPORTS PointSetRegistrator : public Algorithm { public: diff --git a/modules/calib3d/src/ptsetreg.cpp b/modules/calib3d/src/ptsetreg.cpp index 4d2e9135d2..dc2812f5f5 100644 --- a/modules/calib3d/src/ptsetreg.cpp +++ b/modules/calib3d/src/ptsetreg.cpp @@ -863,7 +863,7 @@ Mat estimateAffine2D(InputArray _from, InputArray _to, OutputArray _inliers, Mat src = from.rowRange(0, inliers_count); Mat dst = to.rowRange(0, inliers_count); Mat Hvec = H.reshape(1, 6); - createLMSolver(makePtr(src, dst), static_cast(refineIters))->run(Hvec); + LMSolver::create(makePtr(src, dst), static_cast(refineIters))->run(Hvec); } } @@ -937,7 +937,7 @@ Mat estimateAffinePartial2D(InputArray _from, InputArray _to, OutputArray _inlie double *Hptr = H.ptr(); double Hvec_buf[4] = {Hptr[0], Hptr[3], Hptr[2], Hptr[5]}; Mat Hvec (4, 1, CV_64F, Hvec_buf); - createLMSolver(makePtr(src, dst), static_cast(refineIters))->run(Hvec); + LMSolver::create(makePtr(src, dst), static_cast(refineIters))->run(Hvec); // update H with refined parameters Hptr[0] = Hptr[4] = Hvec_buf[0]; Hptr[1] = -Hvec_buf[1]; diff --git a/modules/calib3d/src/solvepnp.cpp b/modules/calib3d/src/solvepnp.cpp index b544595bb5..0f76e8c452 100644 --- a/modules/calib3d/src/solvepnp.cpp +++ b/modules/calib3d/src/solvepnp.cpp @@ -46,7 +46,7 @@ #include "epnp.h" #include "p3p.h" #include "ap3p.h" -#include "opencv2/calib3d/calib3d_c.h" +#include "calib3d_c_api.h" #include diff --git a/modules/calib3d/src/triangulate.cpp b/modules/calib3d/src/triangulate.cpp index 90eea0100c..cded42232a 100644 --- a/modules/calib3d/src/triangulate.cpp +++ b/modules/calib3d/src/triangulate.cpp @@ -40,7 +40,7 @@ //M*/ #include "precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" +#include "opencv2/core/core_c.h" // cvCorrectMatches function is Copyright (C) 2009, Jostein Austvik Jacobsen. // cvTriangulatePoints function is derived from icvReconstructPointsFor3View, originally by Valery Mosyagin. @@ -50,8 +50,8 @@ // This method is the same as icvReconstructPointsFor3View, with only a few numbers adjusted for two-view geometry -CV_IMPL void -cvTriangulatePoints(CvMat* projMatr1, CvMat* projMatr2, CvMat* projPoints1, CvMat* projPoints2, CvMat* points4D) +static void +icvTriangulatePoints(CvMat* projMatr1, CvMat* projMatr2, CvMat* projPoints1, CvMat* projPoints2, CvMat* points4D) { if( projMatr1 == 0 || projMatr2 == 0 || projPoints1 == 0 || projPoints2 == 0 || @@ -131,8 +131,8 @@ cvTriangulatePoints(CvMat* projMatr1, CvMat* projMatr2, CvMat* projPoints1, CvMa * new_points1 : the optimized points1_. if this is NULL, the corrected points are placed back in points1_ * new_points2 : the optimized points2_. if this is NULL, the corrected points are placed back in points2_ */ -CV_IMPL void -cvCorrectMatches(CvMat *F_, CvMat *points1_, CvMat *points2_, CvMat *new_points1, CvMat *new_points2) +static void +icvCorrectMatches(CvMat *F_, CvMat *points1_, CvMat *points2_, CvMat *new_points1, CvMat *new_points2) { cv::Ptr tmp33; cv::Ptr tmp31, tmp31_2; @@ -365,7 +365,7 @@ void cv::triangulatePoints( InputArray _projMatr1, InputArray _projMatr2, Mat cvPoints4D_ = _points4D.getMat(); CvMat cvPoints4D = cvMat(cvPoints4D_); - cvTriangulatePoints(&cvMatr1, &cvMatr2, &cvPoints1, &cvPoints2, &cvPoints4D); + icvTriangulatePoints(&cvMatr1, &cvMatr2, &cvPoints1, &cvPoints2, &cvPoints4D); } void cv::correctMatches( InputArray _F, InputArray _points1, InputArray _points2, @@ -384,5 +384,5 @@ void cv::correctMatches( InputArray _F, InputArray _points1, InputArray _points2 Mat cvNewPoints1_ = _newPoints1.getMat(), cvNewPoints2_ = _newPoints2.getMat(); CvMat cvNewPoints1 = cvMat(cvNewPoints1_), cvNewPoints2 = cvMat(cvNewPoints2_); - cvCorrectMatches(&cvF, &cvPoints1, &cvPoints2, &cvNewPoints1, &cvNewPoints2); + icvCorrectMatches(&cvF, &cvPoints1, &cvPoints2, &cvNewPoints1, &cvNewPoints2); } diff --git a/modules/calib3d/src/undistort.cpp b/modules/calib3d/src/undistort.cpp index 2cdfe2ab59..6817fcd3ac 100644 --- a/modules/calib3d/src/undistort.cpp +++ b/modules/calib3d/src/undistort.cpp @@ -44,7 +44,7 @@ #include "distortion_model.hpp" #include "undistort.hpp" -#include "opencv2/calib3d/calib3d_c.h" +#include "calib3d_c_api.h" cv::Mat cv::getDefaultNewCameraMatrix( InputArray _cameraMatrix, Size imgsize, bool centerPrincipalPoint ) diff --git a/modules/calib3d/test/test_cameracalibration.cpp b/modules/calib3d/test/test_cameracalibration.cpp index d0dda46226..c013d5adf8 100644 --- a/modules/calib3d/test/test_cameracalibration.cpp +++ b/modules/calib3d/test/test_cameracalibration.cpp @@ -244,6 +244,8 @@ CV_ProjectPointsTest ProjectPoints_test; // --------------------------------- CV_CameraCalibrationTest -------------------------------------------- +typedef Matx33d RotMat; + class CV_CameraCalibrationTest : public cvtest::BaseTest { public: @@ -253,14 +255,17 @@ public: protected: int compare(double* val, double* refVal, int len, double eps, const char* paramName); - virtual void calibrate( int imageCount, int* pointCounts, - CvSize imageSize, CvPoint2D64f* imagePoints, CvPoint3D64f* objectPoints, - int iFixedPoint, double* distortionCoeffs, double* cameraMatrix, double* translationVectors, - double* rotationMatrices, double* newObjPoints, double *stdDevs, double* perViewErrors, + virtual void calibrate(Size imageSize, + const std::vector >& imagePoints, + const std::vector >& objectPoints, + int iFixedPoint, Mat& distortionCoeffs, Mat& cameraMatrix, std::vector& translationVectors, + std::vector& rotationMatrices, std::vector& newObjPoints, + std::vector& stdDevs, std::vector& perViewErrors, int flags ) = 0; - virtual void project( int pointCount, CvPoint3D64f* objectPoints, - double* rotationMatrix, double* translationVector, - double* cameraMatrix, double* distortion, CvPoint2D64f* imagePoints ) = 0; + virtual void project( const std::vector& objectPoints, + const RotMat& rotationMatrix, const Vec3d& translationVector, + const Mat& cameraMatrix, const Mat& distortion, + std::vector& imagePoints ) = 0; void run(int); }; @@ -291,53 +296,33 @@ void CV_CameraCalibrationTest::run( int start_from ) cv::String filepath; cv::String filename; - Size imageSize; - Size etalonSize; - int numImages; + std::vector > imagePoints; + std::vector > objectPoints; + std::vector > reprojectPoints; - CvPoint2D64f* imagePoints; - CvPoint3D64f* objectPoints; - CvPoint2D64f* reprojectPoints; + std::vector transVects; + std::vector rotMatrs; + std::vector newObjPoints; + std::vector stdDevs; + std::vector perViewErrors; - double* transVects; - double* rotMatrs; - double* newObjPoints; - double* stdDevs; - double* perViewErrors; + std::vector goodTransVects; + std::vector goodRotMatrs; + std::vector goodObjPoints; + std::vector goodPerViewErrors; + std::vector goodStdDevs; - double* goodTransVects; - double* goodRotMatrs; - double* goodObjPoints; - double* goodPerViewErrors; - double* goodStdDevs; + Mat cameraMatrix; + Mat distortion = Mat::zeros(1, 5, CV_64F); + Mat goodDistortion = Mat::zeros(1, 5, CV_64F); - double cameraMatrix[3*3]; - double distortion[5]={0,0,0,0,0}; - - double goodDistortion[4]; - - int* numbers; FILE* file = 0; FILE* datafile = 0; int i,j; int currImage; int currPoint; - - int calibFlags; char i_dat_file[100]; - int numPoints; - int numTests; - int currTest; - imagePoints = 0; - objectPoints = 0; - reprojectPoints = 0; - numbers = 0; - - transVects = 0; - rotMatrs = 0; - goodTransVects = 0; - goodRotMatrs = 0; int progress = 0; int values_read = -1; @@ -348,13 +333,15 @@ void CV_CameraCalibrationTest::run( int start_from ) { ts->printf( cvtest::TS::LOG, "Could not open file with list of test files: %s\n", filename.c_str() ); code = cvtest::TS::FAIL_MISSING_TEST_DATA; - goto _exit_; + ts->set_failed_test_info( code ); + return; } + int numTests = 0; values_read = fscanf(datafile,"%d",&numTests); CV_Assert(values_read == 1); - for( currTest = start_from; currTest < numTests; currTest++ ) + for( int currTest = start_from; currTest < numTests; currTest++ ) { values_read = fscanf(datafile,"%s",i_dat_file); CV_Assert(values_read == 1); @@ -370,43 +357,47 @@ void CV_CameraCalibrationTest::run( int start_from ) if( numTests == 1 ) { code = cvtest::TS::FAIL_MISSING_TEST_DATA; - goto _exit_; + break; } continue; // if there is more than one test, just skip the test } + Size imageSize; values_read = fscanf(file,"%d %d\n",&(imageSize.width),&(imageSize.height)); CV_Assert(values_read == 2); if( imageSize.width <= 0 || imageSize.height <= 0 ) { ts->printf( cvtest::TS::LOG, "Image size in test file is incorrect\n" ); code = cvtest::TS::FAIL_INVALID_TEST_DATA; - goto _exit_; + break; } /* Read etalon size */ + Size etalonSize; values_read = fscanf(file,"%d %d\n",&(etalonSize.width),&(etalonSize.height)); CV_Assert(values_read == 2); if( etalonSize.width <= 0 || etalonSize.height <= 0 ) { ts->printf( cvtest::TS::LOG, "Pattern size in test file is incorrect\n" ); code = cvtest::TS::FAIL_INVALID_TEST_DATA; - goto _exit_; + break; } - numPoints = etalonSize.width * etalonSize.height; + int numPoints = etalonSize.width * etalonSize.height; /* Read number of images */ + int numImages = 0; values_read = fscanf(file,"%d\n",&numImages); CV_Assert(values_read == 1); if( numImages <=0 ) { ts->printf( cvtest::TS::LOG, "Number of images in test file is incorrect\n"); code = cvtest::TS::FAIL_INVALID_TEST_DATA; - goto _exit_; + break; } /* Read calibration flags */ + int calibFlags = 0; values_read = fscanf(file,"%d\n",&calibFlags); CV_Assert(values_read == 1); @@ -416,41 +407,30 @@ void CV_CameraCalibrationTest::run( int start_from ) CV_Assert(values_read == 1); /* Need to allocate memory */ - imagePoints = (CvPoint2D64f*)cvAlloc( numPoints * - numImages * sizeof(CvPoint2D64f)); - - objectPoints = (CvPoint3D64f*)cvAlloc( numPoints * - numImages * sizeof(CvPoint3D64f)); - - reprojectPoints = (CvPoint2D64f*)cvAlloc( numPoints * - numImages * sizeof(CvPoint2D64f)); - - /* Alloc memory for numbers */ - numbers = (int*)cvAlloc( numImages * sizeof(int)); - - /* Fill it by numbers of points of each image*/ + imagePoints.resize(numImages); + objectPoints.resize(numImages); + reprojectPoints.resize(numImages); for( currImage = 0; currImage < numImages; currImage++ ) { - numbers[currImage] = etalonSize.width * etalonSize.height; + imagePoints[currImage].resize(numPoints); + objectPoints[currImage].resize(numPoints); + reprojectPoints[currImage].resize(numPoints); } - /* Allocate memory for translate vectors and rotmatrixs*/ - transVects = (double*)cvAlloc(3 * 1 * numImages * sizeof(double)); - rotMatrs = (double*)cvAlloc(3 * 3 * numImages * sizeof(double)); - newObjPoints = (double*)cvAlloc(3 * numbers[0] * sizeof(double)); - stdDevs = (double*)cvAlloc((CV_CALIB_NINTRINSIC + 6*numImages + 3*numbers[0]) - * sizeof(double)); - perViewErrors = (double*)cvAlloc(numImages * sizeof(double)); + transVects.resize(numImages); + rotMatrs.resize(numImages); + newObjPoints.resize(numPoints); + stdDevs.resize(CALIB_NINTRINSIC + 6*numImages + 3*numPoints); + perViewErrors.resize(numImages); - goodTransVects = (double*)cvAlloc(3 * 1 * numImages * sizeof(double)); - goodRotMatrs = (double*)cvAlloc(3 * 3 * numImages * sizeof(double)); - goodObjPoints = (double*)cvAlloc(3 * numbers[0] * sizeof(double)); - goodPerViewErrors = (double*)cvAlloc(numImages * sizeof(double)); - goodStdDevs = (double*)cvAlloc((CV_CALIB_NINTRINSIC + 6*numImages + 3*numbers[0]) - * sizeof(double)); + goodTransVects.resize(numImages); + goodRotMatrs.resize(numImages); + goodObjPoints.resize(numPoints); + goodPerViewErrors.resize(numImages); + + int nstddev = CALIB_NINTRINSIC + 6*numImages + 3*numPoints; + goodStdDevs.resize(nstddev); - /* Read object points */ - i = 0;/* shift for current point */ for( currImage = 0; currImage < numImages; currImage++ ) { for( currPoint = 0; currPoint < numPoints; currPoint++ ) @@ -459,15 +439,13 @@ void CV_CameraCalibrationTest::run( int start_from ) values_read = fscanf(file,"%lf %lf %lf\n",&x,&y,&z); CV_Assert(values_read == 3); - (objectPoints+i)->x = x; - (objectPoints+i)->y = y; - (objectPoints+i)->z = z; - i++; + objectPoints[currImage][currPoint].x = x; + objectPoints[currImage][currPoint].y = y; + objectPoints[currImage][currPoint].z = z; } } /* Read image points */ - i = 0;/* shift for current point */ for( currImage = 0; currImage < numImages; currImage++ ) { for( currPoint = 0; currPoint < numPoints; currPoint++ ) @@ -476,9 +454,8 @@ void CV_CameraCalibrationTest::run( int start_from ) values_read = fscanf(file,"%lf %lf\n",&x,&y); CV_Assert(values_read == 2); - (imagePoints+i)->x = x; - (imagePoints+i)->y = y; - i++; + imagePoints[currImage][currPoint].x = x; + imagePoints[currImage][currPoint].y = y; } } @@ -496,10 +473,10 @@ void CV_CameraCalibrationTest::run( int start_from ) /* Read distortion */ - values_read = fscanf(file,"%lf",goodDistortion+0); CV_Assert(values_read == 1); - values_read = fscanf(file,"%lf",goodDistortion+1); CV_Assert(values_read == 1); - values_read = fscanf(file,"%lf",goodDistortion+2); CV_Assert(values_read == 1); - values_read = fscanf(file,"%lf",goodDistortion+3); CV_Assert(values_read == 1); + for( i = 0; i < 4; i++ ) + { + values_read = fscanf(file,"%lf",&goodDistortion.at(i)); CV_Assert(values_read == 1); + } /* Read good Rot matrices */ for( currImage = 0; currImage < numImages; currImage++ ) @@ -507,7 +484,7 @@ void CV_CameraCalibrationTest::run( int start_from ) for( i = 0; i < 3; i++ ) for( j = 0; j < 3; j++ ) { - values_read = fscanf(file, "%lf", goodRotMatrs + currImage * 9 + j * 3 + i); + values_read = fscanf(file, "%lf", &goodRotMatrs[currImage].val[i*3+j]); CV_Assert(values_read == 1); } } @@ -517,7 +494,7 @@ void CV_CameraCalibrationTest::run( int start_from ) { for( i = 0; i < 3; i++ ) { - values_read = fscanf(file, "%lf", goodTransVects + currImage * 3 + i); + values_read = fscanf(file, "%lf", &goodTransVects[currImage].val[i]); CV_Assert(values_read == 1); } } @@ -526,44 +503,41 @@ void CV_CameraCalibrationTest::run( int start_from ) /* Read good refined 3D object points */ if( releaseObject ) { - for( i = 0; i < numbers[0]; i++ ) + for( i = 0; i < numPoints; i++ ) { for( j = 0; j < 3; j++ ) { - values_read = fscanf(file, "%lf", goodObjPoints + i * 3 + j); + values_read = fscanf(file, "%lf", &goodObjPoints[i].x + j); CV_Assert(values_read == 1); } } } /* Read good stdDeviations */ - for (i = 0; i < CV_CALIB_NINTRINSIC + numImages*6; i++) + for (i = 0; i < CALIB_NINTRINSIC + numImages*6; i++) { - values_read = fscanf(file, "%lf", goodStdDevs + i); + values_read = fscanf(file, "%lf", &goodStdDevs[i]); CV_Assert(values_read == 1); } - for( i = CV_CALIB_NINTRINSIC + numImages*6; i < CV_CALIB_NINTRINSIC + numImages*6 - + numbers[0]*3; i++ ) + for( ; i < nstddev; i++ ) { if( releaseObject ) { - values_read = fscanf(file, "%lf", goodStdDevs + i); + values_read = fscanf(file, "%lf", &goodStdDevs[i]); CV_Assert(values_read == 1); } else goodStdDevs[i] = 0.0; } - memset( cameraMatrix, 0, 9*sizeof(cameraMatrix[0]) ); - cameraMatrix[0] = cameraMatrix[4] = 807.; - cameraMatrix[2] = (imageSize.width - 1)*0.5; - cameraMatrix[5] = (imageSize.height - 1)*0.5; - cameraMatrix[8] = 1.; + cameraMatrix = Mat::zeros(3, 3, CV_64F); + cameraMatrix.at(0, 0) = cameraMatrix.at(1, 1) = 807.; + cameraMatrix.at(0, 2) = (imageSize.width - 1)*0.5; + cameraMatrix.at(1, 2) = (imageSize.height - 1)*0.5; + cameraMatrix.at(2, 2) = 1.; /* Now we can calibrate camera */ - calibrate( numImages, - numbers, - cvSize(imageSize), + calibrate( imageSize, imagePoints, objectPoints, iFixedPoint, @@ -579,23 +553,19 @@ void CV_CameraCalibrationTest::run( int start_from ) /* ---- Reproject points to the image ---- */ for( currImage = 0; currImage < numImages; currImage++ ) { - int nPoints = etalonSize.width * etalonSize.height; if( releaseObject ) { - memcpy( objectPoints + currImage * nPoints, newObjPoints, - nPoints * 3 * sizeof(double) ); + objectPoints[currImage] = newObjPoints; } - project( nPoints, - objectPoints + currImage * nPoints, - rotMatrs + currImage * 9, - transVects + currImage * 3, + project( objectPoints[currImage], + rotMatrs[currImage], + transVects[currImage], cameraMatrix, distortion, - reprojectPoints + currImage * nPoints); + reprojectPoints[currImage]); } /* ----- Compute reprojection error ----- */ - i = 0; double dx,dy; double rx,ry; double meanDx,meanDy; @@ -610,10 +580,10 @@ void CV_CameraCalibrationTest::run( int start_from ) double imageMeanDy = 0; for( currPoint = 0; currPoint < etalonSize.width * etalonSize.height; currPoint++ ) { - rx = reprojectPoints[i].x; - ry = reprojectPoints[i].y; - dx = rx - imagePoints[i].x; - dy = ry - imagePoints[i].y; + rx = reprojectPoints[currImage][currPoint].x; + ry = reprojectPoints[currImage][currPoint].y; + dx = rx - imagePoints[currImage][currPoint].x; + dy = ry - imagePoints[currImage][currPoint].y; meanDx += dx; meanDy += dy; @@ -629,7 +599,6 @@ void CV_CameraCalibrationTest::run( int start_from ) if( dy > maxDy ) maxDy = dy; - i++; } goodPerViewErrors[currImage] = sqrt( (imageMeanDx + imageMeanDy) / (etalonSize.width * etalonSize.height)); @@ -644,188 +613,121 @@ void CV_CameraCalibrationTest::run( int start_from ) meanDy /= numImages * etalonSize.width * etalonSize.height; /* ========= Compare parameters ========= */ + CV_Assert(cameraMatrix.type() == CV_64F && cameraMatrix.size() == Size(3, 3)); + CV_Assert(distortion.type() == CV_64F); + + Size dsz = distortion.size(); + CV_Assert(dsz == Size(4, 1) || dsz == Size(1, 4) || dsz == Size(5, 1) || dsz == Size(1, 5)); + + /*std::cout << "cameraMatrix: " << cameraMatrix << "\n"; + std::cout << "curr distCoeffs: " << distortion << "\n"; + std::cout << "good distCoeffs: " << goodDistortion << "\n";*/ /* ----- Compare focal lengths ----- */ - code = compare(cameraMatrix+0,&goodFcx,1,0.1,"fx"); + code = compare(&cameraMatrix.at(0, 0), &goodFcx, 1, 0.1, "fx"); if( code < 0 ) - goto _exit_; + break; - code = compare(cameraMatrix+4,&goodFcy,1,0.1,"fy"); + code = compare(&cameraMatrix.at(1, 1),&goodFcy, 1, 0.1, "fy"); if( code < 0 ) - goto _exit_; + break; /* ----- Compare principal points ----- */ - code = compare(cameraMatrix+2,&goodCx,1,0.1,"cx"); + code = compare(&cameraMatrix.at(0,2), &goodCx, 1, 0.1, "cx"); if( code < 0 ) - goto _exit_; + break; - code = compare(cameraMatrix+5,&goodCy,1,0.1,"cy"); + code = compare(&cameraMatrix.at(1,2), &goodCy, 1, 0.1, "cy"); if( code < 0 ) - goto _exit_; + break; /* ----- Compare distortion ----- */ - code = compare(distortion,goodDistortion,4,0.1,"[k1,k2,p1,p2]"); + code = compare(&distortion.at(0), &goodDistortion.at(0), 4, 0.1, "[k1,k2,p1,p2]"); if( code < 0 ) - goto _exit_; + break; /* ----- Compare rot matrixs ----- */ - code = compare(rotMatrs,goodRotMatrs, 9*numImages,0.05,"rotation matrices"); + CV_Assert(rotMatrs.size() == (size_t)numImages); + CV_Assert(transVects.size() == (size_t)numImages); + + //code = compare(rotMatrs[0].val, goodRotMatrs[0].val, 9*numImages, 0.05, "rotation matrices"); + for( i = 0; i < numImages; i++ ) + { + if( cv::norm(rotMatrs[i], goodRotMatrs[i], NORM_INF) > 0.05 ) + { + printf("rot mats for frame #%d are very different\n", i); + std::cout << "curr:\n" << rotMatrs[i] << std::endl; + std::cout << "good:\n" << goodRotMatrs[i] << std::endl; + + code = TS::FAIL_BAD_ACCURACY; + break; + } + } if( code < 0 ) - goto _exit_; + break; /* ----- Compare rot matrixs ----- */ - code = compare(transVects,goodTransVects, 3*numImages,0.1,"translation vectors"); + code = compare(transVects[0].val, goodTransVects[0].val, 3*numImages, 0.1, "translation vectors"); if( code < 0 ) - goto _exit_; + break; /* ----- Compare refined 3D object points ----- */ if( releaseObject ) { - code = compare(newObjPoints,goodObjPoints, 3*numbers[0],0.1,"refined 3D object points"); + code = compare(&newObjPoints[0].x, &goodObjPoints[0].x, 3*numPoints, 0.1, "refined 3D object points"); if( code < 0 ) - goto _exit_; + break; } /* ----- Compare per view re-projection errors ----- */ - code = compare(perViewErrors,goodPerViewErrors, numImages,0.1,"per view errors vector"); + CV_Assert(perViewErrors.size() == (size_t)numImages); + code = compare(&perViewErrors[0], &goodPerViewErrors[0], numImages, 1.1, "per view errors vector"); if( code < 0 ) - goto _exit_; + break; /* ----- Compare standard deviations of parameters ----- */ - //only for c-version of test (it does not provides evaluation of stdDevs - //and returns zeros) - for ( i = 0; i < CV_CALIB_NINTRINSIC + 6*numImages + 3*numbers[0]; i++) + if( stdDevs.size() < (size_t)nstddev ) + stdDevs.resize(nstddev); + for ( i = 0; i < nstddev; i++) { if(stdDevs[i] == 0.0) stdDevs[i] = goodStdDevs[i]; } - code = compare(stdDevs,goodStdDevs, CV_CALIB_NINTRINSIC + 6*numImages + 3*numbers[0],.5, + code = compare(&stdDevs[0], &goodStdDevs[0], nstddev, .5, "stdDevs vector"); if( code < 0 ) - goto _exit_; + break; - if( maxDx > 1.0 ) + /*if( maxDx > 1.0 ) { ts->printf( cvtest::TS::LOG, "Error in reprojection maxDx=%f > 1.0\n",maxDx); - code = cvtest::TS::FAIL_BAD_ACCURACY; goto _exit_; + code = cvtest::TS::FAIL_BAD_ACCURACY; break; } if( maxDy > 1.0 ) { ts->printf( cvtest::TS::LOG, "Error in reprojection maxDy=%f > 1.0\n",maxDy); - code = cvtest::TS::FAIL_BAD_ACCURACY; goto _exit_; - } + code = cvtest::TS::FAIL_BAD_ACCURACY; break; + }*/ progress = update_progress( progress, currTest, numTests, 0 ); - cvFree(&imagePoints); - cvFree(&objectPoints); - cvFree(&reprojectPoints); - cvFree(&numbers); - - cvFree(&transVects); - cvFree(&rotMatrs); - cvFree(&newObjPoints); - cvFree(&stdDevs); - cvFree(&perViewErrors); - cvFree(&goodTransVects); - cvFree(&goodRotMatrs); - cvFree(&goodObjPoints); - cvFree(&goodPerViewErrors); - cvFree(&goodStdDevs); - fclose(file); file = 0; } -_exit_: - if( file ) fclose(file); if( datafile ) fclose(datafile); - /* Free all allocated memory */ - cvFree(&imagePoints); - cvFree(&objectPoints); - cvFree(&reprojectPoints); - cvFree(&numbers); - - cvFree(&transVects); - cvFree(&rotMatrs); - cvFree(&goodTransVects); - cvFree(&goodRotMatrs); - if( code < 0 ) ts->set_failed_test_info( code ); } -// --------------------------------- CV_CameraCalibrationTest_C -------------------------------------------- - -class CV_CameraCalibrationTest_C : public CV_CameraCalibrationTest -{ -public: - CV_CameraCalibrationTest_C(){} -protected: - virtual void calibrate( int imageCount, int* pointCounts, - CvSize imageSize, CvPoint2D64f* imagePoints, CvPoint3D64f* objectPoints, int iFixedPoint, - double* distortionCoeffs, double* cameraMatrix, double* translationVectors, - double* rotationMatrices, double* newObjPoints, double *stdDevs, double* perViewErrors, - int flags ); - virtual void project( int pointCount, CvPoint3D64f* objectPoints, - double* rotationMatrix, double* translationVector, - double* cameraMatrix, double* distortion, CvPoint2D64f* imagePoints ); -}; - -void CV_CameraCalibrationTest_C::calibrate(int imageCount, int* pointCounts, - CvSize imageSize, CvPoint2D64f* imagePoints, CvPoint3D64f* objectPoints, int iFixedPoint, - double* distortionCoeffs, double* cameraMatrix, double* translationVectors, - double* rotationMatrices, double* newObjPoints, double *stdDevs, double *perViewErrors, - int flags ) -{ - int i, total = 0; - for( i = 0; i < imageCount; i++ ) - { - perViewErrors[i] = 0.0; - total += pointCounts[i]; - } - - for( i = 0; i < CV_CALIB_NINTRINSIC + imageCount*6 + pointCounts[0]*3; i++) - { - stdDevs[i] = 0.0; - } - - CvMat _objectPoints = cvMat(1, total, CV_64FC3, objectPoints); - CvMat _imagePoints = cvMat(1, total, CV_64FC2, imagePoints); - CvMat _pointCounts = cvMat(1, imageCount, CV_32S, pointCounts); - CvMat _cameraMatrix = cvMat(3, 3, CV_64F, cameraMatrix); - CvMat _distCoeffs = cvMat(4, 1, CV_64F, distortionCoeffs); - CvMat _rotationMatrices = cvMat(imageCount, 9, CV_64F, rotationMatrices); - CvMat _translationVectors = cvMat(imageCount, 3, CV_64F, translationVectors); - CvMat _newObjPoints = cvMat(1, pointCounts[0], CV_64FC3, newObjPoints); - - cvCalibrateCamera4(&_objectPoints, &_imagePoints, &_pointCounts, imageSize, iFixedPoint, - &_cameraMatrix, &_distCoeffs, &_rotationMatrices, &_translationVectors, - &_newObjPoints, flags); -} - -void CV_CameraCalibrationTest_C::project( int pointCount, CvPoint3D64f* objectPoints, - double* rotationMatrix, double* translationVector, - double* cameraMatrix, double* distortion, CvPoint2D64f* imagePoints ) -{ - CvMat _objectPoints = cvMat(1, pointCount, CV_64FC3, objectPoints); - CvMat _imagePoints = cvMat(1, pointCount, CV_64FC2, imagePoints); - CvMat _cameraMatrix = cvMat(3, 3, CV_64F, cameraMatrix); - CvMat _distCoeffs = cvMat(4, 1, CV_64F, distortion); - CvMat _rotationMatrix = cvMat(3, 3, CV_64F, rotationMatrix); - CvMat _translationVector = cvMat(1, 3, CV_64F, translationVector); - - cvProjectPoints2(&_objectPoints, &_rotationMatrix, &_translationVector, &_cameraMatrix, &_distCoeffs, &_imagePoints); -} - // --------------------------------- CV_CameraCalibrationTest_CPP -------------------------------------------- class CV_CameraCalibrationTest_CPP : public CV_CameraCalibrationTest @@ -833,25 +735,31 @@ class CV_CameraCalibrationTest_CPP : public CV_CameraCalibrationTest public: CV_CameraCalibrationTest_CPP(){} protected: - virtual void calibrate( int imageCount, int* pointCounts, - CvSize imageSize, CvPoint2D64f* imagePoints, CvPoint3D64f* objectPoints, int iFixedPoint, - double* distortionCoeffs, double* cameraMatrix, double* translationVectors, - double* rotationMatrices, double* newObjPoints, double *stdDevs, double* perViewErrors, - int flags ); - virtual void project( int pointCount, CvPoint3D64f* objectPoints, - double* rotationMatrix, double* translationVector, - double* cameraMatrix, double* distortion, CvPoint2D64f* imagePoints ); + virtual void calibrate(Size imageSize, + const std::vector >& imagePoints, + const std::vector >& objectPoints, + int iFixedPoint, Mat& distortionCoeffs, Mat& cameraMatrix, std::vector& translationVectors, + std::vector& rotationMatrices, std::vector& newObjPoints, + std::vector& stdDevs, std::vector& perViewErrors, + int flags ); + virtual void project( const std::vector& objectPoints, + const RotMat& rotationMatrix, const Vec3d& translationVector, + const Mat& cameraMatrix, const Mat& distortion, + std::vector& imagePoints ); }; -void CV_CameraCalibrationTest_CPP::calibrate(int imageCount, int* pointCounts, - CvSize _imageSize, CvPoint2D64f* _imagePoints, CvPoint3D64f* _objectPoints, int iFixedPoint, - double* _distortionCoeffs, double* _cameraMatrix, double* translationVectors, - double* rotationMatrices, double* newObjPoints, double *stdDevs, double *perViewErrors, - int flags ) +void CV_CameraCalibrationTest_CPP::calibrate(Size imageSize, + const std::vector >& _imagePoints, + const std::vector >& _objectPoints, + int iFixedPoint, Mat& _distCoeffs, Mat& _cameraMatrix, std::vector& translationVectors, + std::vector& rotationMatrices, std::vector& newObjPoints, + std::vector& stdDevs, std::vector& perViewErrors, + int flags ) { + int pointCount = (int)_imagePoints[0].size(); + size_t i, imageCount = _imagePoints.size(); vector > objectPoints( imageCount ); vector > imagePoints( imageCount ); - Size imageSize = _imageSize; Mat cameraMatrix, distCoeffs(1,4,CV_64F,Scalar::all(0)); vector rvecs, tvecs; Mat newObjMat; @@ -859,26 +767,14 @@ void CV_CameraCalibrationTest_CPP::calibrate(int imageCount, int* pointCounts, Mat stdDevsMatObj; Mat perViewErrorsMat; - CvPoint3D64f* op = _objectPoints; - CvPoint2D64f* ip = _imagePoints; - vector >::iterator objectPointsIt = objectPoints.begin(); - vector >::iterator imagePointsIt = imagePoints.begin(); - for( int i = 0; i < imageCount; ++objectPointsIt, ++imagePointsIt, i++ ) + for( i = 0; i < imageCount; i++ ) { - int num = pointCounts[i]; - objectPointsIt->resize( num ); - imagePointsIt->resize( num ); - vector::iterator oIt = objectPointsIt->begin(); - vector::iterator iIt = imagePointsIt->begin(); - for( int j = 0; j < num; ++oIt, ++iIt, j++, op++, ip++) - { - oIt->x = (float)op->x, oIt->y = (float)op->y, oIt->z = (float)op->z; - iIt->x = (float)ip->x, iIt->y = (float)ip->y; - } + Mat(_imagePoints[i]).convertTo(imagePoints[i], CV_32F); + Mat(_objectPoints[i]).convertTo(objectPoints[i], CV_32F); } - for( int i = CV_CALIB_NINTRINSIC + imageCount*6; i < CV_CALIB_NINTRINSIC + imageCount*6 - + pointCounts[0]*3; i++) + size_t nstddev0 = CV_CALIB_NINTRINSIC + imageCount*6, nstddev1 = nstddev0 + _imagePoints[0].size()*3; + for( i = nstddev0; i < nstddev1; i++ ) { stdDevs[i] = 0.0; } @@ -898,60 +794,38 @@ void CV_CameraCalibrationTest_CPP::calibrate(int imageCount, int* pointCounts, perViewErrorsMat, flags ); - bool releaseObject = iFixedPoint > 0 && iFixedPoint < pointCounts[0] - 1; + bool releaseObject = iFixedPoint > 0 && iFixedPoint < pointCount - 1; if( releaseObject ) { - newObjMat.convertTo( newObjMat, CV_64F ); - assert( newObjMat.total() * newObjMat.channels() == static_cast(3*pointCounts[0]) ); - memcpy( newObjPoints, newObjMat.ptr(), 3*pointCounts[0]*sizeof(double) ); + newObjMat.convertTo( newObjPoints, CV_64F ); } - assert( stdDevsMatInt.type() == CV_64F ); - assert( stdDevsMatInt.total() == static_cast(CV_CALIB_NINTRINSIC) ); - memcpy( stdDevs, stdDevsMatInt.ptr(), CV_CALIB_NINTRINSIC*sizeof(double) ); + Mat stdDevMats[] = {stdDevsMatInt, stdDevsMatExt, stdDevsMatObj}, stdDevsMat; + vconcat(stdDevMats, releaseObject ? 3 : 2, stdDevsMat); + stdDevsMat.convertTo(stdDevs, CV_64F); - assert( stdDevsMatExt.type() == CV_64F ); - assert( stdDevsMatExt.total() == static_cast(6*imageCount) ); - memcpy( stdDevs + CV_CALIB_NINTRINSIC, stdDevsMatExt.ptr(), 6*imageCount*sizeof(double) ); + perViewErrorsMat.convertTo(perViewErrors, CV_64F); + cameraMatrix.convertTo(_cameraMatrix, CV_64F); + distCoeffs.convertTo(_distCoeffs, CV_64F); - if( releaseObject ) + for( i = 0; i < imageCount; i++ ) { - assert( stdDevsMatObj.type() == CV_64F ); - assert( stdDevsMatObj.total() == static_cast(3*pointCounts[0]) ); - memcpy( stdDevs + CV_CALIB_NINTRINSIC + 6*imageCount, stdDevsMatObj.ptr(), - 3*pointCounts[0]*sizeof(double) ); - } - - assert( perViewErrorsMat.type() == CV_64F); - assert( perViewErrorsMat.total() == static_cast(imageCount) ); - memcpy( perViewErrors, perViewErrorsMat.ptr(), imageCount*sizeof(double) ); - - assert( cameraMatrix.type() == CV_64FC1 ); - memcpy( _cameraMatrix, cameraMatrix.ptr(), 9*sizeof(double) ); - - assert( cameraMatrix.type() == CV_64FC1 ); - memcpy( _distortionCoeffs, distCoeffs.ptr(), 4*sizeof(double) ); - - vector::iterator rvecsIt = rvecs.begin(); - vector::iterator tvecsIt = tvecs.begin(); - double *rm = rotationMatrices, - *tm = translationVectors; - assert( rvecsIt->type() == CV_64FC1 ); - assert( tvecsIt->type() == CV_64FC1 ); - for( int i = 0; i < imageCount; ++rvecsIt, ++tvecsIt, i++, rm+=9, tm+=3 ) - { - Mat r9( 3, 3, CV_64FC1 ); - cvtest::Rodrigues( *rvecsIt, r9 ); - memcpy( rm, r9.ptr(), 9*sizeof(double) ); - memcpy( tm, tvecsIt->ptr(), 3*sizeof(double) ); + Mat r9; + cvtest::Rodrigues( rvecs[i], r9 ); + cv::transpose(r9, r9); + r9.convertTo(rotationMatrices[i], CV_64F); + tvecs[i].convertTo(translationVectors[i], CV_64F); } } -void CV_CameraCalibrationTest_CPP::project( int pointCount, CvPoint3D64f* _objectPoints, - double* rotationMatrix, double* translationVector, - double* _cameraMatrix, double* distortion, CvPoint2D64f* _imagePoints ) + +void CV_CameraCalibrationTest_CPP::project( const std::vector& objectPoints, + const RotMat& rotationMatrix, const Vec3d& translationVector, + const Mat& cameraMatrix, const Mat& distortion, + std::vector& imagePoints ) { - Mat objectPoints( pointCount, 3, CV_64FC1, _objectPoints ); + projectPoints(objectPoints, rotationMatrix, translationVector, cameraMatrix, distortion, imagePoints ); + /*Mat objectPoints( pointCount, 3, CV_64FC1, _objectPoints ); Mat rmat( 3, 3, CV_64FC1, rotationMatrix ), rvec( 1, 3, CV_64FC1 ), tvec( 1, 3, CV_64FC1, translationVector ); @@ -967,7 +841,7 @@ void CV_CameraCalibrationTest_CPP::project( int pointCount, CvPoint3D64f* _objec for( int i = 0; it != imagePoints.end(); ++it, i++ ) { _imagePoints[i] = cvPoint2D64f( it->x, it->y ); - } + }*/ } @@ -1075,32 +949,6 @@ _exit_: ts->set_failed_test_info( code ); } -//----------------------------------------- CV_CalibrationMatrixValuesTest_C -------------------------------- - -class CV_CalibrationMatrixValuesTest_C : public CV_CalibrationMatrixValuesTest -{ -public: - CV_CalibrationMatrixValuesTest_C(){} -protected: - virtual void calibMatrixValues( const Mat& cameraMatrix, Size imageSize, - double apertureWidth, double apertureHeight, double& fovx, double& fovy, double& focalLength, - Point2d& principalPoint, double& aspectRatio ); -}; - -void CV_CalibrationMatrixValuesTest_C::calibMatrixValues( const Mat& _cameraMatrix, Size imageSize, - double apertureWidth, double apertureHeight, - double& fovx, double& fovy, double& focalLength, - Point2d& principalPoint, double& aspectRatio ) -{ - CvMat cameraMatrix = cvMat(_cameraMatrix); - CvPoint2D64f pp = {0, 0}; - cvCalibrationMatrixValues( &cameraMatrix, cvSize(imageSize), apertureWidth, apertureHeight, - &fovx, &fovy, &focalLength, &pp, &aspectRatio ); - principalPoint.x = pp.x; - principalPoint.y = pp.y; -} - - //----------------------------------------- CV_CalibrationMatrixValuesTest_CPP -------------------------------- class CV_CalibrationMatrixValuesTest_CPP : public CV_CalibrationMatrixValuesTest @@ -1360,43 +1208,6 @@ _exit_: ts->set_failed_test_info( code ); } -//----------------------------------------- CV_ProjectPointsTest_C -------------------------------- -class CV_ProjectPointsTest_C : public CV_ProjectPointsTest -{ -public: - CV_ProjectPointsTest_C() {} -protected: - virtual void project( const Mat& objectPoints, - const Mat& rvec, const Mat& tvec, - const Mat& cameraMatrix, - const Mat& distCoeffs, - vector& imagePoints, - Mat& dpdrot, Mat& dpdt, Mat& dpdf, - Mat& dpdc, Mat& dpddist, - double aspectRatio=0 ); -}; - -void CV_ProjectPointsTest_C::project( const Mat& opoints, const Mat& rvec, const Mat& tvec, - const Mat& cameraMatrix, const Mat& distCoeffs, vector& ipoints, - Mat& dpdrot, Mat& dpdt, Mat& dpdf, Mat& dpdc, Mat& dpddist, double aspectRatio) -{ - int npoints = opoints.cols*opoints.rows*opoints.channels()/3; - ipoints.resize(npoints); - dpdrot.create(npoints*2, 3, CV_64F); - dpdt.create(npoints*2, 3, CV_64F); - dpdf.create(npoints*2, 2, CV_64F); - dpdc.create(npoints*2, 2, CV_64F); - dpddist.create(npoints*2, distCoeffs.rows + distCoeffs.cols - 1, CV_64F); - Mat imagePoints(ipoints); - CvMat _objectPoints = cvMat(opoints), _imagePoints = cvMat(imagePoints); - CvMat _rvec = cvMat(rvec), _tvec = cvMat(tvec), _cameraMatrix = cvMat(cameraMatrix), _distCoeffs = cvMat(distCoeffs); - CvMat _dpdrot = cvMat(dpdrot), _dpdt = cvMat(dpdt), _dpdf = cvMat(dpdf), _dpdc = cvMat(dpdc), _dpddist = cvMat(dpddist); - - cvProjectPoints2( &_objectPoints, &_rvec, &_tvec, &_cameraMatrix, &_distCoeffs, - &_imagePoints, &_dpdrot, &_dpdt, &_dpdf, &_dpdc, &_dpddist, aspectRatio ); -} - - //----------------------------------------- CV_ProjectPointsTest_CPP -------------------------------- class CV_ProjectPointsTest_CPP : public CV_ProjectPointsTest { @@ -1938,139 +1749,6 @@ void CV_StereoCalibrationTest::run( int ) } } -//-------------------------------- CV_StereoCalibrationTest_C ------------------------------ - -class CV_StereoCalibrationTest_C : public CV_StereoCalibrationTest -{ -public: - CV_StereoCalibrationTest_C() {} -protected: - virtual double calibrateStereoCamera( const vector >& objectPoints, - const vector >& imagePoints1, - const vector >& imagePoints2, - Mat& cameraMatrix1, Mat& distCoeffs1, - Mat& cameraMatrix2, Mat& distCoeffs2, - Size imageSize, Mat& R, Mat& T, - Mat& E, Mat& F, TermCriteria criteria, int flags ); - virtual void rectify( const Mat& cameraMatrix1, const Mat& distCoeffs1, - const Mat& cameraMatrix2, const Mat& distCoeffs2, - Size imageSize, const Mat& R, const Mat& T, - Mat& R1, Mat& R2, Mat& P1, Mat& P2, Mat& Q, - double alpha, Size newImageSize, - Rect* validPixROI1, Rect* validPixROI2, int flags ); - virtual bool rectifyUncalibrated( const Mat& points1, - const Mat& points2, const Mat& F, Size imgSize, - Mat& H1, Mat& H2, double threshold=5 ); - virtual void triangulate( const Mat& P1, const Mat& P2, - const Mat &points1, const Mat &points2, - Mat &points4D ); - virtual void correct( const Mat& F, - const Mat &points1, const Mat &points2, - Mat &newPoints1, Mat &newPoints2 ); -}; - -double CV_StereoCalibrationTest_C::calibrateStereoCamera( const vector >& objectPoints, - const vector >& imagePoints1, - const vector >& imagePoints2, - Mat& cameraMatrix1, Mat& distCoeffs1, - Mat& cameraMatrix2, Mat& distCoeffs2, - Size imageSize, Mat& R, Mat& T, - Mat& E, Mat& F, TermCriteria criteria, int flags ) -{ - cameraMatrix1.create( 3, 3, CV_64F ); - cameraMatrix2.create( 3, 3, CV_64F); - distCoeffs1.create( 1, 5, CV_64F); - distCoeffs2.create( 1, 5, CV_64F); - R.create(3, 3, CV_64F); - T.create(3, 1, CV_64F); - E.create(3, 3, CV_64F); - F.create(3, 3, CV_64F); - - int nimages = (int)objectPoints.size(), total = 0; - for( int i = 0; i < nimages; i++ ) - { - total += (int)objectPoints[i].size(); - } - - Mat npoints( 1, nimages, CV_32S ), - objPt( 1, total, traits::Type::value ), - imgPt( 1, total, traits::Type::value ), - imgPt2( 1, total, traits::Type::value ); - - Point2f* imgPtData2 = imgPt2.ptr(); - Point3f* objPtData = objPt.ptr(); - Point2f* imgPtData = imgPt.ptr(); - for( int i = 0, ni = 0, j = 0; i < nimages; i++, j += ni ) - { - ni = (int)objectPoints[i].size(); - npoints.ptr()[i] = ni; - std::copy(objectPoints[i].begin(), objectPoints[i].end(), objPtData + j); - std::copy(imagePoints1[i].begin(), imagePoints1[i].end(), imgPtData + j); - std::copy(imagePoints2[i].begin(), imagePoints2[i].end(), imgPtData2 + j); - } - CvMat _objPt = cvMat(objPt), _imgPt = cvMat(imgPt), _imgPt2 = cvMat(imgPt2), _npoints = cvMat(npoints); - CvMat _cameraMatrix1 = cvMat(cameraMatrix1), _distCoeffs1 = cvMat(distCoeffs1); - CvMat _cameraMatrix2 = cvMat(cameraMatrix2), _distCoeffs2 = cvMat(distCoeffs2); - CvMat matR = cvMat(R), matT = cvMat(T), matE = cvMat(E), matF = cvMat(F); - - return cvStereoCalibrate(&_objPt, &_imgPt, &_imgPt2, &_npoints, &_cameraMatrix1, - &_distCoeffs1, &_cameraMatrix2, &_distCoeffs2, cvSize(imageSize), - &matR, &matT, &matE, &matF, flags, cvTermCriteria(criteria)); -} - -void CV_StereoCalibrationTest_C::rectify( const Mat& cameraMatrix1, const Mat& distCoeffs1, - const Mat& cameraMatrix2, const Mat& distCoeffs2, - Size imageSize, const Mat& R, const Mat& T, - Mat& R1, Mat& R2, Mat& P1, Mat& P2, Mat& Q, - double alpha, Size newImageSize, - Rect* validPixROI1, Rect* validPixROI2, int flags ) -{ - int rtype = CV_64F; - R1.create(3, 3, rtype); - R2.create(3, 3, rtype); - P1.create(3, 4, rtype); - P2.create(3, 4, rtype); - Q.create(4, 4, rtype); - CvMat _cameraMatrix1 = cvMat(cameraMatrix1), _distCoeffs1 = cvMat(distCoeffs1); - CvMat _cameraMatrix2 = cvMat(cameraMatrix2), _distCoeffs2 = cvMat(distCoeffs2); - CvMat matR = cvMat(R), matT = cvMat(T), _R1 = cvMat(R1), _R2 = cvMat(R2), _P1 = cvMat(P1), _P2 = cvMat(P2), matQ = cvMat(Q); - cvStereoRectify( &_cameraMatrix1, &_cameraMatrix2, &_distCoeffs1, &_distCoeffs2, - cvSize(imageSize), &matR, &matT, &_R1, &_R2, &_P1, &_P2, &matQ, flags, - alpha, cvSize(newImageSize), (CvRect*)validPixROI1, (CvRect*)validPixROI2); -} - -bool CV_StereoCalibrationTest_C::rectifyUncalibrated( const Mat& points1, - const Mat& points2, const Mat& F, Size imgSize, Mat& H1, Mat& H2, double threshold ) -{ - H1.create(3, 3, CV_64F); - H2.create(3, 3, CV_64F); - CvMat _pt1 = cvMat(points1), _pt2 = cvMat(points2), matF, *pF=0, _H1 = cvMat(H1), _H2 = cvMat(H2); - if( F.size() == Size(3, 3) ) - pF = &(matF = cvMat(F)); - return cvStereoRectifyUncalibrated(&_pt1, &_pt2, pF, cvSize(imgSize), &_H1, &_H2, threshold) > 0; -} - -void CV_StereoCalibrationTest_C::triangulate( const Mat& P1, const Mat& P2, - const Mat &points1, const Mat &points2, - Mat &points4D ) -{ - CvMat _P1 = cvMat(P1), _P2 = cvMat(P2), _points1 = cvMat(points1), _points2 = cvMat(points2); - points4D.create(4, points1.cols, points1.type()); - CvMat _points4D = cvMat(points4D); - cvTriangulatePoints(&_P1, &_P2, &_points1, &_points2, &_points4D); -} - -void CV_StereoCalibrationTest_C::correct( const Mat& F, - const Mat &points1, const Mat &points2, - Mat &newPoints1, Mat &newPoints2 ) -{ - CvMat _F = cvMat(F), _points1 = cvMat(points1), _points2 = cvMat(points2); - newPoints1.create(1, points1.cols, points1.type()); - newPoints2.create(1, points2.cols, points2.type()); - CvMat _newPoints1 = cvMat(newPoints1), _newPoints2 = cvMat(newPoints2); - cvCorrectMatches(&_F, &_points1, &_points2, &_newPoints1, &_newPoints2); -} - //-------------------------------- CV_StereoCalibrationTest_CPP ------------------------------ class CV_StereoCalibrationTest_CPP : public CV_StereoCalibrationTest @@ -2148,13 +1826,9 @@ void CV_StereoCalibrationTest_CPP::correct( const Mat& F, /////////////////////////////////////////////////////////////////////////////////////////////////// -TEST(Calib3d_CalibrateCamera_C, regression) { CV_CameraCalibrationTest_C test; test.safe_run(); } TEST(Calib3d_CalibrateCamera_CPP, regression) { CV_CameraCalibrationTest_CPP test; test.safe_run(); } -TEST(Calib3d_CalibrationMatrixValues_C, accuracy) { CV_CalibrationMatrixValuesTest_C test; test.safe_run(); } TEST(Calib3d_CalibrationMatrixValues_CPP, accuracy) { CV_CalibrationMatrixValuesTest_CPP test; test.safe_run(); } -TEST(Calib3d_ProjectPoints_C, accuracy) { CV_ProjectPointsTest_C test; test.safe_run(); } TEST(Calib3d_ProjectPoints_CPP, regression) { CV_ProjectPointsTest_CPP test; test.safe_run(); } -TEST(Calib3d_StereoCalibrate_C, regression) { CV_StereoCalibrationTest_C test; test.safe_run(); } TEST(Calib3d_StereoCalibrate_CPP, regression) { CV_StereoCalibrationTest_CPP test; test.safe_run(); } TEST(Calib3d_StereoCalibrateCorner, regression) { CV_StereoCalibrationCornerTest test; test.safe_run(); } diff --git a/modules/calib3d/test/test_cameracalibration_badarg.cpp b/modules/calib3d/test/test_cameracalibration_badarg.cpp index cc1874dd35..27ced856a9 100644 --- a/modules/calib3d/test/test_cameracalibration_badarg.cpp +++ b/modules/calib3d/test/test_cameracalibration_badarg.cpp @@ -41,44 +41,60 @@ #include "test_precomp.hpp" #include "test_chessboardgenerator.hpp" -#include "opencv2/calib3d/calib3d_c.h" +#include "opencv2/core/types_c.h" namespace opencv_test { namespace { class CV_CameraCalibrationBadArgTest : public cvtest::BadArgTest { public: - CV_CameraCalibrationBadArgTest() : imgSize(800, 600) {} + CV_CameraCalibrationBadArgTest() {} ~CV_CameraCalibrationBadArgTest() {} protected: void run(int); void run_func(void) {} - const static int M = 2; - - Size imgSize; - Size corSize; - Mat chessBoard; - Mat corners; - struct C_Caller { - CvMat* objPts; - CvMat* imgPts; - CvMat* npoints; - Size imageSize; - int iFixedPoint; - CvMat *cameraMatrix; - CvMat *distCoeffs; - CvMat *rvecs; - CvMat *tvecs; - CvMat *newObjPts; - int flags; + _InputArray imgPts_arg; + _InputArray objPts_arg; + _OutputArray rvecs_arg; + _OutputArray tvecs_arg; + _OutputArray newObjPts_arg; + _InputOutputArray cameraMatrix_arg; + _InputOutputArray distCoeffs_arg; + + std::vector > imgPts; + std::vector > objPts; + + Size imageSize0, imageSize; + int iFixedPoint0, iFixedPoint; + Mat cameraMatrix; + Mat distCoeffs; + std::vector rvecs; + std::vector tvecs; + std::vector newObjPts; + int flags0, flags; + + void initArgs() + { + imgPts_arg = imgPts; + objPts_arg = objPts; + rvecs_arg = rvecs; + tvecs_arg = tvecs; + newObjPts_arg = newObjPts; + cameraMatrix_arg = cameraMatrix; + distCoeffs_arg = distCoeffs; + imageSize = imageSize0; + flags = flags0; + iFixedPoint = iFixedPoint0; + } void operator()() const { - cvCalibrateCamera4(objPts, imgPts, npoints, cvSize(imageSize), iFixedPoint, - cameraMatrix, distCoeffs, rvecs, tvecs, newObjPts, flags ); + calibrateCameraRO(objPts_arg, imgPts_arg, imageSize, iFixedPoint, + cameraMatrix_arg, distCoeffs_arg, rvecs_arg, tvecs_arg, + newObjPts_arg, flags); } }; }; @@ -86,6 +102,8 @@ protected: void CV_CameraCalibrationBadArgTest::run( int /* start_from */ ) { + const int M = 2; + Size imgSize(800, 600); Mat_ camMat(3, 3); Mat_ distCoeffs0(1, 5); @@ -93,283 +111,92 @@ void CV_CameraCalibrationBadArgTest::run( int /* start_from */ ) distCoeffs0 << 1.2f, 0.2f, 0.f, 0.f, 0.f; ChessBoardGenerator cbg(Size(8,6)); - corSize = cbg.cornersSize(); - vector exp_corn; - chessBoard = cbg(Mat(imgSize, CV_8U, Scalar(0)), camMat, distCoeffs0, exp_corn); - Mat_(corSize.height, corSize.width, (Point2f*)&exp_corn[0]).copyTo(corners); + Size corSize = cbg.cornersSize(); + vector corners; + cbg(Mat(imgSize, CV_8U, Scalar(0)), camMat, distCoeffs0, corners); - CvMat objPts, imgPts, npoints, cameraMatrix, distCoeffs, rvecs, tvecs; - CvMat newObjPts; - Mat zeros(1, sizeof(CvMat), CV_8U, Scalar(0)); - - C_Caller caller, bad_caller; - caller.imageSize = imgSize; - caller.objPts = &objPts; - caller.imgPts = &imgPts; - caller.npoints = &npoints; - caller.cameraMatrix = &cameraMatrix; - caller.distCoeffs = &distCoeffs; - caller.rvecs = &rvecs; - caller.tvecs = &tvecs; - caller.newObjPts = &newObjPts; + C_Caller caller; + caller.imageSize0 = imgSize; + caller.iFixedPoint0 = -1; + caller.flags0 = 0; ///////////////////////////// - Mat objPts_cpp; - Mat imgPts_cpp; - Mat npoints_cpp; Mat cameraMatrix_cpp; Mat distCoeffs_cpp; Mat rvecs_cpp; Mat tvecs_cpp; Mat newObjPts_cpp; - objPts_cpp.create(corSize, CV_32FC3); - for(int j = 0; j < corSize.height; ++j) - for(int i = 0; i < corSize.width; ++i) - objPts_cpp.at(j, i) = Point3i(i, j, 0); - objPts_cpp = objPts_cpp.reshape(3, 1); - Mat objPts_cpp_all(1, objPts_cpp.cols * M, CV_32FC3); + std::vector objPts_cpp; + for(int y = 0; y < corSize.height; ++y) + for(int x = 0; x < corSize.width; ++x) + objPts_cpp.push_back(Point3f((float)x, (float)y, 0.f)); + caller.objPts.resize(M); + caller.imgPts.resize(M); for(int i = 0; i < M; i++) - objPts_cpp.copyTo(objPts_cpp_all.colRange(objPts_cpp.cols * i, objPts_cpp.cols * (i + 1))); - objPts_cpp = objPts_cpp_all; - - caller.iFixedPoint = -1; - - imgPts_cpp = corners.clone().reshape(2, 1); - Mat imgPts_cpp_all(1, imgPts_cpp.cols * M, CV_32FC2); - for(int i = 0; i < M; i++) - imgPts_cpp.copyTo(imgPts_cpp_all.colRange(imgPts_cpp.cols * i, imgPts_cpp.cols * (i + 1))); - imgPts_cpp = imgPts_cpp_all; - npoints_cpp = Mat_(M, 1, corSize.width * corSize.height); - cameraMatrix_cpp.create(3, 3, CV_32F); - distCoeffs_cpp.create(5, 1, CV_32F); - rvecs_cpp.create(M, 1, CV_32FC3); - tvecs_cpp.create(M, 1, CV_32FC3); - newObjPts_cpp.create(corSize.width * corSize.height, 1, CV_32FC3); - - caller.flags = 0; - //CV_CALIB_USE_INTRINSIC_GUESS; //CV_CALIB_FIX_ASPECT_RATIO - //CV_CALIB_USE_INTRINSIC_GUESS //CV_CALIB_FIX_ASPECT_RATIO - //CV_CALIB_FIX_PRINCIPAL_POINT //CV_CALIB_ZERO_TANGENT_DIST - //CV_CALIB_FIX_FOCAL_LENGTH //CV_CALIB_FIX_K1 //CV_CALIB_FIX_K2 //CV_CALIB_FIX_K3 - - objPts = cvMat(objPts_cpp); - imgPts = cvMat(imgPts_cpp); - npoints = cvMat(npoints_cpp); - cameraMatrix = cvMat(cameraMatrix_cpp); - distCoeffs = cvMat(distCoeffs_cpp); - rvecs = cvMat(rvecs_cpp); - tvecs = cvMat(tvecs_cpp); - newObjPts = cvMat(newObjPts_cpp); + { + caller.objPts[i] = objPts_cpp; + caller.imgPts[i] = corners; + } + caller.cameraMatrix.create(3, 3, CV_32F); + caller.distCoeffs.create(5, 1, CV_32F); + caller.rvecs.clear(); + caller.tvecs.clear(); + caller.newObjPts.clear(); /* /*//*/ */ int errors = 0; - bad_caller = caller; - bad_caller.objPts = 0; - errors += run_test_case( CV_StsBadArg, "Zero passed in objPts", bad_caller); + caller.initArgs(); + caller.objPts_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "None passed in objPts", caller); - bad_caller = caller; - bad_caller.imgPts = 0; - errors += run_test_case( CV_StsBadArg, "Zero passed in imgPts", bad_caller ); + caller.initArgs(); + caller.imgPts_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "None passed in imgPts", caller ); - bad_caller = caller; - bad_caller.npoints = 0; - errors += run_test_case( CV_StsBadArg, "Zero passed in npoints", bad_caller ); + caller.initArgs(); + caller.cameraMatrix_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Zero passed in cameraMatrix", caller ); - bad_caller = caller; - bad_caller.cameraMatrix = 0; - errors += run_test_case( CV_StsBadArg, "Zero passed in cameraMatrix", bad_caller ); + caller.initArgs(); + caller.distCoeffs_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Zero passed in distCoeffs", caller ); - bad_caller = caller; - bad_caller.distCoeffs = 0; - errors += run_test_case( CV_StsBadArg, "Zero passed in distCoeffs", bad_caller ); + caller.initArgs(); + caller.imageSize.width = -1; + errors += run_test_case( CV_StsOutOfRange, "Bad image width", caller ); - bad_caller = caller; - bad_caller.imageSize.width = -1; - errors += run_test_case( CV_StsOutOfRange, "Bad image width", bad_caller ); + caller.initArgs(); + caller.imageSize.height = -1; + errors += run_test_case( CV_StsOutOfRange, "Bad image height", caller ); - bad_caller = caller; - bad_caller.imageSize.height = -1; - errors += run_test_case( CV_StsOutOfRange, "Bad image height", bad_caller ); + caller.initArgs(); + caller.imgPts[0].clear(); + errors += run_test_case( CV_StsUnsupportedFormat, "Bad imgpts[0]", caller ); + caller.imgPts[0] = caller.imgPts[1]; - Mat bad_nts_cpp1 = Mat_(M, 1, 1.f); - Mat bad_nts_cpp2 = Mat_(3, 3, corSize.width * corSize.height); - CvMat bad_npts_c1 = cvMat(bad_nts_cpp1); - CvMat bad_npts_c2 = cvMat(bad_nts_cpp2); + caller.initArgs(); + caller.objPts[1].clear(); + errors += run_test_case( CV_StsUnsupportedFormat, "Bad objpts[1]", caller ); + caller.objPts[1] = caller.objPts[0]; - bad_caller = caller; - bad_caller.npoints = &bad_npts_c1; - errors += run_test_case( CV_StsUnsupportedFormat, "Bad npoints format", bad_caller ); + caller.initArgs(); + Mat badCM = Mat::zeros(4, 4, CV_64F); + caller.cameraMatrix_arg = badCM; + caller.flags = CALIB_USE_INTRINSIC_GUESS; + errors += run_test_case( CV_StsBadArg, "Bad camearaMatrix header", caller ); - bad_caller = caller; - bad_caller.npoints = &bad_npts_c2; - errors += run_test_case( CV_StsUnsupportedFormat, "Bad npoints size", bad_caller ); - - bad_caller = caller; - bad_caller.rvecs = (CvMat*)zeros.ptr(); - errors += run_test_case( CV_StsBadArg, "Bad rvecs header", bad_caller ); - - bad_caller = caller; - bad_caller.tvecs = (CvMat*)zeros.ptr(); - errors += run_test_case( CV_StsBadArg, "Bad tvecs header", bad_caller ); - - bad_caller = caller; - bad_caller.newObjPts = (CvMat*)zeros.ptr(); - errors += run_test_case( CV_StsBadArg, "Bad newObjPts header", bad_caller ); - - Mat bad_rvecs_cpp1(M+1, 1, CV_32FC3); CvMat bad_rvecs_c1 = cvMat(bad_rvecs_cpp1); - Mat bad_tvecs_cpp1(M+1, 1, CV_32FC3); CvMat bad_tvecs_c1 = cvMat(bad_tvecs_cpp1); - - - - Mat bad_rvecs_cpp2(M, 2, CV_32FC3); CvMat bad_rvecs_c2 = cvMat(bad_rvecs_cpp2); - Mat bad_tvecs_cpp2(M, 2, CV_32FC3); CvMat bad_tvecs_c2 = cvMat(bad_tvecs_cpp2); - - bad_caller = caller; - bad_caller.rvecs = &bad_rvecs_c1; - errors += run_test_case( CV_StsBadArg, "Bad rvecs header", bad_caller ); - - bad_caller = caller; - bad_caller.rvecs = &bad_rvecs_c2; - errors += run_test_case( CV_StsBadArg, "Bad rvecs header", bad_caller ); - - bad_caller = caller; - bad_caller.tvecs = &bad_tvecs_c1; - errors += run_test_case( CV_StsBadArg, "Bad tvecs header", bad_caller ); - - bad_caller = caller; - bad_caller.tvecs = &bad_tvecs_c2; - errors += run_test_case( CV_StsBadArg, "Bad tvecs header", bad_caller ); - - bad_caller = caller; - bad_caller.newObjPts = &bad_tvecs_c1; - errors += run_test_case( CV_StsBadArg, "Bad newObjPts header", bad_caller ); - - bad_caller = caller; - bad_caller.newObjPts = &bad_tvecs_c2; - errors += run_test_case( CV_StsBadArg, "Bad newObjPts header", bad_caller ); - - Mat bad_cameraMatrix_cpp1(3, 3, CV_32S); CvMat bad_cameraMatrix_c1 = cvMat(bad_cameraMatrix_cpp1); - Mat bad_cameraMatrix_cpp2(2, 3, CV_32F); CvMat bad_cameraMatrix_c2 = cvMat(bad_cameraMatrix_cpp2); - Mat bad_cameraMatrix_cpp3(3, 2, CV_64F); CvMat bad_cameraMatrix_c3 = cvMat(bad_cameraMatrix_cpp3); - - - - bad_caller = caller; - bad_caller.cameraMatrix = &bad_cameraMatrix_c1; - errors += run_test_case( CV_StsBadArg, "Bad camearaMatrix header", bad_caller ); - - bad_caller = caller; - bad_caller.cameraMatrix = &bad_cameraMatrix_c2; - errors += run_test_case( CV_StsBadArg, "Bad camearaMatrix header", bad_caller ); - - bad_caller = caller; - bad_caller.cameraMatrix = &bad_cameraMatrix_c3; - errors += run_test_case( CV_StsBadArg, "Bad camearaMatrix header", bad_caller ); - - Mat bad_distCoeffs_cpp1(1, 5, CV_32S); CvMat bad_distCoeffs_c1 = cvMat(bad_distCoeffs_cpp1); - Mat bad_distCoeffs_cpp2(2, 2, CV_64F); CvMat bad_distCoeffs_c2 = cvMat(bad_distCoeffs_cpp2); - Mat bad_distCoeffs_cpp3(1, 6, CV_64F); CvMat bad_distCoeffs_c3 = cvMat(bad_distCoeffs_cpp3); - - - - bad_caller = caller; - bad_caller.distCoeffs = &bad_distCoeffs_c1; - errors += run_test_case( CV_StsBadArg, "Bad distCoeffs header", bad_caller ); - - bad_caller = caller; - bad_caller.distCoeffs = &bad_distCoeffs_c2; - errors += run_test_case( CV_StsBadArg, "Bad distCoeffs header", bad_caller ); - - - bad_caller = caller; - bad_caller.distCoeffs = &bad_distCoeffs_c3; - errors += run_test_case( CV_StsBadArg, "Bad distCoeffs header", bad_caller ); - - double CM[] = {0, 0, 0, /**/0, 0, 0, /**/0, 0, 0}; - Mat bad_cameraMatrix_cpp4(3, 3, CV_64F, CM); CvMat bad_cameraMatrix_c4 = cvMat(bad_cameraMatrix_cpp4); - - bad_caller = caller; - bad_caller.flags |= CV_CALIB_USE_INTRINSIC_GUESS; - bad_caller.cameraMatrix = &bad_cameraMatrix_c4; - CM[0] = 0; //bad fx - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[0] = 500; CM[4] = 0; //bad fy - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[0] = 500; CM[4] = 500; CM[2] = -1; //bad cx - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[0] = 500; CM[4] = 500; CM[2] = imgSize.width*2; //bad cx - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[0] = 500; CM[4] = 500; CM[2] = imgSize.width/2; CM[5] = -1; //bad cy - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[0] = 500; CM[4] = 500; CM[2] = imgSize.width/2; CM[5] = imgSize.height*2; //bad cy - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[0] = 500; CM[4] = 500; CM[2] = imgSize.width/2; CM[5] = imgSize.height/2; - CM[1] = 0.1; //Non-zero skew - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[1] = 0; - CM[3] = 0.1; /* mad matrix shape */ - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[3] = 0; CM[6] = 0.1; /* mad matrix shape */ - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[3] = 0; CM[6] = 0; CM[7] = 0.1; /* mad matrix shape */ - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - - CM[3] = 0; CM[6] = 0; CM[7] = 0; CM[8] = 1.1; /* mad matrix shape */ - errors += run_test_case( CV_StsOutOfRange, "Bad camearaMatrix data", bad_caller ); - CM[8] = 1.0; - - ///////////////////////////////////////////////////////////////////////////////////// - bad_caller = caller; - Mat bad_objPts_cpp5 = objPts_cpp.clone(); CvMat bad_objPts_c5 = cvMat(bad_objPts_cpp5); - bad_caller.objPts = &bad_objPts_c5; - - cv::RNG& rng = theRNG(); - for(int i = 0; i < bad_objPts_cpp5.cols; ++i) - bad_objPts_cpp5.at(0, i).z += ((float)rng - 0.5f); - - errors += run_test_case( CV_StsBadArg, "Bad objPts data", bad_caller ); - - bad_objPts_cpp5 = objPts_cpp.clone(); bad_objPts_c5 = cvMat(bad_objPts_cpp5); - bad_caller.objPts = &bad_objPts_c5; - bad_caller.iFixedPoint = corSize.width - 1; - for(int i = 0; i < bad_objPts_cpp5.cols; ++i) - { - bad_objPts_cpp5.at(0, i).x += (float)rng; - bad_objPts_cpp5.at(0, i).y += (float)rng; - } - errors += run_test_case( CV_StsBadArg, "Bad objPts data", bad_caller ); - - bad_caller = caller; - Mat bad_npts_cpp3 = npoints_cpp.clone(); - CvMat bad_npts_c3 = cvMat(bad_npts_cpp3); - bad_caller.npoints = &bad_npts_c3; - bad_caller.iFixedPoint = corSize.width - 1; - for(int i = 0; i < M; i++) - bad_npts_cpp3.at(i) += i; - errors += run_test_case( CV_StsBadArg, "Bad npoints data", bad_caller ); + caller.initArgs(); + Mat badDC = Mat::zeros(10, 10, CV_64F); + caller.distCoeffs_arg = badDC; + caller.flags = CALIB_USE_INTRINSIC_GUESS; + errors += run_test_case( CV_StsBadArg, "Bad camearaMatrix header", caller ); if (errors) ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); else ts->set_failed_test_info(cvtest::TS::OK); - - //try { caller(); } - //catch (...) - //{ - // ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); - // printf("+!"); - //} } @@ -383,108 +210,49 @@ protected: struct C_Caller { - CvMat* src; - CvMat* dst; - CvMat* jacobian; + _InputArray src_arg; + _OutputArray dst_arg, j_arg; - void operator()() { cvRodrigues2(src, dst, jacobian); } + Mat src; + Mat dst; + Mat jacobian; + + void initArgs() + { + src_arg = src; + dst_arg = dst; + j_arg = jacobian; + } + + void operator()() + { + cv::Rodrigues(src_arg, dst_arg, j_arg); + } }; void run(int /* start_from */ ) { Mat zeros(1, sizeof(CvMat), CV_8U, Scalar(0)); - CvMat src_c, dst_c, jacobian_c; - Mat src_cpp(3, 1, CV_32F); src_c = cvMat(src_cpp); - Mat dst_cpp(3, 3, CV_32F); dst_c = cvMat(dst_cpp); - Mat jacobian_cpp(3, 9, CV_32F); jacobian_c = cvMat(jacobian_cpp); + Mat src_cpp(3, 1, CV_32F); + Mat dst_cpp(3, 3, CV_32F); - C_Caller caller, bad_caller; - caller.src = &src_c; - caller.dst = &dst_c; - caller.jacobian = &jacobian_c; - - /* try { caller(); } - catch (...) - { - printf("badasfas"); - }*/ + C_Caller caller; /*/*//*/*/ int errors = 0; - bad_caller = caller; - bad_caller.src = 0; - errors += run_test_case( CV_StsNullPtr, "Src is zero pointer", bad_caller ); + caller.initArgs(); + caller.src_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Src is empty matrix", caller ); - bad_caller = caller; - bad_caller.dst = 0; - errors += run_test_case( CV_StsNullPtr, "Dst is zero pointer", bad_caller ); - - Mat bad_src_cpp1(3, 1, CV_8U); CvMat bad_src_c1 = cvMat(bad_src_cpp1); - Mat bad_dst_cpp1(3, 1, CV_8U); CvMat bad_dst_c1 = cvMat(bad_dst_cpp1); - Mat bad_jac_cpp1(3, 1, CV_8U); CvMat bad_jac_c1 = cvMat(bad_jac_cpp1); - Mat bad_jac_cpp2(3, 1, CV_32FC2); CvMat bad_jac_c2 = cvMat(bad_jac_cpp2); - Mat bad_jac_cpp3(3, 1, CV_32F); CvMat bad_jac_c3 = cvMat(bad_jac_cpp3); - - bad_caller = caller; - bad_caller.src = &bad_src_c1; - errors += run_test_case( CV_StsUnsupportedFormat, "Bad src formart", bad_caller ); - - bad_caller = caller; - bad_caller.dst = &bad_dst_c1; - errors += run_test_case( CV_StsUnmatchedFormats, "Bad dst formart", bad_caller ); - - bad_caller = caller; - bad_caller.jacobian = (CvMat*)zeros.ptr(); - errors += run_test_case( CV_StsBadArg, "Bad jacobian ", bad_caller ); - - bad_caller = caller; - bad_caller.jacobian = &bad_jac_c1; - errors += run_test_case( CV_StsUnmatchedFormats, "Bad jacobian format", bad_caller ); - - bad_caller = caller; - bad_caller.jacobian = &bad_jac_c2; - errors += run_test_case( CV_StsUnmatchedFormats, "Bad jacobian format", bad_caller ); - - bad_caller = caller; - bad_caller.jacobian = &bad_jac_c3; - errors += run_test_case( CV_StsBadSize, "Bad jacobian format", bad_caller ); - - Mat bad_src_cpp2(1, 1, CV_32F); CvMat bad_src_c2 = cvMat(bad_src_cpp2); - - bad_caller = caller; - bad_caller.src = &bad_src_c2; - errors += run_test_case( CV_StsBadSize, "Bad src format", bad_caller ); - - Mat bad_dst_cpp2(2, 1, CV_32F); CvMat bad_dst_c2 = cvMat(bad_dst_cpp2); - Mat bad_dst_cpp3(3, 2, CV_32F); CvMat bad_dst_c3 = cvMat(bad_dst_cpp3); - Mat bad_dst_cpp4(3, 3, CV_32FC2); CvMat bad_dst_c4 = cvMat(bad_dst_cpp4); - - bad_caller = caller; - bad_caller.dst = &bad_dst_c2; - errors += run_test_case( CV_StsBadSize, "Bad dst format", bad_caller ); - - bad_caller = caller; - bad_caller.dst = &bad_dst_c3; - errors += run_test_case( CV_StsBadSize, "Bad dst format", bad_caller ); - - bad_caller = caller; - bad_caller.dst = &bad_dst_c4; - errors += run_test_case( CV_StsBadSize, "Bad dst format", bad_caller ); - - - /********/ - src_cpp.create(3, 3, CV_32F); src_c = cvMat(src_cpp); - dst_cpp.create(3, 1, CV_32F); dst_c = cvMat(dst_cpp); - - - Mat bad_dst_cpp5(5, 5, CV_32F); CvMat bad_dst_c5 = cvMat(bad_dst_cpp5); - - bad_caller = caller; - bad_caller.dst = &bad_dst_c5; - errors += run_test_case( CV_StsBadSize, "Bad dst format", bad_caller ); + caller.initArgs(); + caller.src = Mat::zeros(3, 1, CV_8U); + errors += run_test_case( CV_StsUnsupportedFormat, "Bad src formart", caller ); + caller.initArgs(); + caller.src = Mat::zeros(1, 1, CV_32F); + errors += run_test_case( CV_StsBadSize, "Bad src size", caller ); if (errors) ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); @@ -514,259 +282,107 @@ protected: struct C_Caller { - CvMat* objectPoints; - CvMat* r_vec; - CvMat* t_vec; - CvMat* A; - CvMat* distCoeffs; - CvMat* imagePoints; - CvMat* dpdr; - CvMat* dpdt; - CvMat* dpdf; - CvMat* dpdc; - CvMat* dpdk; - double aspectRatio; + _InputArray objectPoints_arg, rvec_arg, tvec_arg, A_arg, DC_arg; + _OutputArray imagePoints_arg; + Mat objectPoints; + Mat r_vec; + Mat t_vec; + Mat A; + Mat distCoeffs; + Mat imagePoints; + Mat J; + double aspectRatio0, aspectRatio; + + void initArgs() + { + objectPoints_arg = objectPoints; + imagePoints_arg = imagePoints; + rvec_arg = r_vec; + tvec_arg = t_vec; + A_arg = A; + DC_arg = distCoeffs; + aspectRatio = aspectRatio0; + } void operator()() { - cvProjectPoints2( objectPoints, r_vec, t_vec, A, distCoeffs, imagePoints, - dpdr, dpdt, dpdf, dpdc, dpdk, aspectRatio ); + projectPoints(objectPoints_arg, rvec_arg, tvec_arg, A_arg, DC_arg, + imagePoints_arg, J, aspectRatio ); } }; void run(int /* start_from */ ) { - CvMat zeros = CvMat(); - - C_Caller caller, bad_caller; - CvMat objectPoints_c, r_vec_c, t_vec_c, A_c, distCoeffs_c, imagePoints_c, - dpdr_c, dpdt_c, dpdf_c, dpdc_c, dpdk_c; + C_Caller caller; const int n = 10; - Mat imagePoints_cpp(1, n, CV_32FC2); imagePoints_c = cvMat(imagePoints_cpp); - Mat objectPoints_cpp(1, n, CV_32FC3); randu(objectPoints_cpp, Scalar::all(1), Scalar::all(10)); - objectPoints_c = cvMat(objectPoints_cpp); - - Mat t_vec_cpp(Mat::zeros(1, 3, CV_32F)); t_vec_c = cvMat(t_vec_cpp); - Mat r_vec_cpp(3, 1, CV_32F); - cvtest::Rodrigues(Mat::eye(3, 3, CV_32F), r_vec_cpp); r_vec_c = cvMat(r_vec_cpp); - - Mat A_cpp = camMat.clone(); A_c = cvMat(A_cpp); - Mat distCoeffs_cpp = distCoeffs.clone(); distCoeffs_c = cvMat(distCoeffs_cpp); - - Mat dpdr_cpp(2*n, 3, CV_32F); dpdr_c = cvMat(dpdr_cpp); - Mat dpdt_cpp(2*n, 3, CV_32F); dpdt_c = cvMat(dpdt_cpp); - Mat dpdf_cpp(2*n, 2, CV_32F); dpdf_c = cvMat(dpdf_cpp); - Mat dpdc_cpp(2*n, 2, CV_32F); dpdc_c = cvMat(dpdc_cpp); - Mat dpdk_cpp(2*n, 4, CV_32F); dpdk_c = cvMat(dpdk_cpp); - - caller.aspectRatio = 1.0; - caller.objectPoints = &objectPoints_c; - caller.r_vec = &r_vec_c; - caller.t_vec = &t_vec_c; - caller.A = &A_c; - caller.distCoeffs = &distCoeffs_c; - caller.imagePoints = &imagePoints_c; - caller.dpdr = &dpdr_c; - caller.dpdt = &dpdt_c; - caller.dpdf = &dpdf_c; - caller.dpdc = &dpdc_c; - caller.dpdk = &dpdk_c; + caller.objectPoints = objectPoints_cpp; + caller.t_vec = Mat::zeros(1, 3, CV_32F); + cvtest::Rodrigues(Mat::eye(3, 3, CV_32F), caller.r_vec); + caller.A = Mat::eye(3, 3, CV_32F); + caller.distCoeffs = Mat::zeros(1, 5, CV_32F); + caller.aspectRatio0 = 1.0; /********************/ int errors = 0; + caller.initArgs(); + caller.objectPoints_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Zero objectPoints", caller ); - bad_caller = caller; - bad_caller.objectPoints = 0; - errors += run_test_case( CV_StsBadArg, "Zero objectPoints", bad_caller ); + caller.initArgs(); + caller.rvec_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Zero r_vec", caller ); - bad_caller = caller; - bad_caller.r_vec = 0; - errors += run_test_case( CV_StsBadArg, "Zero r_vec", bad_caller ); + caller.initArgs(); + caller.tvec_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Zero t_vec", caller ); - bad_caller = caller; - bad_caller.t_vec = 0; - errors += run_test_case( CV_StsBadArg, "Zero t_vec", bad_caller ); + caller.initArgs(); + caller.A_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Zero camMat", caller ); - bad_caller = caller; - bad_caller.A = 0; - errors += run_test_case( CV_StsBadArg, "Zero camMat", bad_caller ); + caller.initArgs(); + caller.imagePoints_arg = noArray(); + errors += run_test_case( CV_StsBadArg, "Zero imagePoints", caller ); - bad_caller = caller; - bad_caller.imagePoints = 0; - errors += run_test_case( CV_StsBadArg, "Zero imagePoints", bad_caller ); + Mat save_rvec = caller.r_vec; + caller.initArgs(); + caller.r_vec.create(2, 2, CV_32F); + errors += run_test_case( CV_StsBadArg, "Bad rvec format", caller ); + + caller.initArgs(); + caller.r_vec.create(1, 3, CV_8U); + errors += run_test_case( CV_StsBadArg, "Bad rvec format", caller ); + caller.r_vec = save_rvec; /****************************/ - Mat bad_r_vec_cpp1(r_vec_cpp.size(), CV_32S); CvMat bad_r_vec_c1 = cvMat(bad_r_vec_cpp1); - Mat bad_r_vec_cpp2(2, 2, CV_32F); CvMat bad_r_vec_c2 = cvMat(bad_r_vec_cpp2); - Mat bad_r_vec_cpp3(r_vec_cpp.size(), CV_32FC2); CvMat bad_r_vec_c3 = cvMat(bad_r_vec_cpp3); + Mat save_tvec = caller.t_vec; + caller.initArgs(); + caller.t_vec.create(3, 3, CV_32F); + errors += run_test_case( CV_StsBadArg, "Bad tvec format", caller ); - bad_caller = caller; - bad_caller.r_vec = &bad_r_vec_c1; - errors += run_test_case( CV_StsBadArg, "Bad rvec format", bad_caller ); - - bad_caller = caller; - bad_caller.r_vec = &bad_r_vec_c2; - errors += run_test_case( CV_StsBadArg, "Bad rvec format", bad_caller ); - - bad_caller = caller; - bad_caller.r_vec = &bad_r_vec_c3; - errors += run_test_case( CV_StsBadArg, "Bad rvec format", bad_caller ); + caller.initArgs(); + caller.t_vec.create(1, 3, CV_8U); + errors += run_test_case( CV_StsBadArg, "Bad tvec format", caller ); + caller.t_vec = save_tvec; /****************************/ - Mat bad_t_vec_cpp1(t_vec_cpp.size(), CV_32S); CvMat bad_t_vec_c1 = cvMat(bad_t_vec_cpp1); - Mat bad_t_vec_cpp2(2, 2, CV_32F); CvMat bad_t_vec_c2 = cvMat(bad_t_vec_cpp2); - Mat bad_t_vec_cpp3(1, 1, CV_32FC2); CvMat bad_t_vec_c3 = cvMat(bad_t_vec_cpp3); - - bad_caller = caller; - bad_caller.t_vec = &bad_t_vec_c1; - errors += run_test_case( CV_StsBadArg, "Bad tvec format", bad_caller ); - - bad_caller = caller; - bad_caller.t_vec = &bad_t_vec_c2; - errors += run_test_case( CV_StsBadArg, "Bad tvec format", bad_caller ); - - bad_caller = caller; - bad_caller.t_vec = &bad_t_vec_c3; - errors += run_test_case( CV_StsBadArg, "Bad tvec format", bad_caller ); + Mat save_A = caller.A; + caller.initArgs(); + caller.A.create(2, 2, CV_32F); + errors += run_test_case( CV_StsBadArg, "Bad A format", caller ); + caller.A = save_A; /****************************/ - Mat bad_A_cpp1(A_cpp.size(), CV_32S); CvMat bad_A_c1 = cvMat(bad_A_cpp1); - Mat bad_A_cpp2(2, 2, CV_32F); CvMat bad_A_c2 = cvMat(bad_A_cpp2); - - bad_caller = caller; - bad_caller.A = &bad_A_c1; - errors += run_test_case( CV_StsBadArg, "Bad A format", bad_caller ); - - bad_caller = caller; - bad_caller.A = &bad_A_c2; - errors += run_test_case( CV_StsBadArg, "Bad A format", bad_caller ); - - /****************************/ - Mat bad_distCoeffs_cpp1(distCoeffs_cpp.size(), CV_32S); CvMat bad_distCoeffs_c1 = cvMat(bad_distCoeffs_cpp1); - Mat bad_distCoeffs_cpp2(2, 2, CV_32F); CvMat bad_distCoeffs_c2 = cvMat(bad_distCoeffs_cpp2); - Mat bad_distCoeffs_cpp3(1, 7, CV_32F); CvMat bad_distCoeffs_c3 = cvMat(bad_distCoeffs_cpp3); - - bad_caller = caller; - bad_caller.distCoeffs = &zeros; - errors += run_test_case( CV_StsBadArg, "Bad distCoeffs format", bad_caller ); - - bad_caller = caller; - bad_caller.distCoeffs = &bad_distCoeffs_c1; - errors += run_test_case( CV_StsBadArg, "Bad distCoeffs format", bad_caller ); - - bad_caller = caller; - bad_caller.distCoeffs = &bad_distCoeffs_c2; - errors += run_test_case( CV_StsBadArg, "Bad distCoeffs format", bad_caller ); - - bad_caller = caller; - bad_caller.distCoeffs = &bad_distCoeffs_c3; - errors += run_test_case( CV_StsBadArg, "Bad distCoeffs format", bad_caller ); - - - /****************************/ - Mat bad_dpdr_cpp1(dpdr_cpp.size(), CV_32S); CvMat bad_dpdr_c1 = cvMat(bad_dpdr_cpp1); - Mat bad_dpdr_cpp2(dpdr_cpp.cols+1, 3, CV_32F); CvMat bad_dpdr_c2 = cvMat(bad_dpdr_cpp2); - Mat bad_dpdr_cpp3(dpdr_cpp.cols, 7, CV_32F); CvMat bad_dpdr_c3 = cvMat(bad_dpdr_cpp3); - - bad_caller = caller; - bad_caller.dpdr = &zeros; - errors += run_test_case( CV_StsBadArg, "Bad dpdr format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdr = &bad_dpdr_c1; - errors += run_test_case( CV_StsBadArg, "Bad dpdr format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdr = &bad_dpdr_c2; - errors += run_test_case( CV_StsBadArg, "Bad dpdr format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdr = &bad_dpdr_c3; - errors += run_test_case( CV_StsBadArg, "Bad dpdr format", bad_caller ); - - /****************************/ - - bad_caller = caller; - bad_caller.dpdt = &zeros; - errors += run_test_case( CV_StsBadArg, "Bad dpdt format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdt = &bad_dpdr_c1; - errors += run_test_case( CV_StsBadArg, "Bad dpdt format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdt = &bad_dpdr_c2; - errors += run_test_case( CV_StsBadArg, "Bad dpdt format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdt = &bad_dpdr_c3; - errors += run_test_case( CV_StsBadArg, "Bad dpdt format", bad_caller ); - - /****************************/ - - Mat bad_dpdf_cpp2(dpdr_cpp.cols+1, 2, CV_32F); CvMat bad_dpdf_c2 = cvMat(bad_dpdf_cpp2); - - bad_caller = caller; - bad_caller.dpdf = &zeros; - errors += run_test_case( CV_StsBadArg, "Bad dpdf format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdf = &bad_dpdr_c1; - errors += run_test_case( CV_StsBadArg, "Bad dpdf format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdf = &bad_dpdf_c2; - errors += run_test_case( CV_StsBadArg, "Bad dpdf format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdf = &bad_dpdr_c3; - errors += run_test_case( CV_StsBadArg, "Bad dpdf format", bad_caller ); - - /****************************/ - - bad_caller = caller; - bad_caller.dpdc = &zeros; - errors += run_test_case( CV_StsBadArg, "Bad dpdc format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdc = &bad_dpdr_c1; - errors += run_test_case( CV_StsBadArg, "Bad dpdc format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdc = &bad_dpdf_c2; - errors += run_test_case( CV_StsBadArg, "Bad dpdc format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdc = &bad_dpdr_c3; - errors += run_test_case( CV_StsBadArg, "Bad dpdc format", bad_caller ); - - /****************************/ - - bad_caller = caller; - bad_caller.dpdk = &zeros; - errors += run_test_case( CV_StsBadArg, "Bad dpdk format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdk = &bad_dpdr_c1; - errors += run_test_case( CV_StsBadArg, "Bad dpdk format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdk = &bad_dpdf_c2; - errors += run_test_case( CV_StsBadArg, "Bad dpdk format", bad_caller ); - - bad_caller = caller; - bad_caller.dpdk = &bad_dpdr_c3; - errors += run_test_case( CV_StsBadArg, "Bad dpdk format", bad_caller ); - - bad_caller = caller; - bad_caller.distCoeffs = 0; - errors += run_test_case( CV_StsNullPtr, "distCoeffs is NULL while dpdk is not", bad_caller ); - + Mat save_DC = caller.distCoeffs; + caller.initArgs(); + caller.distCoeffs.create(3, 3, CV_32F); + errors += run_test_case( CV_StsBadArg, "Bad distCoeffs format", caller ); + caller.distCoeffs = save_DC; if (errors) ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); @@ -776,8 +392,8 @@ protected: }; -TEST(Calib3d_CalibrateCamera_C, badarg) { CV_CameraCalibrationBadArgTest test; test.safe_run(); } -TEST(Calib3d_Rodrigues_C, badarg) { CV_Rodrigues2BadArgTest test; test.safe_run(); } -TEST(Calib3d_ProjectPoints_C, badarg) { CV_ProjectPoints2BadArgTest test; test.safe_run(); } +TEST(Calib3d_CalibrateCamera_CPP, badarg) { CV_CameraCalibrationBadArgTest test; test.safe_run(); } +TEST(Calib3d_Rodrigues_CPP, badarg) { CV_Rodrigues2BadArgTest test; test.safe_run(); } +TEST(Calib3d_ProjectPoints_CPP, badarg) { CV_ProjectPoints2BadArgTest test; test.safe_run(); } }} // namespace diff --git a/modules/calib3d/test/test_chesscorners_badarg.cpp b/modules/calib3d/test/test_chesscorners_badarg.cpp index 422e364d46..e58d9d925c 100644 --- a/modules/calib3d/test/test_chesscorners_badarg.cpp +++ b/modules/calib3d/test/test_chesscorners_badarg.cpp @@ -48,53 +48,32 @@ namespace opencv_test { namespace { class CV_ChessboardDetectorBadArgTest : public cvtest::BadArgTest { public: - CV_ChessboardDetectorBadArgTest(); + CV_ChessboardDetectorBadArgTest() { flags0 = 0; } protected: void run(int); bool checkByGenerator(); - bool cpp; - - /* cpp interface */ Mat img; - Size pattern_size; - int flags; + Size pattern_size, pattern_size0; + int flags, flags0; vector corners; + _InputArray img_arg; + _OutputArray corners_arg; - /* c interface */ - CvMat arr; - CvPoint2D32f* out_corners; - int* out_corner_count; - - - /* c interface draw corners */ - bool drawCorners; - CvMat drawCorImg; - bool was_found; + void initArgs() + { + img_arg = img; + corners_arg = corners; + pattern_size = pattern_size0; + flags = flags0; + } void run_func() { - if (cpp) - findChessboardCorners(img, pattern_size, corners, flags); - else - if (!drawCorners) - cvFindChessboardCorners( &arr, cvSize(pattern_size), out_corners, out_corner_count, flags ); - else - cvDrawChessboardCorners( &drawCorImg, cvSize(pattern_size), - (CvPoint2D32f*)(corners.empty() ? 0 : &corners[0]), - (int)corners.size(), was_found); + findChessboardCorners(img_arg, pattern_size, corners_arg, flags); } }; -CV_ChessboardDetectorBadArgTest::CV_ChessboardDetectorBadArgTest() -{ - cpp = false; - flags = 0; - out_corners = NULL; - out_corner_count = NULL; - drawCorners = was_found = false; -} - /* ///////////////////// chess_corner_test ///////////////////////// */ void CV_ChessboardDetectorBadArgTest::run( int /*start_from */) { @@ -111,34 +90,18 @@ void CV_ChessboardDetectorBadArgTest::run( int /*start_from */) /* /*//*/ */ int errors = 0; flags = CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE; - cpp = true; img = cb.clone(); + initArgs(); pattern_size = Size(2,2); - errors += run_test_case( CV_StsOutOfRange, "Invlid pattern size" ); - + errors += run_test_case( Error::StsOutOfRange, "Invlid pattern size" ); pattern_size = cbg.cornersSize(); + cb.convertTo(img, CV_32F); - errors += run_test_case( CV_StsUnsupportedFormat, "Not 8-bit image" ); + errors += run_test_case( Error::StsUnsupportedFormat, "Not 8-bit image" ); cv::merge(vector(2, cb), img); - errors += run_test_case( CV_StsUnsupportedFormat, "2 channel image" ); - - cpp = false; - drawCorners = false; - - img = cb.clone(); - arr = cvMat(img); - out_corner_count = 0; - out_corners = 0; - errors += run_test_case( CV_StsNullPtr, "Null pointer to corners" ); - - drawCorners = true; - Mat cvdrawCornImg(img.size(), CV_8UC2); - drawCorImg = cvMat(cvdrawCornImg); - was_found = true; - errors += run_test_case( CV_StsUnsupportedFormat, "2 channel image" ); - + errors += run_test_case( Error::StsUnsupportedFormat, "2 channel image" ); if (errors) ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); diff --git a/modules/calib3d/test/test_fundam.cpp b/modules/calib3d/test/test_fundam.cpp index 8b03de138e..874e8912d7 100644 --- a/modules/calib3d/test/test_fundam.cpp +++ b/modules/calib3d/test/test_fundam.cpp @@ -40,6 +40,7 @@ //M*/ #include "test_precomp.hpp" +#include "opencv2/core/core_c.h" #include "opencv2/calib3d/calib3d_c.h" namespace cvtest { @@ -349,8 +350,10 @@ static int cvTsRodrigues( const CvMat* src, CvMat* dst, CvMat* jacobian ) /*extern*/ void Rodrigues(const Mat& src, Mat& dst, Mat* jac) { - CV_Assert(src.data != dst.data && "Inplace is not supported"); - CV_Assert(!dst.empty() && "'dst' must be allocated"); + if(src.rows == 1 || src.cols == 1) + dst.create(3, 3, src.depth()); + else + dst.create(3, 1, src.depth()); CvMat _src = cvMat(src), _dst = cvMat(dst), _jac; if( jac ) _jac = cvMat(*jac); @@ -663,53 +666,36 @@ int CV_RodriguesTest::prepare_test_case( int test_case_idx ) void CV_RodriguesTest::run_func() { - CvMat v2m_jac, m2v_jac; - - if( calc_jacobians ) + cv::Mat v = test_mat[INPUT][0], M = test_mat[OUTPUT][0], v2 = test_mat[OUTPUT][2]; + cv::Mat M0 = M, v2_0 = v2; + if( !calc_jacobians ) { - v2m_jac = cvMat(test_mat[OUTPUT][1]); - m2v_jac = cvMat(test_mat[OUTPUT][3]); - } - - if( !test_cpp ) - { - CvMat _input = cvMat(test_mat[INPUT][0]), _output = cvMat(test_mat[OUTPUT][0]), _output2 = cvMat(test_mat[OUTPUT][2]); - cvRodrigues2( &_input, &_output, calc_jacobians ? &v2m_jac : 0 ); - cvRodrigues2( &_output, &_output2, calc_jacobians ? &m2v_jac : 0 ); + cv::Rodrigues(v, M); + cv::Rodrigues(M, v2); } else { - cv::Mat v = test_mat[INPUT][0], M = test_mat[OUTPUT][0], v2 = test_mat[OUTPUT][2]; - cv::Mat M0 = M, v2_0 = v2; - if( !calc_jacobians ) + cv::Mat J1 = test_mat[OUTPUT][1], J2 = test_mat[OUTPUT][3]; + cv::Mat J1_0 = J1, J2_0 = J2; + cv::Rodrigues(v, M, J1); + cv::Rodrigues(M, v2, J2); + if( J1.data != J1_0.data ) { - cv::Rodrigues(v, M); - cv::Rodrigues(M, v2); + if( J1.size() != J1_0.size() ) + J1 = J1.t(); + J1.convertTo(J1_0, J1_0.type()); } - else + if( J2.data != J2_0.data ) { - cv::Mat J1 = test_mat[OUTPUT][1], J2 = test_mat[OUTPUT][3]; - cv::Mat J1_0 = J1, J2_0 = J2; - cv::Rodrigues(v, M, J1); - cv::Rodrigues(M, v2, J2); - if( J1.data != J1_0.data ) - { - if( J1.size() != J1_0.size() ) - J1 = J1.t(); - J1.convertTo(J1_0, J1_0.type()); - } - if( J2.data != J2_0.data ) - { - if( J2.size() != J2_0.size() ) - J2 = J2.t(); - J2.convertTo(J2_0, J2_0.type()); - } + if( J2.size() != J2_0.size() ) + J2 = J2.t(); + J2.convertTo(J2_0, J2_0.type()); } - if( M.data != M0.data ) - M.reshape(M0.channels(), M0.rows).convertTo(M0, M0.type()); - if( v2.data != v2_0.data ) - v2.reshape(v2_0.channels(), v2_0.rows).convertTo(v2_0, v2_0.type()); } + if( M.data != M0.data ) + M.reshape(M0.channels(), M0.rows).convertTo(M0, M0.type()); + if( v2.data != v2_0.data ) + v2.reshape(v2_0.channels(), v2_0.rows).convertTo(v2_0, v2_0.type()); } @@ -851,19 +837,14 @@ void CV_FundamentalMatTest::get_test_array_types_and_sizes( int /*test_case_idx* types[INPUT][0] = CV_MAKETYPE(pt_depth, 1); + sizes[INPUT][0] = cvSize(dims, pt_count); if( cvtest::randInt(rng) % 2 ) - sizes[INPUT][0] = cvSize(pt_count, dims); - else { - sizes[INPUT][0] = cvSize(dims, pt_count); + types[INPUT][0] = CV_MAKETYPE(pt_depth, dims); if( cvtest::randInt(rng) % 2 ) - { - types[INPUT][0] = CV_MAKETYPE(pt_depth, dims); - if( cvtest::randInt(rng) % 2 ) - sizes[INPUT][0] = cvSize(pt_count, 1); - else - sizes[INPUT][0] = cvSize(1, pt_count); - } + sizes[INPUT][0] = cvSize(pt_count, 1); + else + sizes[INPUT][0] = cvSize(1, pt_count); } sizes[INPUT][1] = sizes[INPUT][0]; @@ -980,12 +961,12 @@ int CV_FundamentalMatTest::prepare_test_case( int test_case_idx ) void CV_FundamentalMatTest::run_func() { // cvFindFundamentalMat calls cv::findFundamentalMat - CvMat _input0 = cvMat(test_mat[INPUT][0]), _input1 = cvMat(test_mat[INPUT][1]); - CvMat F = cvMat(test_mat[TEMP][0]), mask = cvMat(test_mat[TEMP][1]); - f_result = cvFindFundamentalMat( &_input0, &_input1, &F, method, MAX(sigma*3, 0.01), 0, &mask ); + cv::Mat _input0 = test_mat[INPUT][0], _input1 = test_mat[INPUT][1]; + cv::Mat& F = test_mat[TEMP][0], &mask = test_mat[TEMP][1]; + F = cv::findFundamentalMat( _input0, _input1, method, MAX(sigma*3, 0.01), 0, mask ); + f_result = !F.empty(); } - void CV_FundamentalMatTest::prepare_to_validation( int test_case_idx ) { const Mat& Rt = test_mat[INPUT][3]; @@ -1025,7 +1006,11 @@ void CV_FundamentalMatTest::prepare_to_validation( int test_case_idx ) test_convertHomogeneous( test_mat[INPUT][0], p1 ); test_convertHomogeneous( test_mat[INPUT][1], p2 ); - cvtest::convert(test_mat[TEMP][0], F, F.type()); + Mat Fsrc = test_mat[TEMP][0]; + if( Fsrc.rows > 3 ) + Fsrc = Fsrc.rowRange(0, 3); + + cvtest::convert(Fsrc, F, F.type()); if( method <= CV_FM_8POINT ) memset( status, 1, pt_count ); @@ -1144,19 +1129,14 @@ void CV_EssentialMatTest::get_test_array_types_and_sizes( int /*test_case_idx*/, types[INPUT][0] = CV_MAKETYPE(pt_depth, 1); - if( 0 && cvtest::randInt(rng) % 2 ) - sizes[INPUT][0] = cvSize(pt_count, dims); - else + sizes[INPUT][0] = cvSize(dims, pt_count); + if( cvtest::randInt(rng) % 2 ) { - sizes[INPUT][0] = cvSize(dims, pt_count); + types[INPUT][0] = CV_MAKETYPE(pt_depth, dims); if( cvtest::randInt(rng) % 2 ) - { - types[INPUT][0] = CV_MAKETYPE(pt_depth, dims); - if( cvtest::randInt(rng) % 2 ) - sizes[INPUT][0] = cvSize(pt_count, 1); - else - sizes[INPUT][0] = cvSize(1, pt_count); - } + sizes[INPUT][0] = cvSize(pt_count, 1); + else + sizes[INPUT][0] = cvSize(1, pt_count); } sizes[INPUT][1] = sizes[INPUT][0]; @@ -1464,57 +1444,33 @@ void CV_ConvertHomogeneousTest::get_test_array_types_and_sizes( int /*test_case_ { RNG& rng = ts->get_rng(); int pt_depth1 = cvtest::randInt(rng) % 2 == 0 ? CV_32F : CV_64F; - int pt_depth2 = cvtest::randInt(rng) % 2 == 0 ? CV_32F : CV_64F; + int pt_depth2 = pt_depth1;//cvtest::randInt(rng) % 2 == 0 ? CV_32F : CV_64F; double pt_count_exp = cvtest::randReal(rng)*6 + 1; int t; pt_count = cvRound(exp(pt_count_exp)); pt_count = MAX( pt_count, 5 ); - dims1 = 2 + (cvtest::randInt(rng) % 3); - dims2 = 2 + (cvtest::randInt(rng) % 3); - - if( dims1 == dims2 + 2 ) - dims1--; - else if( dims1 == dims2 - 2 ) - dims1++; + dims1 = 2 + (cvtest::randInt(rng) % 2); + dims2 = dims1 + 1; if( cvtest::randInt(rng) % 2 ) CV_SWAP( dims1, dims2, t ); types[INPUT][0] = CV_MAKETYPE(pt_depth1, 1); + sizes[INPUT][0] = cvSize(dims1, pt_count); if( cvtest::randInt(rng) % 2 ) - sizes[INPUT][0] = cvSize(pt_count, dims1); - else { - sizes[INPUT][0] = cvSize(dims1, pt_count); + types[INPUT][0] = CV_MAKETYPE(pt_depth1, dims1); if( cvtest::randInt(rng) % 2 ) - { - types[INPUT][0] = CV_MAKETYPE(pt_depth1, dims1); - if( cvtest::randInt(rng) % 2 ) - sizes[INPUT][0] = cvSize(pt_count, 1); - else - sizes[INPUT][0] = cvSize(1, pt_count); - } + sizes[INPUT][0] = cvSize(pt_count, 1); + else + sizes[INPUT][0] = cvSize(1, pt_count); } - types[OUTPUT][0] = CV_MAKETYPE(pt_depth2, 1); - - if( cvtest::randInt(rng) % 2 ) - sizes[OUTPUT][0] = cvSize(pt_count, dims2); - else - { - sizes[OUTPUT][0] = cvSize(dims2, pt_count); - if( cvtest::randInt(rng) % 2 ) - { - types[OUTPUT][0] = CV_MAKETYPE(pt_depth2, dims2); - if( cvtest::randInt(rng) % 2 ) - sizes[OUTPUT][0] = cvSize(pt_count, 1); - else - sizes[OUTPUT][0] = cvSize(1, pt_count); - } - } + types[OUTPUT][0] = CV_MAKETYPE(pt_depth2, dims2); + sizes[OUTPUT][0] = cvSize(1, pt_count); types[REF_OUTPUT][0] = types[OUTPUT][0]; sizes[REF_OUTPUT][0] = sizes[OUTPUT][0]; @@ -1543,8 +1499,11 @@ void CV_ConvertHomogeneousTest::fill_array( int /*test_case_idx*/, int /*i*/, in void CV_ConvertHomogeneousTest::run_func() { - CvMat _input = cvMat(test_mat[INPUT][0]), _output = cvMat(test_mat[OUTPUT][0]); - cvConvertPointsHomogeneous( &_input, &_output ); + cv::Mat _input = test_mat[INPUT][0], &_output = test_mat[OUTPUT][0]; + if( dims1 > dims2 ) + cv::convertPointsFromHomogeneous(_input, _output); + else + cv::convertPointsToHomogeneous(_input, _output); } @@ -1600,7 +1559,7 @@ void CV_ComputeEpilinesTest::get_test_array_types_and_sizes( int /*test_case_idx RNG& rng = ts->get_rng(); int fm_depth = cvtest::randInt(rng) % 2 == 0 ? CV_32F : CV_64F; int pt_depth = cvtest::randInt(rng) % 2 == 0 ? CV_32F : CV_64F; - int ln_depth = cvtest::randInt(rng) % 2 == 0 ? CV_32F : CV_64F; + int ln_depth = pt_depth; double pt_count_exp = cvtest::randReal(rng)*6; which_image = 1 + (cvtest::randInt(rng) % 2); @@ -1613,40 +1572,21 @@ void CV_ComputeEpilinesTest::get_test_array_types_and_sizes( int /*test_case_idx types[INPUT][0] = CV_MAKETYPE(pt_depth, 1); - if( cvtest::randInt(rng) % 2 && !few_points ) - sizes[INPUT][0] = cvSize(pt_count, dims); - else + sizes[INPUT][0] = cvSize(dims, pt_count); + if( cvtest::randInt(rng) % 2 || few_points ) { - sizes[INPUT][0] = cvSize(dims, pt_count); - if( cvtest::randInt(rng) % 2 || few_points ) - { - types[INPUT][0] = CV_MAKETYPE(pt_depth, dims); - if( cvtest::randInt(rng) % 2 ) - sizes[INPUT][0] = cvSize(pt_count, 1); - else - sizes[INPUT][0] = cvSize(1, pt_count); - } + types[INPUT][0] = CV_MAKETYPE(pt_depth, dims); + if( cvtest::randInt(rng) % 2 ) + sizes[INPUT][0] = cvSize(pt_count, 1); + else + sizes[INPUT][0] = cvSize(1, pt_count); } types[INPUT][1] = CV_MAKETYPE(fm_depth, 1); sizes[INPUT][1] = cvSize(3, 3); - types[OUTPUT][0] = CV_MAKETYPE(ln_depth, 1); - - if( cvtest::randInt(rng) % 2 && !few_points ) - sizes[OUTPUT][0] = cvSize(pt_count, 3); - else - { - sizes[OUTPUT][0] = cvSize(3, pt_count); - if( cvtest::randInt(rng) % 2 || few_points ) - { - types[OUTPUT][0] = CV_MAKETYPE(ln_depth, 3); - if( cvtest::randInt(rng) % 2 ) - sizes[OUTPUT][0] = cvSize(pt_count, 1); - else - sizes[OUTPUT][0] = cvSize(1, pt_count); - } - } + types[OUTPUT][0] = CV_MAKETYPE(ln_depth, 3); + sizes[OUTPUT][0] = cvSize(1, pt_count); types[REF_OUTPUT][0] = types[OUTPUT][0]; sizes[REF_OUTPUT][0] = sizes[OUTPUT][0]; @@ -1678,8 +1618,8 @@ void CV_ComputeEpilinesTest::fill_array( int test_case_idx, int i, int j, Mat& a void CV_ComputeEpilinesTest::run_func() { - CvMat _points = cvMat(test_mat[INPUT][0]), _F = cvMat(test_mat[INPUT][1]), _lines = cvMat(test_mat[OUTPUT][0]); - cvComputeCorrespondEpilines( &_points, which_image, &_F, &_lines ); + cv::Mat _points = test_mat[INPUT][0], _F = test_mat[INPUT][1], &_lines = test_mat[OUTPUT][0]; + cv::computeCorrespondEpilines( _points, which_image, _F, _lines ); } diff --git a/modules/calib3d/test/test_posit.cpp b/modules/calib3d/test/test_posit.cpp index 5fef2ef904..d63ce33e0f 100644 --- a/modules/calib3d/test/test_posit.cpp +++ b/modules/calib3d/test/test_posit.cpp @@ -40,7 +40,9 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" + +// POSIT is not exposed to C++ API yet, so the test is disabled +#if 0 namespace opencv_test { namespace { @@ -219,4 +221,7 @@ void CV_POSITTest::run( int start_from ) TEST(Calib3d_POSIT, accuracy) { CV_POSITTest test; test.safe_run(); } }} // namespace + +#endif + /* End of file. */ diff --git a/modules/calib3d/test/test_reproject_image_to_3d.cpp b/modules/calib3d/test/test_reproject_image_to_3d.cpp index eaaefd90ce..c99de41708 100644 --- a/modules/calib3d/test/test_reproject_image_to_3d.cpp +++ b/modules/calib3d/test/test_reproject_image_to_3d.cpp @@ -41,7 +41,6 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" namespace opencv_test { namespace { @@ -121,14 +120,9 @@ protected: Mat_ Q(4, 4); randu(Q, Scalar(-5), Scalar(5)); - Mat_ _3dImg(disp.size()); - CvMat cvdisp = cvMat(disp); CvMat cv_3dImg = cvMat(_3dImg); CvMat cvQ = cvMat(Q); - cvReprojectImageTo3D( &cvdisp, &cv_3dImg, &cvQ, handleMissingValues ); - - if (std::numeric_limits::max() == std::numeric_limits::max()) - reprojectImageTo3D(disp, _3dImg, Q, handleMissingValues); + reprojectImageTo3D(disp, _3dImg, Q, handleMissingValues); for(int y = 0; y < disp.rows; ++y) for(int x = 0; x < disp.cols; ++x) diff --git a/modules/calib3d/test/test_undistort.cpp b/modules/calib3d/test/test_undistort.cpp index d358e5ac82..d0975cc4c2 100644 --- a/modules/calib3d/test/test_undistort.cpp +++ b/modules/calib3d/test/test_undistort.cpp @@ -42,7 +42,6 @@ #include "test_precomp.hpp" #include "opencv2/imgproc/imgproc_c.h" -#include "opencv2/calib3d/calib3d_c.h" namespace opencv_test { namespace { @@ -172,7 +171,6 @@ protected: const CvMat* _distCoeffs, const CvMat* matR, const CvMat* matP); private: - bool useCPlus; bool useDstMat; static const int N_POINTS = 10; static const int MAX_X = 2048; @@ -204,7 +202,7 @@ CV_UndistortPointsTest::CV_UndistortPointsTest() test_array[TEMP].push_back(NULL); // dst points test_array[REF_OUTPUT].push_back(NULL); - useCPlus = useDstMat = false; + useDstMat = false; zero_new_cam = zero_distortion = zero_R = false; } @@ -212,16 +210,8 @@ void CV_UndistortPointsTest::get_test_array_types_and_sizes( int test_case_idx, { cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx,sizes,types); RNG& rng = ts->get_rng(); - useCPlus = ((cvtest::randInt(rng) % 2)!=0); - //useCPlus = 0; - if (useCPlus) - { - types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = types[TEMP][0]= CV_32FC2; - } - else - { - types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = types[TEMP][0]= cvtest::randInt(rng)%2 ? CV_64FC2 : CV_32FC2; - } + //rng.next(); + types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = types[TEMP][0]= CV_32FC2; types[INPUT][1] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; types[INPUT][2] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; types[INPUT][3] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; @@ -364,15 +354,12 @@ int CV_UndistortPointsTest::prepare_test_case(int test_case_idx) zero_new_cam = (cvtest::randInt(rng)%2) == 0 ? false : true; zero_R = (cvtest::randInt(rng)%2) == 0 ? false : true; - if (useCPlus) - { - _points.convertTo(src_points, CV_32F); + _points.convertTo(src_points, CV_32F); - camera_mat = test_mat[INPUT][1]; - distortion_coeffs = test_mat[INPUT][2]; - R = test_mat[INPUT][3]; - P = test_mat[INPUT][4]; - } + camera_mat = test_mat[INPUT][1]; + distortion_coeffs = test_mat[INPUT][2]; + R = test_mat[INPUT][3]; + P = test_mat[INPUT][4]; return code; } @@ -407,28 +394,21 @@ void CV_UndistortPointsTest::prepare_to_validation(int /*test_case_idx*/) cvtest::convert(test_mat[INPUT][3], __rot, __rot.type()); cvtest::convert(test_mat[INPUT][4], __proj, __proj.type()); - if (useCPlus) + if (useDstMat) { - if (useDstMat) + CvMat temp = cvMat(dst_points_mat); + for (int i=0;i >& sizes, vector >& types ) { cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx,sizes,types); RNG& rng = ts->get_rng(); - useCPlus = ((cvtest::randInt(rng) % 2)!=0); - //useCPlus = 0; - types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_64FC2; + //rng.next(); + map_type = CV_32F; + types[OUTPUT][0] = types[OUTPUT][1] = types[REF_OUTPUT][0] = types[REF_OUTPUT][1] = map_type; + + img_size.width = cvtest::randInt(rng) % MAX_X + 1; + img_size.height = cvtest::randInt(rng) % MAX_Y + 1; + + types[INPUT][0] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; types[INPUT][1] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; types[INPUT][2] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; types[INPUT][3] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; - types[INPUT][4] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F; - sizes[INPUT][0] = sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = cvSize(N_POINTS,1); - sizes[INPUT][1] = sizes[INPUT][3] = cvSize(3,3); - sizes[INPUT][4] = cvSize(3,3); + sizes[OUTPUT][0] = sizes[OUTPUT][1] = sizes[REF_OUTPUT][0] = sizes[REF_OUTPUT][1] = img_size; + sizes[INPUT][0] = sizes[INPUT][2] = sizes[INPUT][3] = cvSize(3,3); + + Size dsize; if (cvtest::randInt(rng)%2) { if (cvtest::randInt(rng)%2) { - sizes[INPUT][2] = cvSize(1,4); + dsize = Size(1,4); } else { - sizes[INPUT][2] = cvSize(1,5); + dsize = Size(1,5); } } else { if (cvtest::randInt(rng)%2) { - sizes[INPUT][2] = cvSize(4,1); + dsize = Size(4,1); } else { - sizes[INPUT][2] = cvSize(5,1); + dsize = Size(5,1); } } + sizes[INPUT][1] = dsize; } @@ -668,48 +622,14 @@ int CV_InitUndistortRectifyMapTest::prepare_test_case(int test_case_idx) if (code <= 0) return code; - img_size.width = cvtest::randInt(rng) % MAX_X + 1; - img_size.height = cvtest::randInt(rng) % MAX_Y + 1; - - if (useCPlus) - { - mat_type = (cvtest::randInt(rng) % 2) == 0 ? CV_32FC1 : CV_16SC2; - if ((cvtest::randInt(rng) % 4) == 0) - mat_type = -1; - if ((cvtest::randInt(rng) % 4) == 0) - mat_type = CV_32FC2; - _mapx = 0; - _mapy = 0; - } - else - { - int typex = (cvtest::randInt(rng) % 2) == 0 ? CV_32FC1 : CV_16SC2; - //typex = CV_32FC1; ///!!!!!!!!!!!!!!!! - int typey = (typex == CV_32FC1) ? CV_32FC1 : CV_16UC1; - - _mapx = cvCreateMat(img_size.height,img_size.width,typex); - _mapy = cvCreateMat(img_size.height,img_size.width,typey); - - - } - - int dist_size = test_mat[INPUT][2].cols > test_mat[INPUT][2].rows ? test_mat[INPUT][2].cols : test_mat[INPUT][2].rows; + int dist_size = test_mat[INPUT][1].cols > test_mat[INPUT][1].rows ? test_mat[INPUT][1].cols : test_mat[INPUT][1].rows; double cam[9] = {0,0,0,0,0,0,0,0,1}; vector dist(dist_size); - vector new_cam(test_mat[INPUT][4].cols * test_mat[INPUT][4].rows); - vector points(N_POINTS); + vector new_cam(test_mat[INPUT][3].cols * test_mat[INPUT][3].rows); Mat _camera(3,3,CV_64F,cam); - Mat _distort(test_mat[INPUT][2].size(),CV_64F,&dist[0]); - Mat _new_cam(test_mat[INPUT][4].size(),CV_64F,&new_cam[0]); - Mat _points(test_mat[INPUT][0].size(),CV_64FC2, &points[0]); - - //Generating points - for (int i=0;i test_mat[INPUT][2].rows ? test_mat[INPUT][2].cols : test_mat[INPUT][2].rows; - double cam[9] = {0,0,0,0,0,0,0,0,1}; - double rot[9] = {1,0,0,0,1,0,0,0,1}; - vector dist(dist_size); - vector new_cam(test_mat[INPUT][4].cols * test_mat[INPUT][4].rows); - vector points(N_POINTS); - vector r_points(N_POINTS); - //Run reference calculations - Mat ref_points(test_mat[INPUT][0].size(),CV_64FC2,&r_points[0]); - Mat _camera(3,3,CV_64F,cam); - Mat _rot(3,3,CV_64F,rot); - Mat _distort(test_mat[INPUT][2].size(),CV_64F,&dist[0]); - Mat _new_cam(test_mat[INPUT][4].size(),CV_64F,&new_cam[0]); - Mat _points(test_mat[INPUT][0].size(),CV_64FC2,&points[0]); - - cvtest::convert(test_mat[INPUT][1],_camera,_camera.type()); - cvtest::convert(test_mat[INPUT][2],_distort,_distort.type()); - cvtest::convert(test_mat[INPUT][3],_rot,_rot.type()); - cvtest::convert(test_mat[INPUT][4],_new_cam,_new_cam.type()); - - //Applying precalculated undistort rectify map - if (!useCPlus) - { - mapx = cv::Mat(_mapx); - mapy = cv::Mat(_mapy); - } - cv::Mat map1,map2; - cv::convertMaps(mapx,mapy,map1,map2,CV_32FC1); - CvMat _map1 = map1; - CvMat _map2 = map2; - const Point2d* sptr = (const Point2d*)test_mat[INPUT][0].data; - for( int i = 0;i < N_POINTS; i++ ) - { - int u = saturate_cast(sptr[i].x); - int v = saturate_cast(sptr[i].y); - points[i].x = _map1.data.fl[v*_map1.cols + u]; - points[i].y = _map2.data.fl[v*_map2.cols + u]; - } - - //--- - - cv::undistortPoints(_points, ref_points, _camera, - zero_distortion ? Mat() : _distort, - zero_R ? Mat::eye(3,3,CV_64F) : _rot, - zero_new_cam ? _camera : _new_cam); - //cvTsDistortPoints(&_points,&ref_points,&_camera,&_distort,&_rot,&_new_cam); - cvtest::convert(ref_points, test_mat[REF_OUTPUT][0], test_mat[REF_OUTPUT][0].type()); - cvtest::copy(test_mat[INPUT][0],test_mat[OUTPUT][0]); - - cvReleaseMat(&_mapx); - cvReleaseMat(&_mapy); -#else - int dist_size = test_mat[INPUT][2].cols > test_mat[INPUT][2].rows ? test_mat[INPUT][2].cols : test_mat[INPUT][2].rows; - double cam[9] = {0,0,0,0,0,0,0,0,1}; - double rot[9] = {1,0,0,0,1,0,0,0,1}; - double* dist = new double[dist_size ]; - double* new_cam = new double[test_mat[INPUT][4].cols * test_mat[INPUT][4].rows]; - double* points = new double[N_POINTS*2]; - double* r_points = new double[N_POINTS*2]; - //Run reference calculations - CvMat ref_points= cvMat(test_mat[INPUT][0].rows,test_mat[INPUT][0].cols,CV_64FC2,r_points); - CvMat _camera = cvMat(3,3,CV_64F,cam); - CvMat _rot = cvMat(3,3,CV_64F,rot); - CvMat _distort = cvMat(test_mat[INPUT][2].rows,test_mat[INPUT][2].cols,CV_64F,dist); - CvMat _new_cam = cvMat(test_mat[INPUT][4].rows,test_mat[INPUT][4].cols,CV_64F,new_cam); - CvMat _points= cvMat(test_mat[INPUT][0].rows,test_mat[INPUT][0].cols,CV_64FC2,points); - - CvMat _input1 = cvMat(test_mat[INPUT][1]); - CvMat _input2 = cvMat(test_mat[INPUT][2]); - CvMat _input3 = cvMat(test_mat[INPUT][3]); - CvMat _input4 = cvMat(test_mat[INPUT][4]); - - cvtest::convert(cvarrToMat(&_input1), cvarrToMat(&_camera), -1); - cvtest::convert(cvarrToMat(&_input2), cvarrToMat(&_distort), -1); - cvtest::convert(cvarrToMat(&_input3), cvarrToMat(&_rot), -1); - cvtest::convert(cvarrToMat(&_input4), cvarrToMat(&_new_cam), -1); - - //Applying precalculated undistort rectify map - if (!useCPlus) - { - mapx = cv::cvarrToMat(_mapx); - mapy = cv::cvarrToMat(_mapy); - } - cv::Mat map1,map2; - cv::convertMaps(mapx,mapy,map1,map2,CV_32FC1); - CvMat _map1 = cvMat(map1); - CvMat _map2 = cvMat(map2); - for (int i=0;i()[2*i]; - double v = test_mat[INPUT][0].ptr()[2*i+1]; - _points.data.db[2*i] = (double)_map1.data.fl[(int)v*_map1.cols+(int)u]; - _points.data.db[2*i+1] = (double)_map2.data.fl[(int)v*_map2.cols+(int)u]; - } - - //--- - - cvUndistortPoints(&_points,&ref_points,&_camera, - zero_distortion ? 0 : &_distort, zero_R ? 0 : &_rot, zero_new_cam ? &_camera : &_new_cam); - //cvTsDistortPoints(&_points,&ref_points,&_camera,&_distort,&_rot,&_new_cam); - CvMat dst = cvMat(test_mat[REF_OUTPUT][0]); - cvtest::convert(cvarrToMat(&ref_points), cvarrToMat(&dst), -1); - - cvtest::copy(test_mat[INPUT][0],test_mat[OUTPUT][0]); - - delete[] dist; - delete[] new_cam; - delete[] points; - delete[] r_points; - cvReleaseMat(&_mapx); - cvReleaseMat(&_mapy); -#endif + cvtest::initUndistortMap(test_mat[INPUT][0], + zero_distortion ? cv::Mat() : test_mat[INPUT][1], + zero_R ? cv::Mat() : test_mat[INPUT][2], + zero_new_cam ? test_mat[INPUT][0] : test_mat[INPUT][3], + img_size, test_mat[REF_OUTPUT][0], test_mat[REF_OUTPUT][1], + test_mat[REF_OUTPUT][0].type()); } void CV_InitUndistortRectifyMapTest::run_func() { - if (useCPlus) - { - cv::Mat input2,input3,input4; - input2 = zero_distortion ? cv::Mat() : test_mat[INPUT][2]; - input3 = zero_R ? cv::Mat() : test_mat[INPUT][3]; - input4 = zero_new_cam ? cv::Mat() : test_mat[INPUT][4]; - cv::initUndistortRectifyMap(camera_mat,input2,input3,input4,img_size,mat_type,mapx,mapy); - } - else - { - CvMat input1 = cvMat(test_mat[INPUT][1]), input2, input3, input4; - if( !zero_distortion ) - input2 = cvMat(test_mat[INPUT][2]); - if( !zero_R ) - input3 = cvMat(test_mat[INPUT][3]); - if( !zero_new_cam ) - input4 = cvMat(test_mat[INPUT][4]); - cvInitUndistortRectifyMap(&input1, - zero_distortion ? 0 : &input2, - zero_R ? 0 : &input3, - zero_new_cam ? 0 : &input4, - _mapx,_mapy); - } + cv::Mat camera_mat = test_mat[INPUT][0]; + cv::Mat dist = zero_distortion ? cv::Mat() : test_mat[INPUT][1]; + cv::Mat R = zero_R ? cv::Mat() : test_mat[INPUT][2]; + cv::Mat new_cam = zero_new_cam ? cv::Mat() : test_mat[INPUT][3]; + cv::Mat& mapx = test_mat[OUTPUT][0], &mapy = test_mat[OUTPUT][1]; + cv::initUndistortRectifyMap(camera_mat,dist,R,new_cam,img_size,map_type,mapx,mapy); } double CV_InitUndistortRectifyMapTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ ) @@ -1227,7 +1013,6 @@ protected: void fill_array( int test_case_idx, int i, int j, Mat& arr ); private: - bool useCPlus; cv::Mat input0; cv::Mat input1; cv::Mat input2; @@ -1276,21 +1061,13 @@ void CV_UndistortTest::fill_array( int test_case_idx, int i, int j, Mat& arr ) void CV_UndistortTest::run_func() { - if (!useCPlus) + if (zero_distortion) { - CvMat a = cvMat(test_mat[INPUT][1]), k = cvMat(test_mat[INPUT][2]); - cvUndistort2( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], &a, &k); + cv::undistort(input0,input_output,input1,cv::Mat()); } else { - if (zero_distortion) - { - cv::undistort(input0,input_output,input1,cv::Mat()); - } - else - { - cv::undistort(input0,input_output,input1,input2); - } + cv::undistort(input0,input_output,input1,input2); } } @@ -1353,14 +1130,11 @@ int CV_UndistortTest::prepare_test_case( int test_case_idx ) _new_cam.convertTo(_new_cam0, _new_cam0.depth()); //Testing C++ code - useCPlus = ((cvtest::randInt(rng) % 2)!=0); - if (useCPlus) - { - input0 = test_mat[INPUT][0]; - input1 = test_mat[INPUT][1]; - input2 = test_mat[INPUT][2]; - input_new_cam = test_mat[INPUT][3]; - } + //useCPlus = ((cvtest::randInt(rng) % 2)!=0); + input0 = test_mat[INPUT][0]; + input1 = test_mat[INPUT][1]; + input2 = test_mat[INPUT][2]; + input_new_cam = test_mat[INPUT][3]; return code; } @@ -1368,16 +1142,14 @@ int CV_UndistortTest::prepare_test_case( int test_case_idx ) void CV_UndistortTest::prepare_to_validation( int /*test_case_idx*/ ) { - if (useCPlus) - { - Mat& output = test_mat[INPUT_OUTPUT][0]; - input_output.convertTo(output, output.type()); - } + Mat& output = test_mat[INPUT_OUTPUT][0]; + input_output.convertTo(output, output.type()); Mat& src = test_mat[INPUT][0]; Mat& dst = test_mat[REF_INPUT_OUTPUT][0]; Mat& dst0 = test_mat[INPUT_OUTPUT][0]; Mat mapx, mapy; - cvtest::initUndistortMap( test_mat[INPUT][1], test_mat[INPUT][2], dst.size(), mapx, mapy ); + cvtest::initUndistortMap( test_mat[INPUT][1], test_mat[INPUT][2], + Mat(), Mat(), dst.size(), mapx, mapy, CV_32F ); Mat mask( dst.size(), CV_8U ); test_remap( src, dst, mapx, mapy, &mask, interpolation ); dst.setTo(Scalar::all(0), mask); @@ -1446,12 +1218,13 @@ void CV_UndistortMapTest::fill_array( int test_case_idx, int i, int j, Mat& arr void CV_UndistortMapTest::run_func() { - CvMat a = cvMat(test_mat[INPUT][0]), k = cvMat(test_mat[INPUT][1]); + cv::Mat a = test_mat[INPUT][0], k = test_mat[INPUT][1]; + cv::Mat &mapx = test_mat[OUTPUT][0], &mapy = !dualChannel ? test_mat[OUTPUT][1] : mapx; + cv::Size mapsz = test_mat[OUTPUT][0].size(); - if (!dualChannel ) - cvInitUndistortMap( &a, &k, test_array[OUTPUT][0], test_array[OUTPUT][1] ); - else - cvInitUndistortMap( &a, &k, test_array[OUTPUT][0], 0 ); + cv::initUndistortRectifyMap(a, k, cv::Mat(), a, + mapsz, dualChannel ? CV_32FC2 : CV_32FC1, + mapx, !dualChannel ? cv::_InputOutputArray(mapy) : cv::noArray()); } @@ -1503,7 +1276,8 @@ int CV_UndistortMapTest::prepare_test_case( int test_case_idx ) void CV_UndistortMapTest::prepare_to_validation( int ) { Mat mapx, mapy; - cvtest::initUndistortMap( test_mat[INPUT][0], test_mat[INPUT][1], test_mat[REF_OUTPUT][0].size(), mapx, mapy ); + cvtest::initUndistortMap( test_mat[INPUT][0], test_mat[INPUT][1], Mat(), Mat(), + test_mat[REF_OUTPUT][0].size(), mapx, mapy, CV_32F ); if( !dualChannel ) { mapx.copyTo(test_mat[REF_OUTPUT][0]); diff --git a/modules/calib3d/test/test_undistort_badarg.cpp b/modules/calib3d/test/test_undistort_badarg.cpp index 11de8c91ae..e30ad8af99 100644 --- a/modules/calib3d/test/test_undistort_badarg.cpp +++ b/modules/calib3d/test/test_undistort_badarg.cpp @@ -40,7 +40,7 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/calib3d/calib3d_c.h" +#include "opencv2/core/core_c.h" namespace opencv_test { namespace { @@ -55,19 +55,9 @@ protected: private: //common cv::Size img_size; - bool useCPlus; //static const int N_POINTS = 1; static const int N_POINTS2 = 2; - //C - CvMat* _camera_mat; - CvMat* matR; - CvMat* matP; - CvMat* _distortion_coeffs; - CvMat* _src_points; - CvMat* _dst_points; - - //C++ cv::Mat camera_mat; cv::Mat R; @@ -80,27 +70,17 @@ private: CV_UndistortPointsBadArgTest::CV_UndistortPointsBadArgTest () { - useCPlus = false; - _camera_mat = matR = matP = _distortion_coeffs = _src_points = _dst_points = NULL; } void CV_UndistortPointsBadArgTest::run_func() { - if (useCPlus) - { - cv::undistortPoints(src_points,dst_points,camera_mat,distortion_coeffs,R,P); - } - else - { - cvUndistortPoints(_src_points,_dst_points,_camera_mat,_distortion_coeffs,matR,matP); - } + cv::undistortPoints(src_points,dst_points,camera_mat,distortion_coeffs,R,P); } void CV_UndistortPointsBadArgTest::run(int) { //RNG& rng = ts->get_rng(); int errcount = 0; - useCPlus = false; //initializing img_size.width = 800; img_size.height = 600; @@ -110,7 +90,6 @@ void CV_UndistortPointsBadArgTest::run(int) static_cast(img_size.width) / 4.0, static_cast(img_size.height) / 4.0, }; - double d_points[N_POINTS2]; double p[9] = {155.f, 0.f, img_size.width/2.f+img_size.width/50.f, 0, 310.f, img_size.height/2.f+img_size.height/50.f, 0.f, 0.f, 1.f}; double r[9] = {1,0,0,0,1,0,0,0,1}; @@ -119,137 +98,6 @@ void CV_UndistortPointsBadArgTest::run(int) CvMat _P_orig = cvMat(3,3,CV_64F,p); CvMat _R_orig = cvMat(3,3,CV_64F,r); CvMat _src_points_orig = cvMat(1,4,CV_64FC2,s_points); - CvMat _dst_points_orig = cvMat(1,4,CV_64FC2,d_points); - - _camera_mat = &_camera_mat_orig; - _distortion_coeffs = &_distortion_coeffs_orig; - matP = &_P_orig; - matR = &_R_orig; - _src_points = &_src_points_orig; - _dst_points = &_dst_points_orig; - -//tests - CvMat* temp1; - CvMat* temp; - IplImage* temp_img = cvCreateImage(cvSize(img_size.width,img_size.height),8,3); - -//----------- - temp = (CvMat*)temp_img; - _src_points = temp; - errcount += run_test_case( CV_StsAssert, "Input data is not CvMat*" ); - _src_points = &_src_points_orig; - - temp = (CvMat*)temp_img; - _dst_points = temp; - errcount += run_test_case( CV_StsAssert, "Output data is not CvMat*" ); - _dst_points = &_dst_points_orig; - - temp = cvCreateMat(2,3,CV_64F); - _src_points = temp; - errcount += run_test_case( CV_StsAssert, "Invalid input data matrix size" ); - _src_points = &_src_points_orig; - cvReleaseMat(&temp); - - temp = cvCreateMat(2,3,CV_64F); - _dst_points = temp; - errcount += run_test_case(CV_StsAssert, "Invalid output data matrix size" ); - _dst_points = &_dst_points_orig; - cvReleaseMat(&temp); - - temp = cvCreateMat(1,3,CV_64F); - temp1 = cvCreateMat(4,1,CV_64F); - _dst_points = temp; - _src_points = temp1; - errcount += run_test_case(CV_StsAssert, "Output and input data sizes mismatch" ); - _dst_points = &_dst_points_orig; - _src_points = &_src_points_orig; - cvReleaseMat(&temp); - cvReleaseMat(&temp1); - - temp = cvCreateMat(1,3,CV_32S); - _dst_points = temp; - errcount += run_test_case(CV_StsAssert, "Invalid output data matrix type" ); - _dst_points = &_dst_points_orig; - cvReleaseMat(&temp); - - temp = cvCreateMat(1,3,CV_32S); - _src_points = temp; - errcount += run_test_case(CV_StsAssert, "Invalid input data matrix type" ); - _src_points = &_src_points_orig; - cvReleaseMat(&temp); -//------------ - temp = cvCreateMat(2,3,CV_64F); - _camera_mat = temp; - errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" ); - _camera_mat = &_camera_mat_orig; - cvReleaseMat(&temp); - - temp = cvCreateMat(3,4,CV_64F); - _camera_mat = temp; - errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" ); - _camera_mat = &_camera_mat_orig; - cvReleaseMat(&temp); - - temp = (CvMat*)temp_img; - _camera_mat = temp; - errcount += run_test_case( CV_StsAssert, "Camera data is not CvMat*" ); - _camera_mat = &_camera_mat_orig; -//---------- - - temp = (CvMat*)temp_img; - _distortion_coeffs = temp; - errcount += run_test_case( CV_StsAssert, "Distortion coefficients data is not CvMat*" ); - _distortion_coeffs = &_distortion_coeffs_orig; - - temp = cvCreateMat(1,6,CV_64F); - _distortion_coeffs = temp; - errcount += run_test_case( CV_StsAssert, "Invalid distortion coefficients data matrix size" ); - _distortion_coeffs = &_distortion_coeffs_orig; - cvReleaseMat(&temp); - - temp = cvCreateMat(3,3,CV_64F); - _distortion_coeffs = temp; - errcount += run_test_case( CV_StsAssert, "Invalid distortion coefficients data matrix size" ); - _distortion_coeffs = &_distortion_coeffs_orig; - cvReleaseMat(&temp); -//---------- - temp = (CvMat*)temp_img; - matR = temp; - errcount += run_test_case( CV_StsAssert, "R data is not CvMat*" ); - matR = &_R_orig; - - temp = cvCreateMat(4,3,CV_64F); - matR = temp; - errcount += run_test_case( CV_StsAssert, "Invalid R data matrix size" ); - matR = &_R_orig; - cvReleaseMat(&temp); - - temp = cvCreateMat(3,2,CV_64F); - matR = temp; - errcount += run_test_case( CV_StsAssert, "Invalid R data matrix size" ); - matR = &_R_orig; - cvReleaseMat(&temp); - -//----------- - temp = (CvMat*)temp_img; - matP = temp; - errcount += run_test_case( CV_StsAssert, "P data is not CvMat*" ); - matP = &_P_orig; - - temp = cvCreateMat(4,3,CV_64F); - matP = temp; - errcount += run_test_case( CV_StsAssert, "Invalid P data matrix size" ); - matP = &_P_orig; - cvReleaseMat(&temp); - - temp = cvCreateMat(3,2,CV_64F); - matP = temp; - errcount += run_test_case( CV_StsAssert, "Invalid P data matrix size" ); - matP = &_P_orig; - cvReleaseMat(&temp); -//------------ - //C++ tests - useCPlus = true; camera_mat = cv::cvarrToMat(&_camera_mat_orig); distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig); @@ -257,27 +105,19 @@ void CV_UndistortPointsBadArgTest::run(int) R = cv::cvarrToMat(&_R_orig); src_points = cv::cvarrToMat(&_src_points_orig); - temp = cvCreateMat(2,2,CV_32FC2); - src_points = cv::cvarrToMat(temp); + src_points.create(2, 2, CV_32F); errcount += run_test_case( CV_StsAssert, "Invalid input data matrix size" ); src_points = cv::cvarrToMat(&_src_points_orig); - cvReleaseMat(&temp); - temp = cvCreateMat(1,4,CV_64FC2); - src_points = cv::cvarrToMat(temp); + src_points.create(1, 4, CV_64FC2); errcount += run_test_case( CV_StsAssert, "Invalid input data matrix type" ); src_points = cv::cvarrToMat(&_src_points_orig); - cvReleaseMat(&temp); src_points = cv::Mat(); errcount += run_test_case( CV_StsAssert, "Input data matrix is not continuous" ); src_points = cv::cvarrToMat(&_src_points_orig); - cvReleaseMat(&temp); - - //------------ - cvReleaseImage(&temp_img); ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK); } @@ -292,20 +132,7 @@ protected: void run_func(); private: - //common cv::Size img_size; - bool useCPlus; - - //C - CvMat* _camera_mat; - CvMat* matR; - CvMat* _new_camera_mat; - CvMat* _distortion_coeffs; - CvMat* _mapx; - CvMat* _mapy; - - - //C++ cv::Mat camera_mat; cv::Mat R; cv::Mat new_camera_mat; @@ -318,20 +145,11 @@ private: CV_InitUndistortRectifyMapBadArgTest::CV_InitUndistortRectifyMapBadArgTest () { - useCPlus = false; - _camera_mat = matR = _new_camera_mat = _distortion_coeffs = _mapx = _mapy = NULL; } void CV_InitUndistortRectifyMapBadArgTest::run_func() { - if (useCPlus) - { - cv::initUndistortRectifyMap(camera_mat,distortion_coeffs,R,new_camera_mat,img_size,mat_type,mapx,mapy); - } - else - { - cvInitUndistortRectifyMap(_camera_mat,_distortion_coeffs,matR,_new_camera_mat,_mapx,_mapy); - } + cv::initUndistortRectifyMap(camera_mat,distortion_coeffs,R,new_camera_mat,img_size,mat_type,mapx,mapy); } void CV_InitUndistortRectifyMapBadArgTest::run(int) @@ -342,8 +160,8 @@ void CV_InitUndistortRectifyMapBadArgTest::run(int) img_size.height = 600; double cam[9] = {150.f, 0.f, img_size.width/2.f, 0, 300.f, img_size.height/2.f, 0.f, 0.f, 1.f}; double dist[4] = {0.01,0.02,0.001,0.0005}; - float* arr_mapx = new float[img_size.width*img_size.height]; - float* arr_mapy = new float[img_size.width*img_size.height]; + std::vector arr_mapx(img_size.width*img_size.height); + std::vector arr_mapy(img_size.width*img_size.height); double arr_new_camera_mat[9] = {155.f, 0.f, img_size.width/2.f+img_size.width/50.f, 0, 310.f, img_size.height/2.f+img_size.height/50.f, 0.f, 0.f, 1.f}; double r[9] = {1,0,0,0,1,0,0,0,1}; @@ -351,25 +169,10 @@ void CV_InitUndistortRectifyMapBadArgTest::run(int) CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist); CvMat _new_camera_mat_orig = cvMat(3,3,CV_64F,arr_new_camera_mat); CvMat _R_orig = cvMat(3,3,CV_64F,r); - CvMat _mapx_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_mapx); - CvMat _mapy_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_mapy); + CvMat _mapx_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_mapx[0]); + CvMat _mapy_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_mapy[0]); int mat_type_orig = CV_32FC1; - _camera_mat = &_camera_mat_orig; - _distortion_coeffs = &_distortion_coeffs_orig; - _new_camera_mat = &_new_camera_mat_orig; - matR = &_R_orig; - _mapx = &_mapx_orig; - _mapy = &_mapy_orig; - mat_type = mat_type_orig; - -//tests - useCPlus = true; - CvMat* temp; - - //C++ tests - useCPlus = true; - camera_mat = cv::cvarrToMat(&_camera_mat_orig); distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig); new_camera_mat = cv::cvarrToMat(&_new_camera_mat_orig); @@ -377,32 +180,23 @@ void CV_InitUndistortRectifyMapBadArgTest::run(int) mapx = cv::cvarrToMat(&_mapx_orig); mapy = cv::cvarrToMat(&_mapy_orig); - mat_type = CV_64F; errcount += run_test_case( CV_StsAssert, "Invalid map matrix type" ); mat_type = mat_type_orig; - temp = cvCreateMat(3,2,CV_32FC1); - camera_mat = cv::cvarrToMat(temp); + camera_mat.create(3, 2, CV_32F); errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" ); camera_mat = cv::cvarrToMat(&_camera_mat_orig); - cvReleaseMat(&temp); - temp = cvCreateMat(4,3,CV_32FC1); - R = cv::cvarrToMat(temp); + R.create(4, 3, CV_32F); errcount += run_test_case( CV_StsAssert, "Invalid R data matrix size" ); R = cv::cvarrToMat(&_R_orig); - cvReleaseMat(&temp); - temp = cvCreateMat(6,1,CV_32FC1); - distortion_coeffs = cv::cvarrToMat(temp); + distortion_coeffs.create(6, 1, CV_32F); errcount += run_test_case( CV_StsAssert, "Invalid distortion coefficients data matrix size" ); distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig); - cvReleaseMat(&temp); //------------ - delete[] arr_mapx; - delete[] arr_mapy; ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK); } @@ -419,17 +213,7 @@ protected: private: //common cv::Size img_size; - bool useCPlus; - //C - CvMat* _camera_mat; - CvMat* _new_camera_mat; - CvMat* _distortion_coeffs; - CvMat* _src; - CvMat* _dst; - - - //C++ cv::Mat camera_mat; cv::Mat new_camera_mat; cv::Mat distortion_coeffs; @@ -440,20 +224,11 @@ private: CV_UndistortBadArgTest::CV_UndistortBadArgTest () { - useCPlus = false; - _camera_mat = _new_camera_mat = _distortion_coeffs = _src = _dst = NULL; } void CV_UndistortBadArgTest::run_func() { - if (useCPlus) - { - cv::undistort(src,dst,camera_mat,distortion_coeffs,new_camera_mat); - } - else - { - cvUndistort2(_src,_dst,_camera_mat,_distortion_coeffs,_new_camera_mat); - } + cv::undistort(src,dst,camera_mat,distortion_coeffs,new_camera_mat); } void CV_UndistortBadArgTest::run(int) @@ -464,52 +239,15 @@ void CV_UndistortBadArgTest::run(int) img_size.height = 600; double cam[9] = {150.f, 0.f, img_size.width/2.f, 0, 300.f, img_size.height/2.f, 0.f, 0.f, 1.f}; double dist[4] = {0.01,0.02,0.001,0.0005}; - float* arr_src = new float[img_size.width*img_size.height]; - float* arr_dst = new float[img_size.width*img_size.height]; + std::vector arr_src(img_size.width*img_size.height); + std::vector arr_dst(img_size.width*img_size.height); double arr_new_camera_mat[9] = {155.f, 0.f, img_size.width/2.f+img_size.width/50.f, 0, 310.f, img_size.height/2.f+img_size.height/50.f, 0.f, 0.f, 1.f}; CvMat _camera_mat_orig = cvMat(3,3,CV_64F,cam); CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist); CvMat _new_camera_mat_orig = cvMat(3,3,CV_64F,arr_new_camera_mat); - CvMat _src_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_src); - CvMat _dst_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_dst); - - _camera_mat = &_camera_mat_orig; - _distortion_coeffs = &_distortion_coeffs_orig; - _new_camera_mat = &_new_camera_mat_orig; - _src = &_src_orig; - _dst = &_dst_orig; - -//tests - useCPlus = true; - CvMat* temp; - CvMat* temp1; - -//C tests - useCPlus = false; - - temp = cvCreateMat(800,600,CV_32F); - temp1 = cvCreateMat(800,601,CV_32F); - _src = temp; - _dst = temp1; - errcount += run_test_case( CV_StsAssert, "Input and output data matrix sizes mismatch" ); - _src = &_src_orig; - _dst = &_dst_orig; - cvReleaseMat(&temp); - cvReleaseMat(&temp1); - - temp = cvCreateMat(800,600,CV_32F); - temp1 = cvCreateMat(800,600,CV_64F); - _src = temp; - _dst = temp1; - errcount += run_test_case( CV_StsAssert, "Input and output data matrix types mismatch" ); - _src = &_src_orig; - _dst = &_dst_orig; - cvReleaseMat(&temp); - cvReleaseMat(&temp1); - - //C++ tests - useCPlus = true; + CvMat _src_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_src[0]); + CvMat _dst_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_dst[0]); camera_mat = cv::cvarrToMat(&_camera_mat_orig); distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig); @@ -517,9 +255,10 @@ void CV_UndistortBadArgTest::run(int) src = cv::cvarrToMat(&_src_orig); dst = cv::cvarrToMat(&_dst_orig); + camera_mat.create(5, 5, CV_64F); + errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" ); + //------------ - delete[] arr_src; - delete[] arr_dst; ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK); } diff --git a/modules/imgproc/test/test_imgwarp.cpp b/modules/imgproc/test/test_imgwarp.cpp index 7f6caa4693..3660da3d84 100644 --- a/modules/imgproc/test/test_imgwarp.cpp +++ b/modules/imgproc/test/test_imgwarp.cpp @@ -796,7 +796,7 @@ int CV_RemapTest::prepare_test_case( int test_case_idx ) k[2] = cvtest::randReal(rng)*0.004 - 0.002; k[3] = cvtest::randReal(rng)*0.004 - 0.002; - cvtest::initUndistortMap( _a, _k, test_mat[INPUT][1].size(), test_mat[INPUT][1], test_mat[INPUT][2] ); + cvtest::initUndistortMap( _a, _k, Mat(), Mat(), test_mat[INPUT][1].size(), test_mat[INPUT][1], test_mat[INPUT][2], CV_32F ); return code; } diff --git a/modules/stitching/src/motion_estimators.cpp b/modules/stitching/src/motion_estimators.cpp index 6a07842444..d9848dbe7f 100644 --- a/modules/stitching/src/motion_estimators.cpp +++ b/modules/stitching/src/motion_estimators.cpp @@ -41,6 +41,7 @@ //M*/ #include "precomp.hpp" +#include "opencv2/core/core_c.h" #include "opencv2/calib3d/calib3d_c.h" #include "opencv2/core/cvdef.h" diff --git a/modules/ts/include/opencv2/ts.hpp b/modules/ts/include/opencv2/ts.hpp index 1fa7f28d29..df77447c83 100644 --- a/modules/ts/include/opencv2/ts.hpp +++ b/modules/ts/include/opencv2/ts.hpp @@ -220,7 +220,7 @@ void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int Mat calcSobelKernel2D( int dx, int dy, int apertureSize, int origin=0 ); Mat calcLaplaceKernel2D( int aperture_size ); -void initUndistortMap( const Mat& a, const Mat& k, Size sz, Mat& mapx, Mat& mapy ); +void initUndistortMap( const Mat& a, const Mat& k, const Mat& R, const Mat& new_a, Size sz, Mat& mapx, Mat& mapy, int map_type ); void minMaxLoc(const Mat& src, double* minval, double* maxval, vector* minloc, vector* maxloc, const Mat& mask=Mat()); @@ -594,7 +594,7 @@ protected: catch(const cv::Exception& e) { thrown = true; - if( e.code != expected_code ) + if( e.code != expected_code && e.code != cv::Error::StsAssert && e.code != cv::Error::StsError ) { ts->printf(TS::LOG, "%s (test case #%d): the error code %d is different from the expected %d\n", descr, test_case_idx, e.code, expected_code); diff --git a/modules/ts/src/ts_func.cpp b/modules/ts/src/ts_func.cpp index c404759fd4..a837341ebf 100644 --- a/modules/ts/src/ts_func.cpp +++ b/modules/ts/src/ts_func.cpp @@ -2810,29 +2810,57 @@ Mat calcLaplaceKernel2D( int aperture_size ) } -void initUndistortMap( const Mat& _a0, const Mat& _k0, Size sz, Mat& _mapx, Mat& _mapy ) +void initUndistortMap( const Mat& _a0, const Mat& _k0, const Mat& _R0, const Mat& _new_cam0, Size sz, Mat& __mapx, Mat& __mapy, int map_type ) { - _mapx.create(sz, CV_32F); - _mapy.create(sz, CV_32F); + Mat _mapx(sz, CV_32F), _mapy(sz, CV_32F); - double a[9], k[5]={0,0,0,0,0}; - Mat _a(3, 3, CV_64F, a); + double a[9], k[5]={0,0,0,0,0}, iR[9]={1, 0, 0, 0, 1, 0, 0, 0, 1}, a1[9]; + Mat _a(3, 3, CV_64F, a), _a1(3, 3, CV_64F, a1); Mat _k(_k0.rows,_k0.cols, CV_MAKETYPE(CV_64F,_k0.channels()),k); + Mat _iR(3, 3, CV_64F, iR); double fx, fy, cx, cy, ifx, ify, cxn, cyn; + CV_Assert(_k0.empty() || + _k0.size() == Size(5, 1) || + _k0.size() == Size(1, 5) || + _k0.size() == Size(4, 1) || + _k0.size() == Size(1, 4)); + CV_Assert(_a0.size() == Size(3, 3)); + _a0.convertTo(_a, CV_64F); - _k0.convertTo(_k, CV_64F); + if( !_k0.empty() ) + _k0.convertTo(_k, CV_64F); + if( !_R0.empty() ) + { + CV_Assert(_R0.size() == Size(3, 3)); + Mat tmp; + _R0.convertTo(tmp, CV_64F); + invert(tmp, _iR, DECOMP_LU); + } + if( !_new_cam0.empty() ) + { + CV_Assert(_new_cam0.size() == Size(3, 3)); + _new_cam0.convertTo(_a1, CV_64F); + } + else + _a.copyTo(_a1); + fx = a[0]; fy = a[4]; cx = a[2]; cy = a[5]; - ifx = 1./fx; ify = 1./fy; - cxn = cx; - cyn = cy; + ifx = 1./a1[0]; ify = 1./a1[4]; + cxn = a1[2]; + cyn = a1[5]; for( int v = 0; v < sz.height; v++ ) { for( int u = 0; u < sz.width; u++ ) { - double x = (u - cxn)*ifx; - double y = (v - cyn)*ify; + double x_ = (u - cxn)*ifx; + double y_ = (v - cyn)*ify; + double X = iR[0]*x_ + iR[1]*y_ + iR[2]; + double Y = iR[3]*x_ + iR[4]*y_ + iR[5]; + double Z = iR[6]*x_ + iR[7]*y_ + iR[8]; + double x = X/Z; + double y = Y/Z; double x2 = x*x, y2 = y*y; double r2 = x2 + y2; double cdist = 1 + (k[0] + (k[1] + k[4]*r2)*r2)*r2; @@ -2843,8 +2871,10 @@ void initUndistortMap( const Mat& _a0, const Mat& _k0, Size sz, Mat& _mapx, Mat& _mapx.at(v, u) = (float)(x1*fx + cx); } } -} + _mapx.convertTo(__mapx, map_type); + _mapy.convertTo(__mapy, map_type); +} std::ostream& operator << (std::ostream& out, const MatInfo& m) {