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 with Hugh-lines (see OpenCV tutorial 0)
- #include <opencv2/opencv.hpp>
- using namespace cv;
- Mat image,blurred,hsv,inRangeOutput,afterCopy,afterMult,white;
- bool get=true;
- //use it to get HSV value
- 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()
- {
- //get image from webcam
- VideoCapture vc(-1);
- vc.set(CV_CAP_PROP_POS_FRAMES,300);
- 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 size=69;
- bool boo=true;
- int hue=25,sat=101,val=60;
- int huee=180,sate=360,vale=360;
- //uncomment it, if you want to set the HSV values (I recommend doing that for your lighting conditions)
- /* 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("track7","mainWindow",&r,255,NULL);
- createTrackbar("track8","mainWindow",&g,255,NULL);
- createTrackbar("track9","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
- std::vector<Vec4i> lines;
- HoughLinesP(inRangeOutput,lines,1,CV_PI/180.0,250,10,5);
- Mat afterHoughLines(inRangeOutput.rows,inRangeOutput.cols,CV_8UC3,Scalar(0,0,0));
- for(int i=0;i<lines.size();i++)
- {
- line(afterHoughLines,Point(lines[i][0],lines[i][1]),Point(lines[i][2],lines[i][3]),Scalar(255,255,255),8,CV_AA);
- }
- Mat afterHoughLines2=afterHoughLines.clone();
- GaussianBlur(afterHoughLines,afterHoughLines,Size(size,size),0,0); //glow effect
- GaussianBlur(afterHoughLines2,afterHoughLines2,Size(21,21),0,0); //glow effect
- // cvtColor(afterCopy,afterCopy,CV_GRAY2BGR);
- // cvtColor(inRangeOutput,white,CV_GRAY2BGR);
- for(int i=0;i<afterHoughLines.size().height;i++)
- for(int j=0;j<afterHoughLines.size().width;j++)
- {
- Vec3b& s=afterHoughLines.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+=(afterHoughLines*5.0);
- image+=afterHoughLines2;
- imshow("mainWindow",image);
- //vw<<image;
- if(boo)
- {
- size+=8;
- if(size>=151)
- boo=!boo;
- }else{
- size-=8;
- if(size<=69)
- boo=!boo;
- }
- if(waitKey(30)==27)
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement