|
| 1 | +''' camera_calibration.py |
| 2 | +
|
| 3 | +Usage: python camera_calibration.py board_w board_h number_of_views |
| 4 | + |
| 5 | +This program reads a chessboard's width and height, collects requested number of views and calibrates the camera. |
| 6 | +
|
| 7 | +This is a little modified version of the example 11-1 given in the book "Learning OpenCV: Computer Vision with the OpenCV Library". |
| 8 | +
|
| 9 | +Converted to Python by Abid.K --mail me at [email protected] |
| 10 | +
|
| 11 | +''' |
| 12 | + |
| 13 | +################################################################################################ |
| 14 | + |
| 15 | +import cv,time,sys |
| 16 | + |
| 17 | +n_boards=0 #no of boards |
| 18 | +board_w=int(sys.argv[1]) # number of horizontal corners |
| 19 | +board_h=int(sys.argv[2]) # number of vertical corners |
| 20 | +n_boards=int(sys.argv[3]) |
| 21 | +board_n=board_w*board_h # no of total corners |
| 22 | +board_sz=(board_w,board_h) #size of board |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +# creation of memory storages |
| 28 | +image_points=cv.CreateMat(n_boards*board_n,2,cv.CV_32FC1) |
| 29 | +object_points=cv.CreateMat(n_boards*board_n,3,cv.CV_32FC1) |
| 30 | +point_counts=cv.CreateMat(n_boards,1,cv.CV_32SC1) |
| 31 | +intrinsic_matrix=cv.CreateMat(3,3,cv.CV_32FC1) |
| 32 | +distortion_coefficient=cv.CreateMat(5,1,cv.CV_32FC1) |
| 33 | + |
| 34 | +# capture frames of specified properties and modification of matrix values |
| 35 | +i=0 |
| 36 | +z=0 # to print number of frames |
| 37 | +successes=0 |
| 38 | +capture=cv.CaptureFromCAM(0) |
| 39 | +# capturing required number of views |
| 40 | +while(successes<n_boards): |
| 41 | + found=0 |
| 42 | + image=cv.QueryFrame(capture) |
| 43 | + gray_image=cv.CreateImage(cv.GetSize(image),8,1) |
| 44 | + cv.CvtColor(image,gray_image,cv.CV_BGR2GRAY) |
| 45 | + |
| 46 | + (found,corners)=cv.FindChessboardCorners(gray_image,board_sz,cv.CV_CALIB_CB_ADAPTIVE_THRESH| cv.CV_CALIB_CB_FILTER_QUADS) |
| 47 | + corners=cv.FindCornerSubPix(gray_image,corners,(11,11),(-1,-1),(cv.CV_TERMCRIT_EPS+cv.CV_TERMCRIT_ITER,30,0.1)) |
| 48 | + # if got a good image,draw chess board |
| 49 | + if found==1: |
| 50 | + print "found frame number {0}".format(z+1) |
| 51 | + cv.DrawChessboardCorners(image,board_sz,corners,1) |
| 52 | + corner_count=len(corners) |
| 53 | + z=z+1 |
| 54 | + |
| 55 | + # if got a good image, add to matrix |
| 56 | + if len(corners)==board_n: |
| 57 | + step=successes*board_n |
| 58 | + k=step |
| 59 | + for j in range(board_n): |
| 60 | + cv.Set2D(image_points,k,0,corners[j][0]) |
| 61 | + cv.Set2D(image_points,k,1,corners[j][1]) |
| 62 | + cv.Set2D(object_points,k,0,float(j)/float(board_w)) |
| 63 | + cv.Set2D(object_points,k,1,float(j)%float(board_w)) |
| 64 | + cv.Set2D(object_points,k,2,0.0) |
| 65 | + k=k+1 |
| 66 | + cv.Set2D(point_counts,successes,0,board_n) |
| 67 | + successes=successes+1 |
| 68 | + time.sleep(2) |
| 69 | + print "-------------------------------------------------" |
| 70 | + print "\n" |
| 71 | + cv.ShowImage("Test Frame",image) |
| 72 | + cv.WaitKey(33) |
| 73 | + |
| 74 | +print "checking is fine ,all matrices are created" |
| 75 | +cv.DestroyWindow("Test Frame") |
| 76 | + |
| 77 | +# now assigning new matrices according to view_count |
| 78 | +object_points2=cv.CreateMat(successes*board_n,3,cv.CV_32FC1) |
| 79 | +image_points2=cv.CreateMat(successes*board_n,2,cv.CV_32FC1) |
| 80 | +point_counts2=cv.CreateMat(successes,1,cv.CV_32SC1) |
| 81 | + |
| 82 | +#transfer points to matrices |
| 83 | + |
| 84 | +for i in range(successes*board_n): |
| 85 | + cv.Set2D(image_points2,i,0,cv.Get2D(image_points,i,0)) |
| 86 | + cv.Set2D(image_points2,i,1,cv.Get2D(image_points,i,1)) |
| 87 | + cv.Set2D(object_points2,i,0,cv.Get2D(object_points,i,0)) |
| 88 | + cv.Set2D(object_points2,i,1,cv.Get2D(object_points,i,1)) |
| 89 | + cv.Set2D(object_points2,i,2,cv.Get2D(object_points,i,2)) |
| 90 | +for i in range(successes): |
| 91 | + cv.Set2D(point_counts2,i,0,cv.Get2D(point_counts,i,0)) |
| 92 | + |
| 93 | +cv.Set2D(intrinsic_matrix,0,0,1.0) |
| 94 | +cv.Set2D(intrinsic_matrix,1,1,1.0) |
| 95 | + |
| 96 | +rcv = cv.CreateMat(n_boards, 3, cv.CV_64FC1) |
| 97 | +tcv = cv.CreateMat(n_boards, 3, cv.CV_64FC1) |
| 98 | + |
| 99 | +print "checking camera calibration............." |
| 100 | +# camera calibration |
| 101 | +cv.CalibrateCamera2(object_points2,image_points2,point_counts2,cv.GetSize(image),intrinsic_matrix,distortion_coefficient,rcv,tcv,0) |
| 102 | +print " checking camera calibration.........................OK " |
| 103 | + |
| 104 | +# storing results in xml files |
| 105 | +cv.Save("Intrinsics.xml",intrinsic_matrix) |
| 106 | +cv.Save("Distortion.xml",distortion_coefficient) |
| 107 | +# Loading from xml files |
| 108 | +intrinsic = cv.Load("Intrinsics.xml") |
| 109 | +distortion = cv.Load("Distortion.xml") |
| 110 | +print " loaded all distortion parameters" |
| 111 | + |
| 112 | +mapx = cv.CreateImage( cv.GetSize(image), cv.IPL_DEPTH_32F, 1 ); |
| 113 | +mapy = cv.CreateImage( cv.GetSize(image), cv.IPL_DEPTH_32F, 1 ); |
| 114 | +cv.InitUndistortMap(intrinsic,distortion,mapx,mapy) |
| 115 | +cv.NamedWindow( "Undistort" ) |
| 116 | +print "all mapping completed" |
| 117 | +print "Now relax for some time" |
| 118 | +time.sleep(8) |
| 119 | + |
| 120 | +print "now get ready, camera is switching on" |
| 121 | +while(1): |
| 122 | + image=cv.QueryFrame(capture) |
| 123 | + t = cv.CloneImage(image); |
| 124 | + cv.ShowImage( "Calibration", image ) |
| 125 | + cv.Remap( t, image, mapx, mapy ) |
| 126 | + cv.ShowImage("Undistort", image) |
| 127 | + c = cv.WaitKey(33) |
| 128 | + if(c == 1048688): # enter 'p' key to pause for some time |
| 129 | + cv.WaitKey(2000) |
| 130 | + elif c==1048603: # enter esc key to exit |
| 131 | + break |
| 132 | + |
| 133 | +print "everything is fine" |
| 134 | + |
| 135 | +############################################################################################### |
0 commit comments