Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CAPT 1
- #ifdef CAPT
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <cstdio>
- #include "cv.h"
- #include "highgui.h"
- #include "cvBlob/cvblob.h"
- using namespace cv;
- using namespace cvb;
- using namespace std;
- int main(int, char**)
- {
- //ofstream myfile;
- int state=0;
- int calibration=0;
- int go=0;
- int sumB=0, curB=0;
- int mode = -1, move=0;
- VideoCapture cap(0); // open the default camera
- if(!cap.isOpened()) // check if we succeeded
- return -1;
- Mat frame, frameEq;
- Scalar color = CV_RGB(0,128,255);
- cap >> frame;
- namedWindow("Cr",1);
- namedWindow("Camera",0);
- vector<Mat> layers, layers2;
- CvBlobs blobs;
- CvTracks tracks;
- IplConvKernel* morphKernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT, NULL);
- IplImage *img = &(frame.operator IplImage());
- CvSize imgSize = cvGetSize(img);
- //IplImage *mod = cvCreateImage(imgSize, img->depth, img->nChannels);
- Mat handelingFrame;
- CvBlob *head=NULL, *handL=NULL, *handR=NULL ;
- int headN=0, handLN=0, handRN=0;
- int posx=1, posy=1;
- int ges=0;
- for(;;) {
- int nF=0, numberFingers=0, oldnumF=0;
- cap >> frame;
- //Equalize histogram (test code)
- split(frame, layers2);
- equalizeHist(layers2[0],layers2[0]); equalizeHist(layers2[1],layers2[1]); equalizeHist(layers2[2],layers2[2]);
- merge(layers2,frameEq);
- //Color conversation
- cvtColor(frame, handelingFrame, CV_BGR2YCrCb);
- //Skin Color Segmentation.
- split(handelingFrame, layers);
- inRange(layers[1], 40, 133, layers[1]);
- layers[1]=-layers[1]+255;
- IplImage *segmentated = &(layers[1].operator IplImage());
- //Declaring Morph. kernel
- cvMorphologyEx(segmentated, segmentated, NULL, morphKernel, CV_MOP_OPEN, 1);
- cvMorphologyEx(segmentated, segmentated, NULL, morphKernel, CV_MOP_CLOSE, 2);
- IplImage *labelImg = cvCreateImage(cvGetSize(img), IPL_DEPTH_LABEL, 1);
- //Label blobs
- cvLabel(segmentated, labelImg, blobs);
- //Remove bad blobs (too small, too large)
- cvFilterByArea(blobs, 300, 50000);
- CvBlobs::iterator it=blobs.begin();
- //Track blobs between frames (to not lose their number)
- cvUpdateTracks(blobs, tracks, 20., 5);
- cvRenderTracks(tracks, img, img, CV_TRACK_RENDER_ID|CV_TRACK_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE);
- char buffer[44];
- char *str = "/home/boulabiar/Desktop/gestures/gest_1.svg";
- //Simple calibration code to differentiate between the 2 hands and the head
- for (CvBlobs::iterator it=blobs.begin(); it!=blobs.end(); ++it) {
- //Get the countour of the current blob
- CvContourPolygon *polygon = cvConvertChainCodesToPolygon(&(*it).second->contour);
- //Simplify it
- CvContourPolygon *sPolygon = cvSimplifyPolygon(polygon, 1);
- //ConvexHull
- CvContourPolygon *convPoly = cvPolygonContourConvexHull(sPolygon);
- if(waitKey(10) == 32) {
- sprintf (buffer, "/home/boulabiar/Desktop/gestures/gest_%d.svg", ges);
- str=strdup(buffer);
- cvWriteContourPolygonSVG( *sPolygon, str, color, color);
- ges++;
- }
- delete polygon;
- delete sPolygon;
- delete convPoly;
- } // end blob/blob handling
- imshow("Cr", segmentated);
- imshow("Camera", frame);
- cvReleaseBlobs(blobs);
- cvReleaseImage(&labelImg);
- if(waitKey(30) == 27) break;
- }
- return 0;
- }
- #endif
Add Comment
Please, Sign In to add comment