Advertisement
thecplusplusguy

OpenCV realtime lightsaber

Feb 3rd, 2013
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 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 (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. void mouse_callBack(int mode,int x,int y,int flags,void* pointer)
  9. {
  10.     if(mode==1) //click
  11.     {
  12.         Point p(x,y);
  13.         Vec3b v=hsv.at<Vec3b>(y,x);
  14.         std::cout << (int)v[0] << ' ' << (int)v[1] << ' ' << (int)v[2] << std::endl;
  15.     }else if(mode==2)
  16.     {
  17.         get=false;
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     VideoCapture vc(-1);
  24.     namedWindow("mainWindow");
  25.     setMouseCallback("mainWindow",mouse_callBack);
  26.     Vec3b color;
  27.     int r=0,g=0,b=0;
  28.     color[0]=0;
  29.     color[1]=0;
  30.     color[2]=255;
  31. /*  int hue=163,sat=205,val=95;
  32.     int huee=180,sate=360,vale=360;
  33.     createTrackbar("track1","mainWindow",&hue,180,NULL);
  34.     createTrackbar("track2","mainWindow",&sat,360,NULL);
  35.     createTrackbar("track3","mainWindow",&val,360,NULL);
  36.     createTrackbar("track4","mainWindow",&huee,180,NULL);
  37.     createTrackbar("track5","mainWindow",&sate,360,NULL);
  38.     createTrackbar("track6","mainWindow",&vale,360,NULL);
  39. */
  40.     createTrackbar("R","mainWindow",&r,255,NULL);
  41.     createTrackbar("G","mainWindow",&g,255,NULL);
  42.     createTrackbar("B","mainWindow",&b,255,NULL);
  43.     while(1)
  44.     {
  45.         color[0]=b;
  46.         color[1]=g;
  47.         color[2]=r;
  48.         if(get)
  49.             vc >> image;
  50.         GaussianBlur(image,blurred,Size(11,11),0,0);    //reduce noise
  51.         cvtColor(blurred,hsv,CV_BGR2HSV);   //HSV is better to pick color
  52.  
  53.     //  inRange(hsv,Scalar(hue,sat,val),Scalar(huee,sate,vale),inRangeOutput);
  54.         inRange(hsv,Scalar(163,205,95),Scalar(180,360,360),inRangeOutput);  //pick red and convert it to a binary mask
  55.         GaussianBlur(inRangeOutput,afterCopy,Size(101,101),0,0);    //glow effect
  56.         cvtColor(afterCopy,afterCopy,CV_GRAY2BGR);
  57.         cvtColor(inRangeOutput,white,CV_GRAY2BGR);
  58.         for(int i=0;i<afterCopy.size().height;i++)
  59.             for(int j=0;j<afterCopy.size().width;j++)
  60.             {
  61.                 Vec3b& s=afterCopy.at<Vec3b>(i,j);
  62.                 s[0]=(s[0]/255.0)*color[0];
  63.                 s[1]=(s[1]/255.0)*color[1];
  64.                 s[2]=(s[2]/255.0)*color[2];
  65.             }
  66.         image+=(afterCopy*5.0);
  67.         image+=white;
  68.         imshow("mainWindow",image);
  69.         if(waitKey(30)==27)
  70.             break;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement