Guest User

Untitled

a guest
Feb 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #define CAPT 1
  2. #ifdef CAPT
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <cstdio>
  8. #include "cv.h"
  9. #include "highgui.h"
  10. #include "cvBlob/cvblob.h"
  11.  
  12. using namespace cv;
  13. using namespace cvb;
  14. using namespace std;
  15.  
  16.  
  17. int main(int, char**)
  18. {
  19. //ofstream myfile;
  20. int state=0;
  21. int calibration=0;
  22. int go=0;
  23. int sumB=0, curB=0;
  24. int mode = -1, move=0;
  25.  
  26. VideoCapture cap(0); // open the default camera
  27. if(!cap.isOpened()) // check if we succeeded
  28. return -1;
  29.  
  30. Mat frame, frameEq;
  31. Scalar color = CV_RGB(0,128,255);
  32. cap >> frame;
  33.  
  34. namedWindow("Cr",1);
  35. namedWindow("Camera",0);
  36.  
  37. vector<Mat> layers, layers2;
  38. CvBlobs blobs;
  39. CvTracks tracks;
  40. IplConvKernel* morphKernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT, NULL);
  41.  
  42. IplImage *img = &(frame.operator IplImage());
  43.  
  44. CvSize imgSize = cvGetSize(img);
  45. //IplImage *mod = cvCreateImage(imgSize, img->depth, img->nChannels);
  46.  
  47. Mat handelingFrame;
  48. CvBlob *head=NULL, *handL=NULL, *handR=NULL ;
  49. int headN=0, handLN=0, handRN=0;
  50. int posx=1, posy=1;
  51. int ges=0;
  52. for(;;) {
  53. int nF=0, numberFingers=0, oldnumF=0;
  54.  
  55. cap >> frame;
  56.  
  57. //Equalize histogram (test code)
  58. split(frame, layers2);
  59. equalizeHist(layers2[0],layers2[0]); equalizeHist(layers2[1],layers2[1]); equalizeHist(layers2[2],layers2[2]);
  60. merge(layers2,frameEq);
  61.  
  62. //Color conversation
  63. cvtColor(frame, handelingFrame, CV_BGR2YCrCb);
  64.  
  65. //Skin Color Segmentation.
  66. split(handelingFrame, layers);
  67. inRange(layers[1], 40, 133, layers[1]);
  68. layers[1]=-layers[1]+255;
  69.  
  70. IplImage *segmentated = &(layers[1].operator IplImage());
  71.  
  72. //Declaring Morph. kernel
  73. cvMorphologyEx(segmentated, segmentated, NULL, morphKernel, CV_MOP_OPEN, 1);
  74. cvMorphologyEx(segmentated, segmentated, NULL, morphKernel, CV_MOP_CLOSE, 2);
  75.  
  76. IplImage *labelImg = cvCreateImage(cvGetSize(img), IPL_DEPTH_LABEL, 1);
  77.  
  78. //Label blobs
  79. cvLabel(segmentated, labelImg, blobs);
  80.  
  81. //Remove bad blobs (too small, too large)
  82. cvFilterByArea(blobs, 300, 50000);
  83. CvBlobs::iterator it=blobs.begin();
  84.  
  85. //Track blobs between frames (to not lose their number)
  86. cvUpdateTracks(blobs, tracks, 20., 5);
  87. cvRenderTracks(tracks, img, img, CV_TRACK_RENDER_ID|CV_TRACK_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE);
  88.  
  89. char buffer[44];
  90. char *str = "/home/boulabiar/Desktop/gestures/gest_1.svg";
  91.  
  92. //Simple calibration code to differentiate between the 2 hands and the head
  93. for (CvBlobs::iterator it=blobs.begin(); it!=blobs.end(); ++it) {
  94. //Get the countour of the current blob
  95. CvContourPolygon *polygon = cvConvertChainCodesToPolygon(&(*it).second->contour);
  96. //Simplify it
  97. CvContourPolygon *sPolygon = cvSimplifyPolygon(polygon, 1);
  98. //ConvexHull
  99. CvContourPolygon *convPoly = cvPolygonContourConvexHull(sPolygon);
  100. if(waitKey(10) == 32) {
  101. sprintf (buffer, "/home/boulabiar/Desktop/gestures/gest_%d.svg", ges);
  102. str=strdup(buffer);
  103. cvWriteContourPolygonSVG( *sPolygon, str, color, color);
  104. ges++;
  105. }
  106.  
  107. delete polygon;
  108. delete sPolygon;
  109. delete convPoly;
  110. } // end blob/blob handling
  111.  
  112. imshow("Cr", segmentated);
  113. imshow("Camera", frame);
  114. cvReleaseBlobs(blobs);
  115. cvReleaseImage(&labelImg);
  116. if(waitKey(30) == 27) break;
  117.  
  118. }
  119. return 0;
  120. }
  121.  
  122. #endif
Add Comment
Please, Sign In to add comment