1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
int main()
{
Mat image, grayscale;
VideoCapture webcam;
webcam.open(-1);
int numcorners, horcorner,vercorner;
Mat corners;
printf("Enter number of corners horizontally: ");
scanf("%d", &horcorner);
printf("Enter number of corners vertically: ");
scanf("%d", &vercorner);
numcorners=horcorner*vercorner;
namedWindow("original",1);
vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;
vector<Point3f> obj;
vector<Point2f> img;
for(int j=0;j<numcorners;j++)
obj.push_back(Point3f(float(j/vercorner), float(j%vercorner), 0.0f)); //setting up the units of calibration
while(1)
{
//copy webcam stream to image
webcam>>image;
cvtColor(image,grayscale,CV_BGR2GRAY);
//goodfeatures(grayimage, output to store corners, quality factor, distance factor)
goodFeaturesToTrack(grayscale,corners,numcorners,0.1,20);
// Mark these corners on the original image
cornerSubPix(grayscale, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
for(int i=0;i<corners.rows;i++)
{
//draws circle on image, at centre at point, color, thickness, line type,
circle(image,corners.at<Point2f>(i),3,CV_RGB(255,0,0),1,8,0);
}
// Print the number of corners
cout<<corners<<endl;
cout<<corners.at<Point2f>(0)<<endl; //access the coordinates
cout<<corners.at<float>(0,0)<<endl; //access the first element of the first coordinates
imshow("original",image);
int key = waitKey(1);
if(key==27)
return 0;
if(key==' ')
{
for(int k=0;k<corners.rows;k++)
{
img.push_back(corners.at<Point2f>(k));
}
image_points.push_back(img);
object_points.push_back(obj);
printf("Snap stored!\n");
break;
}
}
Mat intrinsic = Mat(3, 3, CV_32FC1);
Mat distCoeffs;
vector<Mat> rvecs;
vector<Mat> tvecs;
intrinsic.ptr<float>(0)[0] = 1;
intrinsic.ptr<float>(1)[1] = 1;
calibrateCamera(object_points, img, image.size(), intrinsic, distCoeffs, rvecs, tvecs); //unhandled exception error at this point not because of too much points
while(1)
{
webcam>>image;
imshow("original",image);
int key=waitKey(1);
if(key==27)
return 0;
}
return 0;
}
|