Advertisement
thecplusplusguy

OpenCV realtime lightsaber with Houh lines

Feb 3rd, 2013
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //This OpenCV example recognize a color (currently set to red) and make it into a lightsaber with Hugh-lines (see OpenCV tutorial 0)
  3. #include <opencv2/opencv.hpp>
  4. using namespace cv;
  5.  
  6.     Mat image,blurred,hsv,inRangeOutput,afterCopy,afterMult,white;
  7.     bool get=true;
  8.    
  9.     //use it to get HSV value
  10. void mouse_callBack(int mode,int x,int y,int flags,void* pointer)
  11. {
  12.     if(mode==1) //click
  13.     {
  14.         Point p(x,y);
  15.         Vec3b v=hsv.at<Vec3b>(y,x);
  16.         std::cout << (int)v[0] << ' ' << (int)v[1] << ' ' << (int)v[2] << std::endl;
  17.     }else if(mode==2)
  18.     {
  19.         get=false;
  20.     }
  21. }
  22.  
  23. int main()
  24. {
  25.     //get image from webcam
  26.     VideoCapture vc(-1);
  27.     vc.set(CV_CAP_PROP_POS_FRAMES,300);
  28.     namedWindow("mainWindow");
  29.     setMouseCallback("mainWindow",mouse_callBack);
  30.     Vec3b color;
  31.     int r=0,g=0,b=0;
  32.     color[0]=0;
  33.     color[1]=0;
  34.     color[2]=255;
  35.     int size=69;
  36.     bool boo=true;
  37.     int hue=25,sat=101,val=60;
  38.     int huee=180,sate=360,vale=360;
  39.     //uncomment it, if you want to set the HSV values (I recommend doing that for your lighting conditions)
  40. /*  createTrackbar("track1","mainWindow",&hue,180,NULL);
  41.     createTrackbar("track2","mainWindow",&sat,360,NULL);
  42.     createTrackbar("track3","mainWindow",&val,360,NULL);
  43.     createTrackbar("track4","mainWindow",&huee,180,NULL);
  44.     createTrackbar("track5","mainWindow",&sate,360,NULL);
  45.     createTrackbar("track6","mainWindow",&vale,360,NULL);*/
  46.  
  47.     createTrackbar("track7","mainWindow",&r,255,NULL);
  48.     createTrackbar("track8","mainWindow",&g,255,NULL);
  49.     createTrackbar("track9","mainWindow",&b,255,NULL);
  50.     while(1)
  51.     {
  52.         color[0]=b;
  53.         color[1]=g;
  54.         color[2]=r;
  55.         if(get)
  56.             vc >> image;
  57.         GaussianBlur(image,blurred,Size(11,11),0,0);    //reduce noise
  58.         cvtColor(blurred,hsv,CV_BGR2HSV);   //HSV is better to pick color
  59.  
  60.         inRange(hsv,Scalar(hue,sat,val),Scalar(huee,sate,vale),inRangeOutput);
  61.     //  inRange(hsv,Scalar(163,205,95),Scalar(180,360,360),inRangeOutput);  //pick red and convert it to a binary mask
  62.         std::vector<Vec4i> lines;
  63.         HoughLinesP(inRangeOutput,lines,1,CV_PI/180.0,250,10,5);
  64.         Mat afterHoughLines(inRangeOutput.rows,inRangeOutput.cols,CV_8UC3,Scalar(0,0,0));
  65.         for(int i=0;i<lines.size();i++)
  66.         {
  67.             line(afterHoughLines,Point(lines[i][0],lines[i][1]),Point(lines[i][2],lines[i][3]),Scalar(255,255,255),8,CV_AA);
  68.         }
  69.         Mat afterHoughLines2=afterHoughLines.clone();
  70.  
  71.         GaussianBlur(afterHoughLines,afterHoughLines,Size(size,size),0,0);  //glow effect
  72.         GaussianBlur(afterHoughLines2,afterHoughLines2,Size(21,21),0,0);    //glow effect
  73.     //  cvtColor(afterCopy,afterCopy,CV_GRAY2BGR);
  74.     //  cvtColor(inRangeOutput,white,CV_GRAY2BGR);
  75.         for(int i=0;i<afterHoughLines.size().height;i++)
  76.             for(int j=0;j<afterHoughLines.size().width;j++)
  77.             {
  78.                 Vec3b& s=afterHoughLines.at<Vec3b>(i,j);
  79.                 s[0]=(s[0]/255.0)*color[0];
  80.                 s[1]=(s[1]/255.0)*color[1];
  81.                 s[2]=(s[2]/255.0)*color[2];
  82.             }
  83.         image+=(afterHoughLines*5.0);
  84.         image+=afterHoughLines2;
  85.         imshow("mainWindow",image);
  86.         //vw<<image;
  87.         if(boo)
  88.         {
  89.             size+=8;
  90.             if(size>=151)
  91.                 boo=!boo;
  92.         }else{
  93.             size-=8;
  94.             if(size<=69)
  95.                 boo=!boo;
  96.         }
  97.  
  98.  
  99.         if(waitKey(30)==27)
  100.             break;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement