1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
48 #pragma warning( disable: 4701 )
51 CvCalibFilter::CvCalibFilter()
54 etalonType = CV_CALIB_ETALON_USER;
63 memset( points, 0, sizeof(points));
64 memset( undistMap, 0, sizeof(undistMap));
66 memset( latestCounts, 0, sizeof(latestCounts));
67 memset( latestPoints, 0, sizeof(latestPoints));
68 memset( &stereo, 0, sizeof(stereo) );
74 imgSize = cvSize(0,0);
79 memset( rectMap, 0, sizeof(rectMap));
83 CvCalibFilter::~CvCalibFilter()
86 cvFree( &etalonParams );
87 cvFree( &etalonPoints );
88 cvReleaseMat( &grayImg );
89 cvReleaseMat( &tempImg );
90 cvReleaseMat( &undistImg );
91 cvReleaseMemStorage( &storage );
95 bool CvCalibFilter::SetEtalon( CvCalibEtalonType type, double* params,
96 int pointCount, CvPoint2D32f* points )
102 if (latestPoints != NULL)
104 for( i = 0; i < MAX_CAMERAS; i++ )
105 cvFree( latestPoints + i );
108 if( type == CV_CALIB_ETALON_USER || type != etalonType )
110 if (etalonParams != NULL)
112 cvFree( &etalonParams );
120 case CV_CALIB_ETALON_CHESSBOARD:
121 etalonParamCount = 3;
122 if( !params || cvRound(params[0]) != params[0] || params[0] < 3 ||
123 cvRound(params[1]) != params[1] || params[1] < 3 || params[2] <= 0 )
129 pointCount = cvRound((params[0] - 1)*(params[1] - 1));
132 case CV_CALIB_ETALON_USER:
133 etalonParamCount = 0;
135 if( !points || pointCount < 4 )
147 if( etalonParamCount > 0 )
149 arrSize = etalonParamCount * sizeof(etalonParams[0]);
150 etalonParams = (double*)cvAlloc( arrSize );
153 arrSize = pointCount * sizeof(etalonPoints[0]);
155 if( etalonPointCount != pointCount )
157 if (etalonPoints != NULL)
159 cvFree( &etalonPoints );
161 etalonPointCount = pointCount;
162 etalonPoints = (CvPoint2D32f*)cvAlloc( arrSize );
167 case CV_CALIB_ETALON_CHESSBOARD:
169 int etalonWidth = cvRound( params[0] ) - 1;
170 int etalonHeight = cvRound( params[1] ) - 1;
173 etalonParams[0] = etalonWidth;
174 etalonParams[1] = etalonHeight;
175 etalonParams[2] = params[2];
177 for( y = 0; y < etalonHeight; y++ )
178 for( x = 0; x < etalonWidth; x++ )
180 etalonPoints[k++] = cvPoint2D32f( (etalonWidth - 1 - x)*params[2],
186 case CV_CALIB_ETALON_USER:
189 memcpy( etalonParams, params, arrSize );
193 memcpy( etalonPoints, points, arrSize );
207 CvCalibFilter::GetEtalon( int* paramCount, const double** params,
208 int* pointCount, const CvPoint2D32f** points ) const
211 *paramCount = etalonParamCount;
214 *params = etalonParams;
217 *pointCount = etalonPointCount;
220 *points = etalonPoints;
226 void CvCalibFilter::SetCameraCount( int count )
230 if( count != cameraCount )
232 for( int i = 0; i < cameraCount; i++ )
234 cvFree( points + i );
235 cvFree( latestPoints + i );
236 cvReleaseMat( &undistMap[i][0] );
237 cvReleaseMat( &undistMap[i][1] );
238 cvReleaseMat( &rectMap[i][0] );
239 cvReleaseMat( &rectMap[i][1] );
242 memset( latestCounts, 0, sizeof(latestPoints) );
249 bool CvCalibFilter::SetFrames( int frames )
257 framesTotal = frames;
262 void CvCalibFilter::Stop( bool calibrate )
265 isCalibrated = false;
267 // deallocate undistortion maps
268 for( i = 0; i < cameraCount; i++ )
270 cvReleaseMat( &undistMap[i][0] );
271 cvReleaseMat( &undistMap[i][1] );
272 cvReleaseMat( &rectMap[i][0] );
273 cvReleaseMat( &rectMap[i][1] );
276 if( calibrate && framesAccepted > 0 )
278 int n = framesAccepted;
279 CvPoint3D32f* buffer =
280 (CvPoint3D32f*)cvAlloc( n * etalonPointCount * sizeof(buffer[0]));
282 float* rotMatr = (float*)cvAlloc( n * 9 * sizeof(rotMatr[0]));
283 float* transVect = (float*)cvAlloc( n * 3 * sizeof(transVect[0]));
284 int* counts = (int*)cvAlloc( n * sizeof(counts[0]));
286 cvInitMatHeader( &mat, 1, sizeof(CvCamera)/sizeof(float), CV_32FC1, 0 );
287 memset( cameraParams, 0, cameraCount * sizeof(cameraParams[0]));
289 for( i = 0; i < framesAccepted; i++ )
291 counts[i] = etalonPointCount;
292 for( j = 0; j < etalonPointCount; j++ )
293 buffer[i * etalonPointCount + j] = cvPoint3D32f( etalonPoints[j].x,
294 etalonPoints[j].y, 0 );
297 for( i = 0; i < cameraCount; i++ )
299 cvCalibrateCamera( framesAccepted, counts,
300 imgSize, points[i], buffer,
301 cameraParams[i].distortion,
302 cameraParams[i].matrix,
303 transVect, rotMatr, 0 );
305 cameraParams[i].imgSize[0] = (float)imgSize.width;
306 cameraParams[i].imgSize[1] = (float)imgSize.height;
308 // cameraParams[i].focalLength[0] = cameraParams[i].matrix[0];
309 // cameraParams[i].focalLength[1] = cameraParams[i].matrix[4];
311 // cameraParams[i].principalPoint[0] = cameraParams[i].matrix[2];
312 // cameraParams[i].principalPoint[1] = cameraParams[i].matrix[5];
314 memcpy( cameraParams[i].rotMatr, rotMatr, 9 * sizeof(rotMatr[0]));
315 memcpy( cameraParams[i].transVect, transVect, 3 * sizeof(transVect[0]));
317 mat.data.ptr = (uchar*)(cameraParams + i);
319 /* check resultant camera parameters: if there are some INF's or NAN's,
320 stop and reset results */
321 if( !cvCheckArr( &mat, CV_CHECK_RANGE | CV_CHECK_QUIET, -10000, 10000 ))
327 isCalibrated = i == cameraCount;
329 {/* calibrate stereo cameras */
330 if( cameraCount == 2 )
332 stereo.camera[0] = &cameraParams[0];
333 stereo.camera[1] = &cameraParams[1];
335 icvStereoCalibration( framesAccepted, counts,
341 for( i = 0; i < 9; i++ )
343 stereo.fundMatr[i] = stereo.fundMatr[i];
353 cvFree( &transVect );
360 bool CvCalibFilter::FindEtalon( IplImage** imgs )
362 return FindEtalon( (CvMat**)imgs );
366 bool CvCalibFilter::FindEtalon( CvMat** mats )
370 if( !mats || etalonPointCount == 0 )
378 int i, tempPointCount0 = etalonPointCount*2;
380 for( i = 0; i < cameraCount; i++ )
382 if( !latestPoints[i] )
383 latestPoints[i] = (CvPoint2D32f*)
384 cvAlloc( tempPointCount0*2*sizeof(latestPoints[0]));
387 for( i = 0; i < cameraCount; i++ )
390 int tempPointCount = tempPointCount0;
393 if( !CV_IS_MAT(mats[i]) && !CV_IS_IMAGE(mats[i]))
399 size = cvGetSize(mats[i]);
401 if( size.width != imgSize.width || size.height != imgSize.height )
406 if( !grayImg || grayImg->width != imgSize.width ||
407 grayImg->height != imgSize.height )
409 cvReleaseMat( &grayImg );
410 cvReleaseMat( &tempImg );
411 grayImg = cvCreateMat( imgSize.height, imgSize.width, CV_8UC1 );
412 tempImg = cvCreateMat( imgSize.height, imgSize.width, CV_8UC1 );
416 storage = cvCreateMemStorage();
420 case CV_CALIB_ETALON_CHESSBOARD:
421 if( CV_MAT_CN(cvGetElemType(mats[i])) == 1 )
422 cvCopy( mats[i], grayImg );
424 cvCvtColor( mats[i], grayImg, CV_BGR2GRAY );
425 found = cvFindChessBoardCornerGuesses( grayImg, tempImg, storage,
426 cvSize( cvRound(etalonParams[0]),
427 cvRound(etalonParams[1])),
428 latestPoints[i], &tempPointCount ) != 0;
430 cvFindCornerSubPix( grayImg, latestPoints[i], tempPointCount,
431 cvSize(5,5), cvSize(-1,-1),
432 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,10,0.1));
440 latestCounts[i] = found ? tempPointCount : -tempPointCount;
441 result = result && found;
446 cvClearMemStorage( storage );
452 bool CvCalibFilter::Push( const CvPoint2D32f** pts )
455 int i, newMaxPoints = etalonPointCount*(MAX(framesAccepted,framesTotal) + 1);
457 isCalibrated = false;
461 for( i = 0; i < cameraCount; i++ )
462 if( latestCounts[i] <= 0 )
464 pts = (const CvPoint2D32f**)latestPoints;
467 for( i = 0; i < cameraCount; i++ )
475 if( maxPoints < newMaxPoints )
477 CvPoint2D32f* prev = points[i];
478 cvFree( points + i );
479 points[i] = (CvPoint2D32f*)cvAlloc( newMaxPoints * sizeof(prev[0]));
480 memcpy( points[i], prev, maxPoints * sizeof(prev[0]));
483 memcpy( points[i] + framesAccepted*etalonPointCount, pts[i],
484 etalonPointCount*sizeof(points[0][0]));
487 if( maxPoints < newMaxPoints )
488 maxPoints = newMaxPoints;
490 result = i == cameraCount;
492 if( ++framesAccepted >= framesTotal )
498 bool CvCalibFilter::GetLatestPoints( int idx, CvPoint2D32f** pts,
499 int* count, bool* found )
503 if( (unsigned)idx >= (unsigned)cameraCount ||
504 !pts || !count || !found )
510 n = latestCounts[idx];
514 *pts = latestPoints[idx];
520 void CvCalibFilter::DrawPoints( IplImage** dst )
522 DrawPoints( (CvMat**)dst );
526 void CvCalibFilter::DrawPoints( CvMat** dstarr )
538 for( i = 0; i < cameraCount; i++ )
540 if( dstarr[i] && latestCounts[i] )
542 CvMat dst_stub, *dst;
545 CvPoint2D32f* pts = 0;
547 GetLatestPoints( i, &pts, &count, &found );
549 dst = cvGetMat( dstarr[i], &dst_stub );
551 static const CvScalar line_colors[] =
562 const int colorCount = sizeof(line_colors)/sizeof(line_colors[0]);
564 CvScalar color = line_colors[0];
565 CvPoint prev_pt = { 0, 0};
567 for( j = 0; j < count; j++ )
570 pt.x = cvRound(pts[j].x);
571 pt.y = cvRound(pts[j].y);
575 if( etalonType == CV_CALIB_ETALON_CHESSBOARD )
576 color = line_colors[(j/cvRound(etalonParams[0]))%colorCount];
578 color = CV_RGB(0,255,0);
581 cvLine( dst, prev_pt, pt, color, 1, CV_AA );
584 cvLine( dst, cvPoint( pt.x - r, pt.y - r ),
585 cvPoint( pt.x + r, pt.y + r ), color, 1, CV_AA );
587 cvLine( dst, cvPoint( pt.x - r, pt.y + r),
588 cvPoint( pt.x + r, pt.y - r), color, 1, CV_AA );
590 cvCircle( dst, pt, r+1, color, 1, CV_AA );
600 /* Get total number of frames and already accepted pair of frames */
601 int CvCalibFilter::GetFrameCount( int* total ) const
604 *total = framesTotal;
606 return framesAccepted;
610 /* Get camera parameters for specified camera. If camera is not calibrated
611 the function returns 0 */
612 const CvCamera* CvCalibFilter::GetCameraParams( int idx ) const
614 if( (unsigned)idx >= (unsigned)cameraCount )
620 return isCalibrated ? cameraParams + idx : 0;
624 /* Get camera parameters for specified camera. If camera is not calibrated
625 the function returns 0 */
626 const CvStereoCamera* CvCalibFilter::GetStereoParams() const
628 if( !(isCalibrated && cameraCount == 2) )
638 /* Sets camera parameters for all cameras */
639 bool CvCalibFilter::SetCameraParams( CvCamera* params )
652 arrSize = cameraCount * sizeof(params[0]);
654 cvInitMatHeader( &mat, 1, cameraCount * (arrSize/sizeof(float)),
656 cvCheckArr( &mat, CV_CHECK_RANGE, -10000, 10000 );
658 memcpy( cameraParams, params, arrSize );
665 bool CvCalibFilter::SaveCameraParams( const char* filename )
671 FILE* f = fopen( filename, "w" );
673 if( !f ) return false;
675 fprintf( f, "%d\n\n", cameraCount );
677 for( i = 0; i < cameraCount; i++ )
679 for( j = 0; j < (int)(sizeof(cameraParams[i])/sizeof(float)); j++ )
681 fprintf( f, "%15.10f ", ((float*)(cameraParams + i))[j] );
683 fprintf( f, "\n\n" );
686 /* Save stereo params */
689 for( i = 0; i < 2; i++ )
691 for( j = 0; j < 4; j++ )
693 fprintf(f, "%15.10f ", stereo.quad[i][j].x );
694 fprintf(f, "%15.10f ", stereo.quad[i][j].y );
700 for( i = 0; i < 2; i++ )
702 for( j = 0; j < 9; j++ )
704 fprintf(f, "%15.10lf ", stereo.coeffs[i][j/3][j%3] );
718 bool CvCalibFilter::LoadCameraParams( const char* filename )
722 FILE* f = fopen( filename, "r" );
724 isCalibrated = false;
726 if( !f ) return false;
728 if( fscanf( f, "%d", &d ) != 1 || d <= 0 || d > 10 )
733 for( i = 0; i < cameraCount; i++ )
735 for( j = 0; j < (int)(sizeof(cameraParams[i])/sizeof(float)); j++ )
737 fscanf( f, "%f", &((float*)(cameraParams + i))[j] );
742 /* Load stereo params */
745 for( i = 0; i < 2; i++ )
747 for( j = 0; j < 4; j++ )
749 fscanf(f, "%f ", &(stereo.quad[i][j].x) );
750 fscanf(f, "%f ", &(stereo.quad[i][j].y) );
755 for( i = 0; i < 2; i++ )
757 for( j = 0; j < 9; j++ )
759 fscanf(f, "%lf ", &(stereo.coeffs[i][j/3][j%3]) );
768 stereo.warpSize = cvSize( cvRound(cameraParams[0].imgSize[0]), cvRound(cameraParams[0].imgSize[1]));
776 bool CvCalibFilter::Rectify( IplImage** srcarr, IplImage** dstarr )
778 return Rectify( (CvMat**)srcarr, (CvMat**)dstarr );
781 bool CvCalibFilter::Rectify( CvMat** srcarr, CvMat** dstarr )
785 if( !srcarr || !dstarr )
791 if( isCalibrated && cameraCount == 2 )
793 for( i = 0; i < cameraCount; i++ )
795 if( srcarr[i] && dstarr[i] )
797 IplImage src_stub, *src;
798 IplImage dst_stub, *dst;
800 src = cvGetImage( srcarr[i], &src_stub );
801 dst = cvGetImage( dstarr[i], &dst_stub );
803 if( src->imageData == dst->imageData )
806 undistImg->width != src->width ||
807 undistImg->height != src->height ||
808 CV_MAT_CN(undistImg->type) != src->nChannels )
810 cvReleaseMat( &undistImg );
811 undistImg = cvCreateMat( src->height, src->width,
812 CV_8U + (src->nChannels-1)*8 );
814 cvCopy( src, undistImg );
815 src = cvGetImage( undistImg, &src_stub );
820 if( !rectMap[i][0] || rectMap[i][0]->width != src->width ||
821 rectMap[i][0]->height != src->height )
823 cvReleaseMat( &rectMap[i][0] );
824 cvReleaseMat( &rectMap[i][1] );
825 rectMap[i][0] = cvCreateMat(stereo.warpSize.height,stereo.warpSize.width,CV_32FC1);
826 rectMap[i][1] = cvCreateMat(stereo.warpSize.height,stereo.warpSize.width,CV_32FC1);
827 cvComputePerspectiveMap(stereo.coeffs[i], rectMap[i][0], rectMap[i][1]);
829 cvRemap( src, dst, rectMap[i][0], rectMap[i][1] );
835 for( i = 0; i < cameraCount; i++ )
837 if( srcarr[i] != dstarr[i] )
838 cvCopy( srcarr[i], dstarr[i] );
845 bool CvCalibFilter::Undistort( IplImage** srcarr, IplImage** dstarr )
847 return Undistort( (CvMat**)srcarr, (CvMat**)dstarr );
851 bool CvCalibFilter::Undistort( CvMat** srcarr, CvMat** dstarr )
855 if( !srcarr || !dstarr )
863 for( i = 0; i < cameraCount; i++ )
865 if( srcarr[i] && dstarr[i] )
867 CvMat src_stub, *src;
868 CvMat dst_stub, *dst;
870 src = cvGetMat( srcarr[i], &src_stub );
871 dst = cvGetMat( dstarr[i], &dst_stub );
873 if( src->data.ptr == dst->data.ptr )
875 if( !undistImg || undistImg->width != src->width ||
876 undistImg->height != src->height ||
877 CV_ARE_TYPES_EQ( undistImg, src ))
879 cvReleaseMat( &undistImg );
880 undistImg = cvCreateMat( src->height, src->width, src->type );
883 cvCopy( src, undistImg );
889 CvMat A = cvMat( 3, 3, CV_32FC1, cameraParams[i].matrix );
890 CvMat k = cvMat( 1, 4, CV_32FC1, cameraParams[i].distortion );
892 if( !undistMap[i][0] || undistMap[i][0]->width != src->width ||
893 undistMap[i][0]->height != src->height )
895 cvReleaseMat( &undistMap[i][0] );
896 cvReleaseMat( &undistMap[i][1] );
897 undistMap[i][0] = cvCreateMat( src->height, src->width, CV_32FC1 );
898 undistMap[i][1] = cvCreateMat( src->height, src->width, CV_32FC1 );
899 cvInitUndistortMap( &A, &k, undistMap[i][0], undistMap[i][1] );
902 cvRemap( src, dst, undistMap[i][0], undistMap[i][1] );
904 cvUndistort2( src, dst, &A, &k );
912 for( i = 0; i < cameraCount; i++ )
914 if( srcarr[i] != dstarr[i] )
915 cvCopy( srcarr[i], dstarr[i] );