fork 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. const float camlen=480.0;
  59. const float camwidth=640.0;
  60. bool finalrender;
  61.  
  62. //Draws the 3D scene
  63. void drawScene() {
  64. //Clear information from last draw
  65. float vx=0.0,vy=0.0,vz=0.0,size=0.0;
  66. float vertsize=float(vertexpoints.size());
  67. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  68.  
  69. glMatrixMode(GL_MODELVIEW);
  70. glLoadIdentity();
  71.  
  72. glTranslatef(0.0f, 0.0f, -8.0f);
  73.  
  74. //Add ambient light
  75. GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
  76. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
  77.  
  78. //Add positioned light
  79. GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
  80. GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
  81. glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
  82. glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  83.  
  84. //Add directed light
  85. GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
  86. //Coming from the direction (-1, 0.5, 0.5)
  87. GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
  88. glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
  89. glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
  90. // dont need if only one shape glPushMatrix(); //opengl uses matrices, saves the transform state
  91.  
  92. glRotatef(_angle, 0.0f, 1.0f, 0.0f); //rotate by 70 degree anticlockwise
  93. glRotatef(-90.0, 1.0f, 0.0f, 0.0f);
  94. glColor3f(1.0f, 1.0f, 0.0f); //set color of shape orangey
  95. if(finalrender==false)
  96. {
  97. glBegin(GL_POINTS); //Begin points coordinates
  98. for(int v=0;v<vertsize;v++)
  99. {
  100. vx=float(vertexpoints[v].x);
  101. vy=float(vertexpoints[v].y);
  102. vz=float(vertexpoints[v].z);
  103. glVertex3f((-1.5+(3.0*(vx/90.0))),(-1.0+(2.0*(vy/90.0))),(-1.5+(3.0*(vz/90.0))));
  104. //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
  105. }
  106.  
  107. glEnd(); //End points coordinates
  108. }
  109. else
  110. {
  111.  
  112. for(int v=0;v<vertsize;v++)
  113. {
  114. //front
  115. vx=float(vertexpoints[v].x);
  116. vy=float(vertexpoints[v].y);
  117. vz=float(vertexpoints[v].z);
  118. if((vx>0)&&(vy>0)&&(vz>0))
  119. {
  120. glBegin(GL_QUADS);
  121. //front
  122. glNormal3f(0.0f, 0.0f, 1.0f);
  123. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  124. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  125. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  126. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  127.  
  128. //Right
  129. glNormal3f(1.0f, 0.0f, 0.0f);
  130. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  131. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  132. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  133. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  134.  
  135. //Back
  136. glNormal3f(0.0f, 0.0f, -1.0f);
  137. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  138. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  139. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  140. glVertex3f(((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  141.  
  142. //Left
  143. glNormal3f(-1.0f, 0.0f, 0.0f);
  144. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  145. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((-1.0f+(2.0*(vx/90.0)))-((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  146. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((1.5f+(3.0*(vx/90.0)))+((1/90.0)/2)));
  147. glVertex3f(((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)), ((1.0f+(2.0*(vx/90.0)))+((1/90.0)/2)), ((-1.5f+(3.0*(vx/90.0)))-((1/90.0)/2)));
  148.  
  149. glEnd();
  150.  
  151. }
  152. }
  153. }
  154. glutSwapBuffers(); //Send the 3D scene to the screen
  155. }
  156.  
  157. void Update(int value)
  158. {
  159. _angle+=2.0f; //increase by 20 degrees
  160. if(_angle > 360){
  161. _angle -= 360;
  162. }
  163.  
  164. glutPostRedisplay(); //redisplay new picture
  165. glutTimerFunc(25, Update, 0); //call update function every 25ms.decrease and it will call it quicker and will rotate faster
  166. }
  167.  
  168.  
  169. int main(int argc, char** argv)
  170. {
  171. glutInit(&argc, argv);
  172. glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  173. glutInitWindowSize(400,400);
  174.  
  175. Mat image;
  176. Mat Rot, Tran;
  177. int numcorners;
  178.  
  179. Mat icovar;
  180. Scalar meanmat;
  181. double covar[3][3]={{35.2189, 146.3495, 105.9640},{146.3495,801.1402,527.6974},{105.9640,527.6974,553.3654}};
  182. meanmat[0]=15.5662;
  183. meanmat[1]=118.3597;
  184. meanmat[2]=48.5153;
  185.  
  186. Mat covmat(3,3,CV_64F,covar);
  187.  
  188. Mat mask = Mat::zeros(camlen, camwidth, CV_8UC1); //create matrix same size as image which is 480 by 640 based on the webcam capture
  189. icovar=inversemat(covmat); //determinant of covariance matrix is zero. SOLVED
  190.  
  191. float distance = 250;
  192. int elemsize=3;
  193.  
  194. Mat element = getStructuringElement(0, Size( 2*elemsize + 1, 2*elemsize+1 ), Point( elemsize, elemsize ) );
  195.  
  196. Mat corners;
  197. cout<<"3D Image Reconstruction Program v1.0"<<endl;
  198. cout<<"-----------------------------------------------------------"<<endl;
  199. cout<<"Produced by: Seifullaah Sherrief"<<endl;
  200. cout<<"\nWelcome to the 3D Image reconstruction program, which will convert an object"<<endl;
  201. cout<<"into a 3D model. You will require the calibration chart with the four marked "<<endl;
  202. cout<<"circle corners, an object and a green coloured background to place the chart "<<endl;
  203. cout<<"and object in front. please refer to the User manual for operational\ninstructions."<<endl;
  204. cout<<"Press 'r' when ready to render and press 'esc' for the final 3D model"<<endl;
  205. cout<<"-------------------------------------------------------------------------"<<endl;
  206.  
  207. vector<vector<Point3f>> object_points;
  208. vector<vector<Point2f>> image_points;
  209.  
  210. vector<Point3f> obj;
  211. vector<Point2f> img;
  212.  
  213. vector<Point3i> threedpoint;
  214. vector<Point2f> projectedpoints;
  215.  
  216. Mat intrinsic = Mat(3, 3, CV_32FC1);
  217. Mat distCoeffs;
  218. vector<Mat> rvecs;
  219. vector<Mat> tvecs;
  220.  
  221. intrinsic.ptr<float>(0)[0] = 1;
  222. intrinsic.ptr<float>(1)[1] = 1;
  223. Mat silhouette;
  224. int objtemp=0;
  225. float xmax=580,xmin=80,ymin=190;
  226. VideoCapture webcam;
  227. webcam.open(-1);
  228.  
  229. bool render=false;
  230.  
  231. //distance for note purpose
  232. //rectangle horizontally dot to dot 2620 vertically 1750mm
  233. //square horizontally dot to do 1733 vertically 1750mm
  234.  
  235. //int sz[] = {lenx,leny,lenz};
  236. //Mat threedimension(3,sz,CV_32F,Scalar::all(1.0)); //create 3dim matrix, type 32 filled with 1s.
  237. double threedimension[90][90][90];
  238.  
  239. for(int i=0; i<90; i++)
  240. {
  241. for(int j=0; j<90; j++)
  242. {
  243. for(int k=0;k<90;k++)
  244. {
  245. threedimension[i][j][k]=1.0;
  246. }
  247. }
  248. }
  249.  
  250. cout<<"Enter number of corners to detect (must be greater than 4) e.g 5: "<<endl;
  251. cin>>numcorners;
  252.  
  253. if(!webcam.isOpened())
  254. {
  255. 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;
  256. system("PAUSE");
  257. return 0;
  258. }
  259.  
  260. obj.push_back(Point3f(0,0,0));
  261. obj.push_back(Point3f(90.0,0,0));
  262. obj.push_back(Point3f(0,90.0,0));
  263. obj.push_back(Point3f(90.0,90.0,0));
  264.  
  265. glutCreateWindow("Temporary Visual of 3D Model");
  266. initRendering();
  267. while(1)
  268. {
  269. //copy webcam stream to image
  270. webcam>>image;
  271. glutKeyboardFunc(handleKeypress);
  272. glutReshapeFunc(handleResize);
  273. int key=waitKey(1);
  274. if(key=='r'){render=true;}
  275.  
  276. #pragma omp parallel sections
  277. {
  278. #pragma omp section
  279. {
  280. silhouette=imagesegmentation(image,icovar,meanmat,distance,mask,element);
  281. }
  282. #pragma omp section
  283. {
  284. corners=Cornerdetect(image,corners,numcorners);
  285. }
  286. }
  287.  
  288. if(corners.rows>0)
  289. {
  290. #pragma omp parallel for
  291. for(int i=0;i<corners.rows;i++)
  292. {
  293. cout<<corners.at<float>(i,0)<<endl;
  294. //if((corners.at<float>(i,0)>xmin)&&(corners.at<float>(i,1)>ymin)&&(corners.at<float>(i,0)<xmax))
  295. //{
  296. //draws circle on image, at centre at point, color, thickness, line type,
  297. circle(image,corners.at<Point2f>(i),3,CV_RGB(255,0,0),1,8,0);
  298. //obj.push_back(Point3f(float(objtemp/2), float(objtemp%2), 0.0f)); //setting up the units of calibration
  299. img.push_back(corners.at<Point2f>(i));
  300. objtemp++;
  301. //}
  302. }
  303. if(objtemp==4)
  304. {
  305. image_points.push_back(img);
  306. object_points.push_back(obj);
  307. calibrateCamera(object_points, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs);
  308. Rot=rvecs[0];
  309. Tran=tvecs[0];
  310.  
  311. if(render)
  312. {
  313. for(int l=0;l<90;l++)
  314. {
  315. for(int w=0;w<90;w++)
  316. {
  317. for(int h=0;h<90;h++)
  318. {
  319. threedpoint.push_back(Point3i(l,w,h));
  320. }
  321. }
  322. }
  323.  
  324. projectPoints(threedpoint,Rot,Tran,intrinsic,distCoeffs,projectedpoints);
  325.  
  326. // Allocate the vector
  327. vertexpoints.resize(projectedpoints.size(),Point3f(0,0,0));
  328.  
  329. double t1 = omp_get_wtime();
  330. #pragma omp parallel for
  331. for(int index=0;index<projectedpoints.size();index++)
  332. {
  333. int dx = threedpoint[index].x, dy=threedpoint[index].y,dz=threedpoint[index].z;
  334. double check = threedimension[dx][dy][dz];
  335. if(check==1)
  336. {
  337. vertexpoints[index] = Point3f(dx,dy,dz);
  338. if(float(mask.at<uchar>(projectedpoints[index]))==255.0)
  339. {
  340. threedimension[dx][dy][dz]=0.0;
  341. }
  342. }
  343. /*else if((check==0.0)&&(mask.at<uchar>(projectedpoints[index]))==0)
  344. {
  345. threedimension[dx][dy][dz]=1.0;
  346. vertexpoints[index] = Point3f(dx,dy,dz);
  347. }*/
  348. }
  349. double elapsed = t1 - omp_get_wtime();
  350. printf( "Image segmentation took: %.2f", elapsed);
  351.  
  352. glutDisplayFunc(drawScene);
  353. glutTimerFunc(25, Update, 0); //call update function every 25ms.decrease and it will call it quicker and will rotate faster
  354. glutMainLoopEvent(); //from here you can specifiy your own glutmainloop event which ones to do, this will do one iteration and continue through
  355. //out the while loop hopefully
  356. break; //a testing purpose break
  357. }
  358.  
  359. }
  360. imshow("original", image);
  361. waitKey(30);
  362. imshow("mask",mask);
  363. waitKey(30); //this is to give the processor some time to display the image
  364. //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
  365. //after rendering clear it so it doesn't stack on top
  366. }
  367. vertexpoints.clear();
  368. objtemp=0;
  369. img.clear();
  370. image_points.clear();
  371. object_points.clear();
  372. if(finish){break;}
  373.  
  374. }
  375. webcam.release();
  376. destroyWindow("original");
  377. destroyWindow("mask");
  378. cout<<"The Final 3D Model is Being Rendered and will be displayed Shortly."<<endl;
  379. cout<<"\nThank you for using the 3D Image Reconstruction Software"<<endl;
  380. finalrender=true;
  381. //finalrender is for switching between different rendering
  382. //check to see if any of the neighbour voxels are zero and then just push those vertices to the vertex and render triangle fan
  383. //we have the final 3d array
  384.  
  385. for(int i=0;i<threedpoint.size();i++)
  386. {
  387. if(threedimension[threedpoint[i].x][threedpoint[i].y][threedpoint[i].z]==1)
  388. {
  389. vertexpoints.push_back(Point3f(threedpoint[i])); //push all the values of vertex points to the renderer
  390. }
  391. }
  392.  
  393. glutReshapeFunc(handleResize);
  394. glutDisplayFunc(drawScene);
  395. glutTimerFunc(25, Update, 0); //call update function every 25ms.decrease and it will call it quicker and will rotate faster
  396. glutMainLoop();
  397. return 0;
  398. }
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