因为光设里让无人机用摄像头进行导航,背景比较单一,就是往下拍,下面就是一个羽毛球场,都是单调的直线。这里就显示做了一个简单的程序,就是调节参数,把成功检测到的直线记录下来。
环境:Win10+VS2012+opencv2.4.10
代码在这:github,这一部分工作是统一上传的,所以和别的工程放在同一个repository里了,见谅
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <opencv2/opencv.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; using namespace std; void printHelp(){ cout<<"q:quit"<<endl<<"w:open control panel"<<endl<<"s:save the frame"<<endl<<"h:help"<<endl; cout<<"----------------------"<<endl; } int main(){ int save_number=0; char imageFromCamera[20]; char imageAfterCanny[20]; char imageAfterHough[20]; char key; int g_nthreshold=100; int canny1=50; int canny2=200; namedWindow("Control Panel"); cvCreateTrackbar("threshold","Control Panel",&g_nthreshold,200); cvCreateTrackbar("canny1","Control Panel",&canny1,500); cvCreateTrackbar("canny2","Control Panel",&canny2,500); VideoCapture cap(1); Mat frame; Mat frame2; Mat hough; printHelp(); while(1){ cap>>frame2; frame=frame2.clone(); Canny(frame, frame,canny1,canny2,3); imshow("Video after canny",frame); hough=frame2.clone(); vector<Vec4i> mylines; HoughLinesP(frame, mylines, 1, CV_PI/180, g_nthreshold+1, 50, 10 ); for( size_t i = 0; i < mylines.size(); i++ ) { Vec4i l = mylines[i]; line( hough, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 1, CV_AA); } imshow("after hough",hough); save_number++; sprintf(imageFromCamera,"data/Camera%03d.jpg",save_number); sprintf(imageAfterCanny,"data/Canny%03d.jpg",save_number); sprintf(imageAfterHough,"data/Hough%03d.jpg",save_number); imwrite(imageFromCamera,frame2); imwrite(imageAfterCanny,frame); imwrite(imageAfterHough,hough); key = waitKey(10); if(key=='q'){ break; }else if(key=='w'){ namedWindow("Control Panel"); cvCreateTrackbar("threshold","Control Panel",&g_nthreshold,200); cvCreateTrackbar("canny1","Control Panel",&canny1,500); cvCreateTrackbar("canny2","Control Panel",&canny2,500); }else if(key=='h'){ printHelp(); } } } |