Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //This OpenCV example recognize a color (currently set to red) and make it into a lightsaber (see OpenCV tutorial 0)
- #include <opencv2/opencv.hpp>
- using namespace cv;
- Mat image,blurred,hsv,inRangeOutput,afterCopy,afterMult,white;
- bool get=true;
- void mouse_callBack(int mode,int x,int y,int flags,void* pointer)
- {
- if(mode==1) //click
- {
- Point p(x,y);
- Vec3b v=hsv.at<Vec3b>(y,x);
- std::cout << (int)v[0] << ' ' << (int)v[1] << ' ' << (int)v[2] << std::endl;
- }else if(mode==2)
- {
- get=false;
- }
- }
- int main()
- {
- VideoCapture vc(-1);
- namedWindow("mainWindow");
- setMouseCallback("mainWindow",mouse_callBack);
- Vec3b color;
- int r=0,g=0,b=0;
- color[0]=0;
- color[1]=0;
- color[2]=255;
- /* int hue=163,sat=205,val=95;
- int huee=180,sate=360,vale=360;
- createTrackbar("track1","mainWindow",&hue,180,NULL);
- createTrackbar("track2","mainWindow",&sat,360,NULL);
- createTrackbar("track3","mainWindow",&val,360,NULL);
- createTrackbar("track4","mainWindow",&huee,180,NULL);
- createTrackbar("track5","mainWindow",&sate,360,NULL);
- createTrackbar("track6","mainWindow",&vale,360,NULL);
- */
- createTrackbar("R","mainWindow",&r,255,NULL);
- createTrackbar("G","mainWindow",&g,255,NULL);
- createTrackbar("B","mainWindow",&b,255,NULL);
- while(1)
- {
- color[0]=b;
- color[1]=g;
- color[2]=r;
- if(get)
- vc >> image;
- GaussianBlur(image,blurred,Size(11,11),0,0); //reduce noise
- cvtColor(blurred,hsv,CV_BGR2HSV); //HSV is better to pick color
- // inRange(hsv,Scalar(hue,sat,val),Scalar(huee,sate,vale),inRangeOutput);
- inRange(hsv,Scalar(163,205,95),Scalar(180,360,360),inRangeOutput); //pick red and convert it to a binary mask
- GaussianBlur(inRangeOutput,afterCopy,Size(101,101),0,0); //glow effect
- cvtColor(afterCopy,afterCopy,CV_GRAY2BGR);
- cvtColor(inRangeOutput,white,CV_GRAY2BGR);
- for(int i=0;i<afterCopy.size().height;i++)
- for(int j=0;j<afterCopy.size().width;j++)
- {
- Vec3b& s=afterCopy.at<Vec3b>(i,j);
- s[0]=(s[0]/255.0)*color[0];
- s[1]=(s[1]/255.0)*color[1];
- s[2]=(s[2]/255.0)*color[2];
- }
- image+=(afterCopy*5.0);
- image+=white;
- imshow("mainWindow",image);
- if(waitKey(30)==27)
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement