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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include<vector>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv2/imgproc/imgproc.hpp>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include"reconstruction.h"
using namespace cv;
using namespace std;
int main()
{
Mat image, grayscale;
int numcorners, horcorner,vercorner;
Mat icovar;
Scalar meanmat;
int k=1;
//covariance for dark combined
double covar[3][3]={{180.1437, 180.8316, 179.0236},{188.8316,355.5152,238.8029},{179.0236,238.8029,267.9239}};
meanmat[0]=13.8340;
meanmat[1]=68.3459;
meanmat[2]=22.7451;
Mat covmat(3,3,CV_64F,covar);
Mat mask = Mat::zeros(480, 640, CV_8UC1); //create matrix same size as image which is 480 by 640 based on the webcam capture
//intitialize capture
Vec3b pixel;
double distance = 200;
double mdist=0;
icovar=inversemat(covmat); //determinant of covariance matrix is zero. SOLVED
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;
vector<Point2f> projectedpoints;
VideoCapture webcam;
webcam.open(-1);
while(1)
{
//copy webcam stream to image
webcam>>image;
if(!webcam.isOpened())
{
cout<<"\nThe Camera is being used by another application, make sure all applications using the camera are closed and try running this program again."<<endl;
break;
}
for(int i = 0; i < image.rows;i++)
{
for(int j=0; j<image.cols;j++) //in case it fails changed it from i=1 to i=0
{
pixel= image.at<Vec3b>(i,j); //prints wierd characters
mdist=mahadistance(icovar,meanmat,pixel);
if(mdist<distance)
mask.at<uchar>(i,j)=255;
else
mask.at<uchar>(i,j)=0;
}
}
cvtColor(image,grayscale,CV_BGR2GRAY);
//goodfeatures(grayimage, output to store corners, quality factor, distance factor)
goodFeaturesToTrack(grayscale,corners,numcorners,0.1,100); //good so far 0.1 and 100 also 0.01 and 100 a little ok i chose this
// 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));
if(corners.rows==numcorners)
{
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);
obj.push_back(Point3f(float(i/vercorner), float(i%vercorner), 0.0f)); //setting up the units of calibration
img.push_back(corners.at<Point2f>(i));
}
image_points.push_back(img);
object_points.push_back(obj);
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, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs); //unhandled exception error at this point not because of too much points
if(k==1)
{
k=k+1;
float xmax=0,xmin=1000,ymax=0,ymin=1000,z;
for(int l=0;l<corners.rows;l++)
{
if(corners.at<float>(l,0)<xmin)
{
xmin=corners.at<float>(l,0);
}
if(corners.at<float>(l,1)<ymin)
{
ymin=corners.at<float>(l,1);
}
if(corners.at<float>(l,0)>xmax)
{
xmax=corners.at<float>(l,0);
}
if(corners.at<float>(l,1)>ymax)
{
ymax=corners.at<float>(l,1);
}
}
printf("Enter the height of the object (mm)");
scanf("%f",&z);
int sz[] = {(xmax-xmin),(ymax-ymin),z};
Mat threedimension(3,sz,CV_32F,Scalar::all(1)); //create 3dim matrix, type 32 filled with 1s.
//access each element like threedimension.at<float>(x,y,z)
}
projectPoints(obj,rvecs,tvecs,intrinsic,distCoeffs,projectedpoints);
cout<<projectedpoints<<endl;
imshow("original",image);
imshow("mask",mask);
}
img.clear();
obj.clear();
image_points.clear();
object_points.clear();
}
webcam.release();
system("PAUSE");
return 0;
}
|