fork(6) download
  1. #include<vector>
  2. #include<opencv\cv.h>
  3. #include<opencv\highgui.h>
  4. #include<opencv2/imgproc/imgproc.hpp>
  5. #include<cstdlib>
  6. #include<iostream>
  7. #include<omp.h>
  8. //#include<glut.h>
  9. #include<GL/freeglut.h>
  10. #include<GL/gl.h>
  11. #include<GL/glu.h>
  12. #include<GL/glut.h>
  13. #include"reconstruction.h"
  14.  
  15. using namespace cv;
  16. using namespace std;
  17.  
  18. bool finish=false;
  19.  
  20. //Called when a key is pressed
  21. void handleKeypress(unsigned char key, //The key that was pressed
  22. int x, int y) { //The current mouse coordinates
  23. switch (key) {
  24. case 27: //Escape key
  25. finish=true;
  26. }
  27. }
  28.  
  29. //Initializes 3D rendering
  30. void initRendering() {
  31. //Makes 3D drawing work when something is in front of something else
  32. glEnable(GL_DEPTH_TEST);
  33. glEnable(GL_COLOR_MATERIAL);
  34. glEnable(GL_LIGHTING); //Enable lighting
  35. glEnable(GL_LIGHT0); //Enable light #0
  36. glEnable(GL_LIGHT1); //Enable light #1
  37. glEnable(GL_NORMALIZE); //Automatically normalize normals
  38. glShadeModel(GL_SMOOTH); //Enable smooth shading
  39. }
  40.  
  41. //Called when the window is resized
  42. void handleResize(int w, int h) {
  43. //Tell OpenGL how to convert from coordinates to pixel values
  44. glViewport(0, 0, w, h);
  45.  
  46. glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
  47.  
  48. //Set the camera perspective
  49. glLoadIdentity(); //Reset the camera
  50. gluPerspective(45.0, //The camera angle
  51. (double)w / (double)h, //The width-to-height ratio
  52. 1.0, //The near z clipping coordinate
  53. 200.0); //The far z clipping coordinate
  54. }
  55.  
  56. float _angle = -70.0f;
  57. vector<Point3f> vertexpoints;
  58. float lenx=0.0,leny=0.0,lenz=0.0;
  59. const float camlen=480.0;
  60. const float camwidth=640.0;
  61. bool finalrender;
  62.  
  63. //Draws the 3D scene
  64. void drawScene() {
  65. //Clear information from last draw
  66. float vx=0.0,vy=0.0,vz=0.0,size=0.0;
  67. float vertsize=float(vertexpoints.size());
  68. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  69.  
  70. glMatrixMode(GL_MODELVIEW);
  71. glLoadIdentity();
  72.  
  73. glTranslatef(0.0f, 0.0f, -8.0f);
  74.  
  75. //Add ambient light
  76. GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
  77. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
  78.  
  79. //Add positioned light
  80. GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
  81. GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
  82. glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
  83. glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  84.  
  85. //Add directed light
  86. GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
  87. //Coming from the direction (-1, 0.5, 0.5)
  88. GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
  89. glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
  90. glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
  91. // dont need if only one shape glPushMatrix(); //opengl uses matrices, saves the transform state
  92.  
  93. glRotatef(_angle, 0.0f, 1.0f, 0.0f); //rotate by 70 degree anticlockwise
  94. glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
  95. glColor3f(1.0f, 1.0f, 0.0f); //set color of shape orangey
  96. if(finalrender==false)
  97. {
  98. glBegin(GL_POINTS); //Begin points coordinates
  99. for(int v=0;v<vertsize;v++)
  100. {
  101. vx=float(vertexpoints[v].x);
  102. vy=float(vertexpoints[v].y);
  103. vz=float(vertexpoints[v].z);
  104. glVertex3f((-1.5+(3.0*(vx/lenx))),(-1.0+2.0*(vy/leny)),(-1.5+(3.0*(vz/lenz))));
  105. //idea behind this is to limit the size between -1.5 to 1.5 for x and z direction and -1 to 1 for y direction
  106. }
  107.  
  108. glEnd(); //End points coordinates
  109. }
  110. else
  111. {
  112. glBegin(GL_TRIANGLE_STRIP); //Begin points coordinates
  113. for(int v=0;v<vertsize;v++)
  114. {
  115. vx=float(vertexpoints[v].x);
  116. vy=float(vertexpoints[v].y);
  117. vz=float(vertexpoints[v].z);
  118. glVertex3f((-1.5+(3.0*(vx/lenx))),(-1.0+2.0*(vy/leny)),(-1.5+(3.0*(vz/lenz))));
  119. //idea behind this is to limit the size between -1.5 to 1.5 for x and z direction and -1 to 1 for y direction
  120. }
  121.  
  122. glEnd(); //End points coordinates
  123. }
  124. //glPopMatrix(); // restore transform state dont need it as only shape being drawn
  125.  
  126. glutSwapBuffers(); //Send the 3D scene to the screen
  127. }
  128.  
  129. void Update(int value)
  130. {
  131. _angle+=2.0f; //increase by 20 degrees
  132. if(_angle > 360){
  133. _angle -= 360;
  134. }
  135.  
  136. glutPostRedisplay(); //redisplay new picture
  137. glutTimerFunc(25, Update, 0); //call update function every 25ms.decrease and it will call it quicker and will rotate faster
  138. }
  139.  
  140. bool PtInRect(float x, float y)
  141. {
  142. if((x<camwidth)&&(x>0)&&(y<camlen)&&(y>0))
  143. {
  144. return true;
  145. }
  146. else return false;
  147. }
  148.  
  149. static int win;
  150.  
  151. int main(int argc, char** argv)
  152. {
  153. glutInit(&argc, argv);
  154. glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  155. glutInitWindowSize(400,400);
  156.  
  157. Mat image;
  158. Mat Rot, Tran;
  159. int numcorners;
  160.  
  161. Mat icovar;
  162. Scalar meanmat;
  163. double covar[3][3]={{35.2189, 146.3495, 105.9640},{146.3495,801.1402,527.6974},{105.9640,527.6974,553.3654}};
  164. meanmat[0]=15.5662;
  165. meanmat[1]=118.3597;
  166. meanmat[2]=48.5153;
  167.  
  168. Mat covmat(3,3,CV_64F,covar);
  169.  
  170. Mat mask = Mat::zeros(camlen, camwidth, CV_8UC1); //create matrix same size as image which is 480 by 640 based on the webcam capture
  171. icovar=inversemat(covmat); //determinant of covariance matrix is zero. SOLVED
  172.  
  173. float distance = 250;
  174. int elemsize=3;
  175.  
  176. Mat element = getStructuringElement(0, Size( 2*elemsize + 1, 2*elemsize+1 ), Point( elemsize, elemsize ) );
  177.  
  178. Mat corners;
  179. cout<<"Enter number of corners to detect (must be greater than 4) e.g 5: "<<endl;
  180. cin>>numcorners;
  181.  
  182. vector<vector<Point3f>> object_points;
  183. vector<vector<Point2f>> image_points;
  184.  
  185. vector<Point3f> obj;
  186. vector<Point2f> img;
  187.  
  188. vector<Point3f> threedpoint;
  189. vector<Point2f> projectedpoints;
  190.  
  191. Mat intrinsic = Mat(3, 3, CV_32FC1);
  192. Mat distCoeffs;
  193. vector<Mat> rvecs;
  194. vector<Mat> tvecs;
  195.  
  196. intrinsic.ptr<float>(0)[0] = 1;
  197. intrinsic.ptr<float>(1)[1] = 1;
  198. float check=0.0;
  199. Mat silhouette;
  200. int objtemp=0;
  201. float xmax=580,xmin=80,ymin=190;
  202. VideoCapture webcam;
  203. webcam.open(-1);
  204.  
  205. bool render=false;
  206.  
  207. //distance for note purpose
  208. //rectangle horizontally dot to dot 2620 vertically 1750mm
  209. //square horizontally dot to do 1733 vertically 1750mm
  210. cout<<"Enter the distance between the two marked corners in the x direction (mm): "<<endl;
  211. cin>>lenx;
  212. cout<<"---------------------------------------------------------------------"<<endl;
  213. cout<<"NOTE:Make sure you Adjust the parameters of the camera i.e. brightness\nto the required amount. Press 'r' when ready to render and press 's' to generate the final 3D model"<<endl;
  214. cout<<"---------------------------------------------------------------------"<<endl;
  215. cout<<"Enter the distance between the two marked corners in the y direction (mm): "<<endl;
  216. cin>>leny;
  217. cout<<"Enter the height of the object with extra 1cm for space (mm): "<<endl;
  218. cin>>lenz;
  219.  
  220. int sz[] = {lenx,leny,lenz};
  221. Mat threedimension(3,sz,CV_32F,Scalar::all(1.0)); //create 3dim matrix, type 32 filled with 1s.
  222. //cout<<threedimension.at<float>(0,0,4); // but its not coordinates its elements so you are accessing elements in a 3d array not finding y value
  223. //you are getting the value in the first row, first column and 4th element along z axis
  224. //to access x value of point 3f of ith element it is a[i].x; easy.
  225. //same with point2f
  226. //cout<<threedimension.at<float>(0,0,0); //now it works, had to do float not ints.
  227. if(!webcam.isOpened())
  228. {
  229. 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;
  230. system("PAUSE");
  231. return 0;
  232. }
  233.  
  234. obj.push_back(Point3f(0,0,0));
  235. obj.push_back(Point3f(lenx,0,0));
  236. obj.push_back(Point3f(0,leny,0));
  237. obj.push_back(Point3f(lenx,leny,0));
  238.  
  239. win=glutCreateWindow("Temporary Visual of 3D Model");
  240. initRendering();
  241. while(1)
  242. {
  243. //copy webcam stream to image
  244. webcam>>image;
  245. glutKeyboardFunc(handleKeypress);
  246. glutReshapeFunc(handleResize);
  247. int key=waitKey(1);
  248. if(key=='r'){render=true;}
  249. #pragma omp parallel sections
  250. {
  251. #pragma omp section
  252. {silhouette=imagesegmentation(image,icovar,meanmat,distance,mask,element); }
  253. #pragma omp section
  254. {corners=Cornerdetect(image,corners,numcorners);}
  255. }
  256. if(corners.rows>4)
  257. {
  258. //#pragma omp parallel for
  259. for(int i=(corners.rows-4);i<corners.rows;i++)
  260. {
  261. if((corners.at<float>(i,0)>xmin)&&(corners.at<float>(i,1)>ymin)&&(corners.at<float>(i,0)<xmax))
  262. {
  263. //draws circle on image, at centre at point, color, thickness, line type,
  264. circle(image,corners.at<Point2f>(i),3,CV_RGB(255,0,0),1,8,0);
  265. //obj.push_back(Point3f(float(objtemp/2), float(objtemp%2), 0.0f)); //setting up the units of calibration
  266. img.push_back(corners.at<Point2f>(i));
  267. objtemp++;
  268. }
  269. }
  270. if(objtemp==4)
  271. {
  272. image_points.push_back(img);
  273. object_points.push_back(obj);
  274. calibrateCamera(object_points, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs);
  275. Rot=rvecs[0];
  276. Tran=tvecs[0];
  277.  
  278. if(render)
  279. {
  280. //#pragma omp parallel for //unhandled exception error
  281. for(int l=0;l<int(lenx);l++)
  282. {
  283. for(int w=0;w<int(leny);w++)
  284. {
  285. for(int h=0;h<int(lenz);h++)
  286. {
  287. threedpoint.push_back(Point3f(l,w,h));
  288. }
  289. }
  290. }
  291.  
  292. projectPoints(threedpoint,Rot,Tran,intrinsic,distCoeffs,projectedpoints);
  293. for(int index=0;index<projectedpoints.size();index++)
  294. {
  295. if(PtInRect(projectedpoints[index].x,projectedpoints[index].y))
  296. {
  297. //access each element of threedimension.at<float>(x,y,z)
  298. float dx=threedpoint[index].x, dy=threedpoint[index].y,dz=threedpoint[index].z;
  299. check = threedimension.at<float>(dx,dy,dz);
  300. if(check==1.0) //-1 rows and -1 cols check into it, solved cannot check the matrix directly so assign it to a variable then check
  301. {
  302. vertexpoints.push_back(Point3f(dx,dy,dz));
  303. if(float(mask.at<uchar>(projectedpoints[index]))==255.0)
  304. {
  305.  
  306. threedimension.at<float>(dx,dy,dz)=0.0;
  307. }
  308.  
  309. }
  310. else if((check==0.0)&&(int(mask.at<uchar>(projectedpoints[index]))==0))
  311. {
  312. threedimension.at<float>(dx,dy,dz)=1.0;
  313. vertexpoints.push_back(Point3f(dx,dy,dz));
  314. }
  315. }
  316. }
  317. glutDisplayFunc(drawScene);
  318. glutTimerFunc(25, Update, 0); //call update function every 25ms.decrease and it will call it quicker and will rotate faster
  319. glutMainLoopEvent();
  320.  
  321. }
  322.  
  323. imshow("original", image);
  324. waitKey(30);
  325. imshow("mask",mask);
  326. waitKey(30); //this is to give the processor some time to display the image
  327. //rendering over here with the displays, loop through each points which is a one to get a list of vectors (vertexpoints) then draw using those points by looping through it
  328. //after rendering clear it so it doesn't stack on top
  329. }
  330. }
  331. if(finish==false){vertexpoints.clear();}
  332. objtemp=0;
  333. img.clear();
  334. image_points.clear();
  335. object_points.clear();
  336. if(finish){break;}
  337. //from here you can specifiy your own glutmainloop event which ones to do, this will do one iteration and continue through
  338. //out the while loop hopefully
  339. }
  340.  
  341. webcam.release();
  342. destroyWindow("original");
  343. destroyWindow("mask");
  344. cout<<"The Final 3D Model is Being Rendered and will be displayed Shortly."<<endl;
  345. finalrender=true;
  346. //finalrender is for switching between different rendering
  347. //check to see if any of the neighbour voxels are zero and then just push those vertices to the vertex and render triangle fan
  348. //we have the final 3d array
  349.  
  350. for(int l=0;l<int(lenx);l++)
  351. {
  352. for(int w=0;w<int(leny);w++)
  353. {
  354. for(int h=0;h<int(lenz);h++)
  355. {
  356. if(threedimension.at<float>(l,w,h)==1)
  357. {
  358. //check if above, left or right, behind or infront is zero it is an edge. no diagonals that would be inside.
  359. //obviously no edge at the bottom so leave the bottom as it is. so check for w is 0 if it is at the bottom of the model, then push the vertex to the draw program
  360. if((threedimension.at<float>(l+1,w,h)==0)||(threedimension.at<float>(l-1,w,h)==0)||(threedimension.at<float>(l,w+1,h)==0)||(threedimension.at<float>(l,w,h)==0)||(threedimension.at<float>(l,w,h+1)==0)||(threedimension.at<float>(l,w,h-1)==0)||w==0)
  361. {
  362. vertexpoints.push_back(Point3f(l,w,h));
  363. }
  364. }
  365. }
  366. }
  367. }
  368.  
  369. glutReshapeFunc(handleResize);
  370. glutDisplayFunc(drawScene);
  371. glutTimerFunc(25, Update, 0); //call update function every 25ms.decrease and it will call it quicker and will rotate faster
  372. glutMainLoop();
  373. return 0;
  374. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:22: fatal error: opencv\cv.h: No such file or directory
 #include<opencv\cv.h>
                      ^
compilation terminated.
stdout
Standard output is empty