Re: Camera Calibration Example 11-1. Code from the book.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Camera Calibration Example 11-1. Code from the book.

Ed Elston
Martin, I see that it is using it during calibration and also that
it is recorded into the xml file, but do you believe that this
additional coefficient is used during the undistortion process?  I
don't know either way.

Ed

--- In [hidden email], "mdale9_opencv" <martin.dale.1@...>
wrote:
>
> There is no mistake,  the latest version of openCV 1.1pre1a works
with
> either 4 or 5 distortion coefficients, the inclusion of the 5th
> coefficient is a new addition and it is the 3rd order radial
> distortion, it has just been stuck at the end for backwards
compatibility.
>
> Martin.
> --- In [hidden email], "jcz" <jakub.czaplicki@> wrote:
> >
> > Just wanted to share the code I rewritten from the O'Reillys
OpenCV

> > book. Here's the code for example 11-1 (pages 398-401), slightly
> > modified, as I am not using arguments as inputs - just hard coded
> > chessboard size and number of views.
> >
> > I also found one mistage in line:
> >    CvMat* distortion_coeffs = cvCreateMat(5,1,CV_32FC1);
> >
> > it should be:
> >    CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);
> >
> > otherwise cvCalibrateCamera2(...) reports error - wrong
distortion
> > matrix size.
> >
> > To test the code and calibrate my camera I used checkerboard.gif,
> > which you can find at
http://tech.groups.yahoo.com/group/OpenCV/files/ 

> >
> > The code:
> >
> > //Example 11-1. Camera Calibration. Sligthly modified (no input
> > arguments)
> > //
> > // Hit 'p' to pause/unpause, ESC to quit
> > //
> >
> > #include "stdafx.h"
> > #include "cv.h"
> > #include "highgui.h"
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > using namespace std;
> > #using <mscorlib.dll>
> >
> > // Chessboard calibration
> > int n_boards = 0;
> > const int board_dt = 20; //wait20 frame per chessboard view
> > int board_w, board_h;
> >
> > int main(int argc, char* argv[])
> > {
> > board_w = 6;
> > board_h = 4;
> > n_boards = 10; // number of views
> > int board_n = board_w * board_h;
> > CvSize board_sz = cvSize( board_w, board_h);
> > CvCapture* capture = cvCreateCameraCapture( 0 );
> > assert(capture);
> >
> > cvNamedWindow("Calibration");
> > //Allocate storage
> > CvMat* image_points = cvCreateMat
(n_boards*board_n,2,CV_32FC1);
> > CvMat* object_points = cvCreateMat
(n_boards*board_n,3,CV_32FC1);
> > CvMat* point_counts = cvCreateMat
(n_boards,1,CV_32SC1);
> > CvMat* intrinsic_matrix = cvCreateMat(3,3,CV_32FC1);
> > CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1); //
mistake in
> > book (5,1,CV_32FC1) ?
> >
> > CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
> > int corner_count;
> > int successes = 0;
> > int step, frame = 0;
> > IplImage *image = cvQueryFrame( capture );
> > IplImage *gray_image = cvCreateImage(cvGetSize
(image),8,1); //subpixel
> >
> > //Capture corner views loop until we've got n_boards
> > //successful captures (all corners on the board are found)
> > while(successes < n_boards )
> > {
> > //skip every board_dt frames to allow user to move
chessboard
> > if (frame++ % board_dt == 0)
> > {
> > //find chessboard corners
> > int found = cvFindChessboardCorners(
> > image,board_sz,corners,
&corner_count,
> > CV_CALIB_CB_ADAPTIVE_THRESH
| CV_CALIB_CB_FILTER_QUADS );
> > //Get Subpixel accuracy on those corners
> > cvCvtColor(image, gray_image, CV_BGR2GRAY);
> > cvFindCornerSubPix(gray_image, corners,
corner_count,
> > cvSize(11,11), cvSize(-1,-1),
> > cvTermCriteria
(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1) );
> > //DRAW IT
> > cvDrawChessboardCorners(image, board_sz,
corners, corner_count,
> found);
> > cvShowImage( "Calibration", image);
> >
> > //if we got a good board, add it to our data
> > if( corner_count == board_n )
> > {
> > step = successes*board_n;
> > for( int i=step, j=0; j<board_n;
++i,++j )
> > {
> > CV_MAT_ELEM(*image_points,
float, i, 0) = corners[j].x;
> > CV_MAT_ELEM(*image_points,
float, i, 1) = corners[j].y;
> > CV_MAT_ELEM
(*object_points,float, i, 0) = j/board_w;
> > CV_MAT_ELEM
(*object_points,float, i, 1) = j%board_w;
> > CV_MAT_ELEM
(*object_points,float, i, 2) = 0.0f;
> > }
> > CV_MAT_ELEM(*point_counts, int,
successes, 0) = board_n;

> > successes++;
> > }
> > } //end skip board_dt beteween chessboard capture
> >
> > //Handle pause/unpause and ESC
> > int c = cvWaitKey(15);
> > if(c=='p')
> > {
> > c = 0;
> > while (c != 'p'&& c != 27)
> > {
> > c = cvWaitKey(250);
> > }
> > }
> > if (c==27)
> > return 0;
> >
> > image = cvQueryFrame( capture ); // get next image
> > }//end collection while loop
> >
> > //Allocate matrices according to how many chessboards found
> > CvMat* object_points2 = cvCreateMat
(successes*board_n,3,CV_32FC1);
> > CvMat* image_points2 = cvCreateMat
(successes*board_n,2,CV_32FC1);
> > CvMat* point_counts2 = cvCreateMat(successes,1,CV_32SC1);
> > //Transfer the points into the correct size matrices
> > // Below we write out the details in the next two loops. We
could
> > instead have written:
> > //image_points->rows = object_points->rows =
successes*board_n;

> > //point_counts->rows = successes;
> > for (int i=0; i<successes*board_n; ++i)
> > {
> > CV_MAT_ELEM( *image_points2, float, i, 0) =
> > CV_MAT_ELEM( *image_points, float, i, 0);
> > CV_MAT_ELEM( *image_points2, float, i, 1) =
> > CV_MAT_ELEM( *image_points, float, i, 1);
> > CV_MAT_ELEM( *object_points2, float, i, 0) =
> > CV_MAT_ELEM( *object_points, float, i, 0);
> > CV_MAT_ELEM( *object_points2, float, i, 1) =
> > CV_MAT_ELEM( *object_points, float, i, 1);
> > CV_MAT_ELEM( *object_points2, float, i, 2) =
> > CV_MAT_ELEM( *object_points, float, i, 2);
> > }
> > for (int i=0; i<successes; ++i) //these are all the same
number
> > {
> > CV_MAT_ELEM( *point_counts2, int, i, 0) =
> > CV_MAT_ELEM( *point_counts, int, i, 0);
> > }
> > cvReleaseMat(&object_points);
> > cvReleaseMat(&image_points);
> > cvReleaseMat(&point_counts);
> >
> > // At this point we have all of the chessboard corners we
need.
> > // Initialize the intrinsic matrix such that the two focal
> > // lengths have ration of 1.0
> > CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0) = 1.0f;
> > CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1) = 1.0f;
> >
> > //Calibrate the camera !
> > cvCalibrateCamera2(object_points2, image_points2,
point_counts2,

> > cvGetSize( image ),
> > intrinsic_matrix, distortion_coeffs, NULL, NULL, 0 /*
> > CV_CALIB_FIX_ASPECT_RATIO */ );
> >
> > // Save intrinsics and distortions
> > cvSave("Intrinsics.xml",intrinsic_matrix);
> > cvSave("Distortion.xml",distortion_coeffs);
> >
> > //Example of loading these matrrices back in:
> > CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
> > CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
> >
> > //Build the undistort map that we will use for all
subsequent frames
> > IplImage* mapx = cvCreateImage( cvGetSize(image),
IPL_DEPTH_32F, 1 );
> > IplImage* mapy = cvCreateImage( cvGetSize(image),
IPL_DEPTH_32F, 1 );
> > cvInitUndistortMap( intrinsic, distortion, mapx, mapy );
> >
> > //Just run the camera to the screen, now showing the raw and
the

> > undistorted image
> > cvNamedWindow("Undistort");
> > while(image)
> > {
> > IplImage *t = cvCloneImage(image);
> > cvShowImage( "Calibration", image); //show raw image
> > cvRemap( t, image, mapx, mapy); //undistort image
> > cvReleaseImage(&t);
> > cvShowImage( "Undistort", image);
> >
> > //Handle pause/unpause and ESC
> > int c = cvWaitKey(15);
> > if(c=='p')
> > {
> > c = 0;
> > while (c != 'p'&& c != 27)
> > {
> > c = cvWaitKey(250);
> > }
> > }
> > if (c==27)
> > return 0;
> >
> > image = cvQueryFrame( capture ); // get next image
> > }
> >
> > return 0;
> > }
> >
>


Reply | Threaded
Open this post in threaded view
|

Re: Camera Calibration Example 11-1. Code from the book.

mdale9_opencv
Ed,

Looking through the cvundistort.cpp source file it looks as though the
additional coefficient is used.  The stub checks to see if the
distortion coeff matrix is in one of the correct sizes (1x5, 5x1, 1x4,
4x1) and then within the implementation a 5x1 vector initialized to
zero is instantiated and filled with numbers from the distortion
coefficients, that way if you are only using 4 coeffs then the 5th one
is zero.

Martin.

--- In [hidden email], "Ed Elston" <ed.elston@...> wrote:

>
> Martin, I see that it is using it during calibration and also that
> it is recorded into the xml file, but do you believe that this
> additional coefficient is used during the undistortion process?  I
> don't know either way.
>
> Ed
>
> --- In [hidden email], "mdale9_opencv" <martin.dale.1@>
> wrote:
> >
> > There is no mistake,  the latest version of openCV 1.1pre1a works
> with
> > either 4 or 5 distortion coefficients, the inclusion of the 5th
> > coefficient is a new addition and it is the 3rd order radial
> > distortion, it has just been stuck at the end for backwards
> compatibility.
> >
> > Martin.
> > --- In [hidden email], "jcz" <jakub.czaplicki@> wrote:
> > >
> > > Just wanted to share the code I rewritten from the O'Reillys
> OpenCV
> > > book. Here's the code for example 11-1 (pages 398-401), slightly
> > > modified, as I am not using arguments as inputs - just hard coded
> > > chessboard size and number of views.
> > >
> > > I also found one mistage in line:
> > >    CvMat* distortion_coeffs = cvCreateMat(5,1,CV_32FC1);
> > >
> > > it should be:
> > >    CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);
> > >
> > > otherwise cvCalibrateCamera2(...) reports error - wrong
> distortion
> > > matrix size.
> > >
> > > To test the code and calibrate my camera I used checkerboard.gif,
> > > which you can find at
> http://tech.groups.yahoo.com/group/OpenCV/files/ 
> > >
> > > The code:
> > >
> > > //Example 11-1. Camera Calibration. Sligthly modified (no input
> > > arguments)
> > > //
> > > // Hit 'p' to pause/unpause, ESC to quit
> > > //
> > >
> > > #include "stdafx.h"
> > > #include "cv.h"
> > > #include "highgui.h"
> > > #include <stdio.h>
> > > #include <stdlib.h>
> > >
> > > using namespace std;
> > > #using <mscorlib.dll>
> > >
> > > // Chessboard calibration
> > > int n_boards = 0;
> > > const int board_dt = 20; //wait20 frame per chessboard view
> > > int board_w, board_h;
> > >
> > > int main(int argc, char* argv[])
> > > {
> > > board_w = 6;
> > > board_h = 4;
> > > n_boards = 10; // number of views
> > > int board_n = board_w * board_h;
> > > CvSize board_sz = cvSize( board_w, board_h);
> > > CvCapture* capture = cvCreateCameraCapture( 0 );
> > > assert(capture);
> > >
> > > cvNamedWindow("Calibration");
> > > //Allocate storage
> > > CvMat* image_points = cvCreateMat
> (n_boards*board_n,2,CV_32FC1);
> > > CvMat* object_points = cvCreateMat
> (n_boards*board_n,3,CV_32FC1);
> > > CvMat* point_counts = cvCreateMat
> (n_boards,1,CV_32SC1);
> > > CvMat* intrinsic_matrix = cvCreateMat(3,3,CV_32FC1);
> > > CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1); //
> mistake in
> > > book (5,1,CV_32FC1) ?
> > >
> > > CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
> > > int corner_count;
> > > int successes = 0;
> > > int step, frame = 0;
> > > IplImage *image = cvQueryFrame( capture );
> > > IplImage *gray_image = cvCreateImage(cvGetSize
> (image),8,1); //subpixel
> > >
> > > //Capture corner views loop until we've got n_boards
> > > //successful captures (all corners on the board are found)
> > > while(successes < n_boards )
> > > {
> > > //skip every board_dt frames to allow user to move
> chessboard
> > > if (frame++ % board_dt == 0)
> > > {
> > > //find chessboard corners
> > > int found = cvFindChessboardCorners(
> > > image,board_sz,corners,
> &corner_count,
> > > CV_CALIB_CB_ADAPTIVE_THRESH
> | CV_CALIB_CB_FILTER_QUADS );
> > > //Get Subpixel accuracy on those corners
> > > cvCvtColor(image, gray_image, CV_BGR2GRAY);
> > > cvFindCornerSubPix(gray_image, corners,
> corner_count,
> > > cvSize(11,11), cvSize(-1,-1),
> > > cvTermCriteria
> (CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1) );
> > > //DRAW IT
> > > cvDrawChessboardCorners(image, board_sz,
> corners, corner_count,
> > found);
> > > cvShowImage( "Calibration", image);
> > >
> > > //if we got a good board, add it to our data
> > > if( corner_count == board_n )
> > > {
> > > step = successes*board_n;
> > > for( int i=step, j=0; j<board_n;
> ++i,++j )
> > > {
> > > CV_MAT_ELEM(*image_points,
> float, i, 0) = corners[j].x;
> > > CV_MAT_ELEM(*image_points,
> float, i, 1) = corners[j].y;
> > > CV_MAT_ELEM
> (*object_points,float, i, 0) = j/board_w;
> > > CV_MAT_ELEM
> (*object_points,float, i, 1) = j%board_w;
> > > CV_MAT_ELEM
> (*object_points,float, i, 2) = 0.0f;
> > > }
> > > CV_MAT_ELEM(*point_counts, int,
> successes, 0) = board_n;
> > > successes++;
> > > }
> > > } //end skip board_dt beteween chessboard capture
> > >
> > > //Handle pause/unpause and ESC
> > > int c = cvWaitKey(15);
> > > if(c=='p')
> > > {
> > > c = 0;
> > > while (c != 'p'&& c != 27)
> > > {
> > > c = cvWaitKey(250);
> > > }
> > > }
> > > if (c==27)
> > > return 0;
> > >
> > > image = cvQueryFrame( capture ); // get next image
> > > }//end collection while loop
> > >
> > > //Allocate matrices according to how many chessboards found
> > > CvMat* object_points2 = cvCreateMat
> (successes*board_n,3,CV_32FC1);
> > > CvMat* image_points2 = cvCreateMat
> (successes*board_n,2,CV_32FC1);
> > > CvMat* point_counts2 = cvCreateMat(successes,1,CV_32SC1);
> > > //Transfer the points into the correct size matrices
> > > // Below we write out the details in the next two loops. We
> could
> > > instead have written:
> > > //image_points->rows = object_points->rows =
> successes*board_n;
> > > //point_counts->rows = successes;
> > > for (int i=0; i<successes*board_n; ++i)
> > > {
> > > CV_MAT_ELEM( *image_points2, float, i, 0) =
> > > CV_MAT_ELEM( *image_points, float, i, 0);
> > > CV_MAT_ELEM( *image_points2, float, i, 1) =
> > > CV_MAT_ELEM( *image_points, float, i, 1);
> > > CV_MAT_ELEM( *object_points2, float, i, 0) =
> > > CV_MAT_ELEM( *object_points, float, i, 0);
> > > CV_MAT_ELEM( *object_points2, float, i, 1) =
> > > CV_MAT_ELEM( *object_points, float, i, 1);
> > > CV_MAT_ELEM( *object_points2, float, i, 2) =
> > > CV_MAT_ELEM( *object_points, float, i, 2);
> > > }
> > > for (int i=0; i<successes; ++i) //these are all the same
> number
> > > {
> > > CV_MAT_ELEM( *point_counts2, int, i, 0) =
> > > CV_MAT_ELEM( *point_counts, int, i, 0);
> > > }
> > > cvReleaseMat(&object_points);
> > > cvReleaseMat(&image_points);
> > > cvReleaseMat(&point_counts);
> > >
> > > // At this point we have all of the chessboard corners we
> need.
> > > // Initialize the intrinsic matrix such that the two focal
> > > // lengths have ration of 1.0
> > > CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0) = 1.0f;
> > > CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1) = 1.0f;
> > >
> > > //Calibrate the camera !
> > > cvCalibrateCamera2(object_points2, image_points2,
> point_counts2,
> > > cvGetSize( image ),
> > > intrinsic_matrix, distortion_coeffs, NULL, NULL, 0 /*
> > > CV_CALIB_FIX_ASPECT_RATIO */ );
> > >
> > > // Save intrinsics and distortions
> > > cvSave("Intrinsics.xml",intrinsic_matrix);
> > > cvSave("Distortion.xml",distortion_coeffs);
> > >
> > > //Example of loading these matrrices back in:
> > > CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
> > > CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
> > >
> > > //Build the undistort map that we will use for all
> subsequent frames
> > > IplImage* mapx = cvCreateImage( cvGetSize(image),
> IPL_DEPTH_32F, 1 );
> > > IplImage* mapy = cvCreateImage( cvGetSize(image),
> IPL_DEPTH_32F, 1 );
> > > cvInitUndistortMap( intrinsic, distortion, mapx, mapy );
> > >
> > > //Just run the camera to the screen, now showing the raw and
> the
> > > undistorted image
> > > cvNamedWindow("Undistort");
> > > while(image)
> > > {
> > > IplImage *t = cvCloneImage(image);
> > > cvShowImage( "Calibration", image); //show raw image
> > > cvRemap( t, image, mapx, mapy); //undistort image
> > > cvReleaseImage(&t);
> > > cvShowImage( "Undistort", image);
> > >
> > > //Handle pause/unpause and ESC
> > > int c = cvWaitKey(15);
> > > if(c=='p')
> > > {
> > > c = 0;
> > > while (c != 'p'&& c != 27)
> > > {
> > > c = cvWaitKey(250);
> > > }
> > > }
> > > if (c==27)
> > > return 0;
> > >
> > > image = cvQueryFrame( capture ); // get next image
> > > }
> > >
> > > return 0;
> > > }
> > >
> >
>


Reply | Threaded
Open this post in threaded view
|

advise about senior project

MELDA AKIN
 Hello everyone,

 I need your advises.

My senior project is eye tracker system but when I search on the internet, I understood that it is very difficult to coding these systems and I have no project-friends. I read lots of papers about it and I investigated so many code samples and I have 3,5 month for coding it. Could you give me advise for it? I wanted to select it. It was my own choice but now I am very regretful and unfortunately I am very bad. I do not know what can I do. Please send me your thinking.

Thank you




     

[Non-text portions of this message have been removed]

Reply | Threaded
Open this post in threaded view
|

Re: Camera Calibration Example 11-1. Code from the book.

Ed Elston
In reply to this post by mdale9_opencv
Martin, seems pretty conclusive - thanks.  I'm not to the point of
searching through the source code yet, more into testing different
image processing techniques.

Ed

--- In [hidden email], "mdale9_opencv" <martin.dale.1@...>
wrote:
>
> Ed,
>
> Looking through the cvundistort.cpp source file it looks as though
the
> additional coefficient is used.  The stub checks to see if the
> distortion coeff matrix is in one of the correct sizes (1x5, 5x1,
1x4,
> 4x1) and then within the implementation a 5x1 vector initialized to
> zero is instantiated and filled with numbers from the distortion
> coefficients, that way if you are only using 4 coeffs then the 5th
one
> is zero.
>
> Martin.
>
> --- In [hidden email], "Ed Elston" <ed.elston@> wrote:
> >
> > Martin, I see that it is using it during calibration and also
that
> > it is recorded into the xml file, but do you believe that this
> > additional coefficient is used during the undistortion process?  
I
> > don't know either way.
> >
> > Ed
> >
> > --- In [hidden email], "mdale9_opencv" <martin.dale.1@>
> > wrote:
> > >
> > > There is no mistake,  the latest version of openCV 1.1pre1a
works

> > with
> > > either 4 or 5 distortion coefficients, the inclusion of the 5th
> > > coefficient is a new addition and it is the 3rd order radial
> > > distortion, it has just been stuck at the end for backwards
> > compatibility.
> > >
> > > Martin.
> > > --- In [hidden email], "jcz" <jakub.czaplicki@> wrote:
> > > >
> > > > Just wanted to share the code I rewritten from the O'Reillys
> > OpenCV
> > > > book. Here's the code for example 11-1 (pages 398-401),
slightly
> > > > modified, as I am not using arguments as inputs - just hard
coded

> > > > chessboard size and number of views.
> > > >
> > > > I also found one mistage in line:
> > > >    CvMat* distortion_coeffs = cvCreateMat(5,1,CV_32FC1);
> > > >
> > > > it should be:
> > > >    CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);
> > > >
> > > > otherwise cvCalibrateCamera2(...) reports error - wrong
> > distortion
> > > > matrix size.
> > > >
> > > > To test the code and calibrate my camera I used
checkerboard.gif,
> > > > which you can find at
> > http://tech.groups.yahoo.com/group/OpenCV/files/ 
> > > >
> > > > The code:
> > > >
> > > > //Example 11-1. Camera Calibration. Sligthly modified (no
input

> > > > arguments)
> > > > //
> > > > // Hit 'p' to pause/unpause, ESC to quit
> > > > //
> > > >
> > > > #include "stdafx.h"
> > > > #include "cv.h"
> > > > #include "highgui.h"
> > > > #include <stdio.h>
> > > > #include <stdlib.h>
> > > >
> > > > using namespace std;
> > > > #using <mscorlib.dll>
> > > >
> > > > // Chessboard calibration
> > > > int n_boards = 0;
> > > > const int board_dt = 20; //wait20 frame per chessboard view
> > > > int board_w, board_h;
> > > >
> > > > int main(int argc, char* argv[])
> > > > {
> > > > board_w = 6;
> > > > board_h = 4;
> > > > n_boards = 10; // number of views
> > > > int board_n = board_w * board_h;
> > > > CvSize board_sz = cvSize( board_w, board_h);
> > > > CvCapture* capture = cvCreateCameraCapture( 0 );
> > > > assert(capture);
> > > >
> > > > cvNamedWindow("Calibration");
> > > > //Allocate storage
> > > > CvMat* image_points = cvCreateMat
> > (n_boards*board_n,2,CV_32FC1);
> > > > CvMat* object_points = cvCreateMat
> > (n_boards*board_n,3,CV_32FC1);
> > > > CvMat* point_counts = cvCreateMat
> > (n_boards,1,CV_32SC1);
> > > > CvMat* intrinsic_matrix = cvCreateMat(3,3,CV_32FC1);
> > > > CvMat* distortion_coeffs = cvCreateMat
(4,1,CV_32FC1); //

> > mistake in
> > > > book (5,1,CV_32FC1) ?
> > > >
> > > > CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
> > > > int corner_count;
> > > > int successes = 0;
> > > > int step, frame = 0;
> > > > IplImage *image = cvQueryFrame( capture );
> > > > IplImage *gray_image = cvCreateImage(cvGetSize
> > (image),8,1); //subpixel
> > > >
> > > > //Capture corner views loop until we've got n_boards
> > > > //successful captures (all corners on the board are
found)
> > > > while(successes < n_boards )
> > > > {
> > > > //skip every board_dt frames to allow user
to move
> > chessboard
> > > > if (frame++ % board_dt == 0)
> > > > {
> > > > //find chessboard corners
> > > > int found = cvFindChessboardCorners(
> > > >
        image,board_sz,corners,
> > &corner_count,
> > > >
        CV_CALIB_CB_ADAPTIVE_THRESH
> > | CV_CALIB_CB_FILTER_QUADS );
> > > > //Get Subpixel accuracy on those
corners
> > > > cvCvtColor(image, gray_image,
CV_BGR2GRAY);
> > > > cvFindCornerSubPix(gray_image,
corners,
> > corner_count,
> > > > cvSize(11,11), cvSize(-1,-
1),
> > > > cvTermCriteria
> > (CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1) );
> > > > //DRAW IT
> > > > cvDrawChessboardCorners(image,
board_sz,
> > corners, corner_count,
> > > found);
> > > > cvShowImage( "Calibration", image);
> > > >
> > > > //if we got a good board, add it to
our data
> > > > if( corner_count == board_n )
> > > > {
> > > > step = successes*board_n;
> > > > for( int i=step, j=0;
j<board_n;
> > ++i,++j )
> > > > {
> > > > CV_MAT_ELEM
(*image_points,
> > float, i, 0) = corners[j].x;
> > > > CV_MAT_ELEM
(*image_points,
> > float, i, 1) = corners[j].y;
> > > > CV_MAT_ELEM
> > (*object_points,float, i, 0) = j/board_w;
> > > > CV_MAT_ELEM
> > (*object_points,float, i, 1) = j%board_w;
> > > > CV_MAT_ELEM
> > (*object_points,float, i, 2) = 0.0f;
> > > > }
> > > > CV_MAT_ELEM(*point_counts,
int,
> > successes, 0) = board_n;
> > > > successes++;
> > > > }
> > > > } //end skip board_dt beteween chessboard
capture

> > > >
> > > > //Handle pause/unpause and ESC
> > > > int c = cvWaitKey(15);
> > > > if(c=='p')
> > > > {
> > > > c = 0;
> > > > while (c != 'p'&& c != 27)
> > > > {
> > > > c = cvWaitKey(250);
> > > > }
> > > > }
> > > > if (c==27)
> > > > return 0;
> > > >
> > > > image = cvQueryFrame( capture ); // get next
image
> > > > }//end collection while loop
> > > >
> > > > //Allocate matrices according to how many
chessboards found
> > > > CvMat* object_points2 = cvCreateMat
> > (successes*board_n,3,CV_32FC1);
> > > > CvMat* image_points2 = cvCreateMat
> > (successes*board_n,2,CV_32FC1);
> > > > CvMat* point_counts2 = cvCreateMat
(successes,1,CV_32SC1);
> > > > //Transfer the points into the correct size matrices
> > > > // Below we write out the details in the next two
loops. We
> > could
> > > > instead have written:
> > > > //image_points->rows = object_points->rows =
> > successes*board_n;
> > > > //point_counts->rows = successes;
> > > > for (int i=0; i<successes*board_n; ++i)
> > > > {
> > > > CV_MAT_ELEM( *image_points2, float, i, 0) =
> > > > CV_MAT_ELEM( *image_points, float,
i, 0);
> > > > CV_MAT_ELEM( *image_points2, float, i, 1) =
> > > > CV_MAT_ELEM( *image_points, float,
i, 1);
> > > > CV_MAT_ELEM( *object_points2, float, i, 0) =
> > > > CV_MAT_ELEM( *object_points, float,
i, 0);
> > > > CV_MAT_ELEM( *object_points2, float, i, 1) =
> > > > CV_MAT_ELEM( *object_points, float,
i, 1);
> > > > CV_MAT_ELEM( *object_points2, float, i, 2) =
> > > > CV_MAT_ELEM( *object_points, float,
i, 2);
> > > > }
> > > > for (int i=0; i<successes; ++i) //these are all the
same
> > number
> > > > {
> > > > CV_MAT_ELEM( *point_counts2, int, i, 0) =
> > > > CV_MAT_ELEM( *point_counts, int, i,
0);
> > > > }
> > > > cvReleaseMat(&object_points);
> > > > cvReleaseMat(&image_points);
> > > > cvReleaseMat(&point_counts);
> > > >
> > > > // At this point we have all of the chessboard
corners we
> > need.
> > > > // Initialize the intrinsic matrix such that the two
focal
> > > > // lengths have ration of 1.0
> > > > CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0) = 1.0f;
> > > > CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1) = 1.0f;
> > > >
> > > > //Calibrate the camera !
> > > > cvCalibrateCamera2(object_points2, image_points2,
> > point_counts2,
> > > > cvGetSize( image ),
> > > > intrinsic_matrix, distortion_coeffs, NULL,
NULL, 0 /*

> > > > CV_CALIB_FIX_ASPECT_RATIO */ );
> > > >
> > > > // Save intrinsics and distortions
> > > > cvSave("Intrinsics.xml",intrinsic_matrix);
> > > > cvSave("Distortion.xml",distortion_coeffs);
> > > >
> > > > //Example of loading these matrrices back in:
> > > > CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
> > > > CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
> > > >
> > > > //Build the undistort map that we will use for all
> > subsequent frames
> > > > IplImage* mapx = cvCreateImage( cvGetSize(image),
> > IPL_DEPTH_32F, 1 );
> > > > IplImage* mapy = cvCreateImage( cvGetSize(image),
> > IPL_DEPTH_32F, 1 );
> > > > cvInitUndistortMap( intrinsic, distortion, mapx,
mapy );
> > > >
> > > > //Just run the camera to the screen, now showing the
raw and
> > the
> > > > undistorted image
> > > > cvNamedWindow("Undistort");
> > > > while(image)
> > > > {
> > > > IplImage *t = cvCloneImage(image);
> > > > cvShowImage( "Calibration", image); //show
raw image
> > > > cvRemap( t, image, mapx, mapy); //undistort
image

> > > > cvReleaseImage(&t);
> > > > cvShowImage( "Undistort", image);
> > > >
> > > > //Handle pause/unpause and ESC
> > > > int c = cvWaitKey(15);
> > > > if(c=='p')
> > > > {
> > > > c = 0;
> > > > while (c != 'p'&& c != 27)
> > > > {
> > > > c = cvWaitKey(250);
> > > > }
> > > > }
> > > > if (c==27)
> > > > return 0;
> > > >
> > > > image = cvQueryFrame( capture ); // get next
image
> > > > }
> > > >
> > > > return 0;
> > > > }
> > > >
> > >
> >
>