From 4d86e2140d0b3966537614a85f7a4f909bd0025e Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Mon, 21 Oct 2013 20:47:55 +0400 Subject: [PATCH] OpenCL examples refactoring --- samples/ocl/adaptive_bilateral_filter.cpp | 31 +++++++++------- samples/ocl/bgfg_segm.cpp | 25 +++++-------- samples/ocl/clahe.cpp | 60 ++++++++++++++++--------------- samples/ocl/facedetect.cpp | 47 +++++++++--------------- samples/ocl/hog.cpp | 10 +++++- samples/ocl/pyrlk_optical_flow.cpp | 20 +++++------ samples/ocl/squares.cpp | 22 +++++++----- samples/ocl/stereo_match.cpp | 8 +++-- samples/ocl/surf_matcher.cpp | 34 ++++++++---------- samples/ocl/tvl1_optical_flow.cpp | 33 ++++------------- 10 files changed, 130 insertions(+), 160 deletions(-) diff --git a/samples/ocl/adaptive_bilateral_filter.cpp b/samples/ocl/adaptive_bilateral_filter.cpp index d3d2521..b8ad3ed 100644 --- a/samples/ocl/adaptive_bilateral_filter.cpp +++ b/samples/ocl/adaptive_bilateral_filter.cpp @@ -12,17 +12,27 @@ int main( int argc, const char** argv ) { const char* keys = "{ i | input | | specify input image }" - "{ k | ksize | 5 | specify kernel size }"; + "{ k | ksize | 5 | specify kernel size }" + "{ h | help | false | print help message }"; + CommandLineParser cmd(argc, argv, keys); + if (cmd.get("help")) + { + cout << "Usage : adaptive_bilateral_filter [options]" << endl; + cout << "Available options:" << endl; + cmd.printParams(); + return EXIT_SUCCESS; + } + string src_path = cmd.get("i"); int ks = cmd.get("k"); const char * winName[] = {"input", "adaptive bilateral CPU", "adaptive bilateral OpenCL", "bilateralFilter OpenCL"}; - Mat src = imread(src_path); - Mat abFilterCPU; - if(src.empty()){ - //cout << "error read image: " << src_path << endl; - return -1; + Mat src = imread(src_path), abFilterCPU; + if (src.empty()) + { + cout << "error read image: " << src_path << endl; + return EXIT_FAILURE; } ocl::oclMat dsrc(src), dABFilter, dBFilter; @@ -32,17 +42,12 @@ int main( int argc, const char** argv ) ocl::adaptiveBilateralFilter(dsrc, dABFilter, ksize, 10); ocl::bilateralFilter(dsrc, dBFilter, ks, 30, 9); - Mat abFilter = dABFilter; - Mat bFilter = dBFilter; + Mat abFilter = dABFilter, bFilter = dBFilter; imshow(winName[0], src); - imshow(winName[1], abFilterCPU); - imshow(winName[2], abFilter); - imshow(winName[3], bFilter); - waitKey(); - return 0; + return EXIT_SUCCESS; } diff --git a/samples/ocl/bgfg_segm.cpp b/samples/ocl/bgfg_segm.cpp index 589a349..997be05 100644 --- a/samples/ocl/bgfg_segm.cpp +++ b/samples/ocl/bgfg_segm.cpp @@ -14,7 +14,6 @@ using namespace cv::ocl; int main(int argc, const char** argv) { - cv::CommandLineParser cmd(argc, argv, "{ c | camera | false | use camera }" "{ f | file | 768x576.avi | input video file }" @@ -26,7 +25,7 @@ int main(int argc, const char** argv) cout << "Usage : bgfg_segm [options]" << endl; cout << "Available options:" << endl; cmd.printParams(); - return 0; + return EXIT_SUCCESS; } bool useCamera = cmd.get("camera"); @@ -36,13 +35,12 @@ int main(int argc, const char** argv) if (method != "mog" && method != "mog2") { cerr << "Incorrect method" << endl; - return -1; + return EXIT_FAILURE; } int m = method == "mog" ? M_MOG : M_MOG2; VideoCapture cap; - if (useCamera) cap.open(0); else @@ -50,8 +48,8 @@ int main(int argc, const char** argv) if (!cap.isOpened()) { - cerr << "can not open camera or video file" << endl; - return -1; + cout << "can not open camera or video file" << endl; + return EXIT_FAILURE; } Mat frame; @@ -62,15 +60,11 @@ int main(int argc, const char** argv) cv::ocl::MOG mog; cv::ocl::MOG2 mog2; - oclMat d_fgmask; - oclMat d_fgimg; - oclMat d_bgimg; + oclMat d_fgmask, d_fgimg, d_bgimg; d_fgimg.create(d_frame.size(), d_frame.type()); - Mat fgmask; - Mat fgimg; - Mat bgimg; + Mat fgmask, fgimg, bgimg; switch (m) { @@ -83,7 +77,7 @@ int main(int argc, const char** argv) break; } - for(;;) + for (;;) { cap >> frame; if (frame.empty()) @@ -123,10 +117,9 @@ int main(int argc, const char** argv) if (!bgimg.empty()) imshow("mean background image", bgimg); - int key = waitKey(30); - if (key == 27) + if (27 == waitKey(30)) break; } - return 0; + return EXIT_SUCCESS; } diff --git a/samples/ocl/clahe.cpp b/samples/ocl/clahe.cpp index 5dc2075..558a0d1 100644 --- a/samples/ocl/clahe.cpp +++ b/samples/ocl/clahe.cpp @@ -9,15 +9,13 @@ using namespace std; Ptr pFilter; int tilesize; int cliplimit; -string outfile; static void TSize_Callback(int pos) { if(pos==0) - { pFilter->setTilesGridSize(Size(1,1)); - } - pFilter->setTilesGridSize(Size(tilesize,tilesize)); + else + pFilter->setTilesGridSize(Size(tilesize,tilesize)); } static void Clip_Callback(int) @@ -31,63 +29,64 @@ int main(int argc, char** argv) "{ i | input | | specify input image }" "{ c | camera | 0 | specify camera id }" "{ s | use_cpu | false | use cpu algorithm }" - "{ o | output | clahe_output.jpg | specify output save path}"; + "{ o | output | clahe_output.jpg | specify output save path}" + "{ h | help | false | print help message }"; + + cv::CommandLineParser cmd(argc, argv, keys); + if (cmd.get("help")) + { + cout << "Usage : clahe [options]" << endl; + cout << "Available options:" << endl; + cmd.printParams(); + return EXIT_SUCCESS; + } - CommandLineParser cmd(argc, argv, keys); - string infile = cmd.get("i"); - outfile = cmd.get("o"); + string infile = cmd.get("i"), outfile = cmd.get("o"); int camid = cmd.get("c"); bool use_cpu = cmd.get("s"); CvCapture* capture = 0; - bool running = true; namedWindow("CLAHE"); createTrackbar("Tile Size", "CLAHE", &tilesize, 32, (TrackbarCallback)TSize_Callback); createTrackbar("Clip Limit", "CLAHE", &cliplimit, 20, (TrackbarCallback)Clip_Callback); Mat frame, outframe; - ocl::oclMat d_outframe; + ocl::oclMat d_outframe, d_frame; int cur_clip; Size cur_tilesize; - if(use_cpu) - { - pFilter = createCLAHE(); - } - else - { - pFilter = ocl::createCLAHE(); - } + pFilter = use_cpu ? createCLAHE() : ocl::createCLAHE(); + cur_clip = (int)pFilter->getClipLimit(); cur_tilesize = pFilter->getTilesGridSize(); setTrackbarPos("Tile Size", "CLAHE", cur_tilesize.width); setTrackbarPos("Clip Limit", "CLAHE", cur_clip); + if(infile != "") { frame = imread(infile); if(frame.empty()) { cout << "error read image: " << infile << endl; - return -1; + return EXIT_FAILURE; } } else - { capture = cvCaptureFromCAM(camid); - } + cout << "\nControls:\n" << "\to - save output image\n" << "\tESC - exit\n"; - while(running) + + for (;;) { if(capture) frame = cvQueryFrame(capture); else frame = imread(infile); if(frame.empty()) - { continue; - } + if(use_cpu) { cvtColor(frame, frame, COLOR_BGR2GRAY); @@ -95,15 +94,18 @@ int main(int argc, char** argv) } else { - ocl::oclMat d_frame(frame); - ocl::cvtColor(d_frame, d_outframe, COLOR_BGR2GRAY); + ocl::cvtColor(d_frame = frame, d_outframe, COLOR_BGR2GRAY); pFilter->apply(d_outframe, d_outframe); d_outframe.download(outframe); } + imshow("CLAHE", outframe); + char key = (char)cvWaitKey(3); - if(key == 'o') imwrite(outfile, outframe); - else if(key == 27) running = false; + if(key == 'o') + imwrite(outfile, outframe); + else if(key == 27) + break; } - return 0; + return EXIT_SUCCESS; } diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index be61b79..d20c937 100644 --- a/samples/ocl/facedetect.cpp +++ b/samples/ocl/facedetect.cpp @@ -28,27 +28,29 @@ static void workBegin() { work_begin = getTickCount(); } + static void workEnd() { work_end += (getTickCount() - work_begin); } + static double getTime() { return work_end /((double)cvGetTickFrequency() * 1000.); } -void detect( Mat& img, vector& faces, +static void detect( Mat& img, vector& faces, ocl::OclCascadeClassifierBuf& cascade, double scale, bool calTime); -void detectCPU( Mat& img, vector& faces, +static void detectCPU( Mat& img, vector& faces, CascadeClassifier& cascade, double scale, bool calTime); -void Draw(Mat& img, vector& faces, double scale); +static void Draw(Mat& img, vector& faces, double scale); // This function test if gpu_rst matches cpu_rst. @@ -56,7 +58,6 @@ void Draw(Mat& img, vector& faces, double scale); // Else if will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels) double checkRectSimilarity(Size sz, vector& cpu_rst, vector& gpu_rst); - int main( int argc, const char** argv ) { const char* keys = @@ -72,10 +73,12 @@ int main( int argc, const char** argv ) CommandLineParser cmd(argc, argv, keys); if (cmd.get("help")) { + cout << "Usage : facedetect [options]" << endl; cout << "Available options:" << endl; cmd.printParams(); - return 0; + return EXIT_SUCCESS; } + CvCapture* capture = 0; Mat frame, frameCopy, image; @@ -89,8 +92,8 @@ int main( int argc, const char** argv ) if( !cascade.load( cascadeName ) || !cpu_cascade.load(cascadeName) ) { - cerr << "ERROR: Could not load classifier cascade" << endl; - return -1; + cout << "ERROR: Could not load classifier cascade" << endl; + return EXIT_FAILURE; } if( inputName.empty() ) @@ -99,25 +102,17 @@ int main( int argc, const char** argv ) if(!capture) cout << "Capture from CAM 0 didn't work" << endl; } - else if( inputName.size() ) + else { - image = imread( inputName, 1 ); + image = imread( inputName, CV_LOAD_IMAGE_COLOR ); if( image.empty() ) { capture = cvCaptureFromAVI( inputName.c_str() ); if(!capture) cout << "Capture from AVI didn't work" << endl; - return -1; + return EXIT_FAILURE; } } - else - { - image = imread( "lena.jpg", 1 ); - if(image.empty()) - cout << "Couldn't read lena.jpg" << endl; - return -1; - } - cvNamedWindow( "result", 1 ); if( capture ) @@ -134,24 +129,16 @@ int main( int argc, const char** argv ) frame.copyTo( frameCopy ); else flip( frame, frameCopy, 0 ); + if(useCPU) - { detectCPU(frameCopy, faces, cpu_cascade, scale, false); - } else - { detect(frameCopy, faces, cascade, scale, false); - } + Draw(frameCopy, faces, scale); if( waitKey( 10 ) >= 0 ) - goto _cleanup_; + break; } - - - waitKey(0); - - -_cleanup_: cvReleaseCapture( &capture ); } else @@ -164,9 +151,7 @@ _cleanup_: { cout << "loop" << i << endl; if(useCPU) - { detectCPU(image, faces, cpu_cascade, scale, i==0?false:true); - } else { detect(image, faces, cascade, scale, i==0?false:true); diff --git a/samples/ocl/hog.cpp b/samples/ocl/hog.cpp index 89c8dff..28f0cc1 100644 --- a/samples/ocl/hog.cpp +++ b/samples/ocl/hog.cpp @@ -72,6 +72,14 @@ int main(int argc, char** argv) "{ l |larger_win| false | use 64x128 window}" "{ o | output | | specify output path when input is images}"; CommandLineParser cmd(argc, argv, keys); + if (cmd.get("help")) + { + cout << "Usage : hog [options]" << endl; + cout << "Available options:" << endl; + cmd.printParams(); + return EXIT_SUCCESS; + } + App app(cmd); try { @@ -89,7 +97,7 @@ int main(int argc, char** argv) { return cout << "unknown exception" << endl, 1; } - return 0; + return EXIT_SUCCESS; } App::App(CommandLineParser& cmd) diff --git a/samples/ocl/pyrlk_optical_flow.cpp b/samples/ocl/pyrlk_optical_flow.cpp index 5a59803..c3648ef 100644 --- a/samples/ocl/pyrlk_optical_flow.cpp +++ b/samples/ocl/pyrlk_optical_flow.cpp @@ -44,7 +44,8 @@ static void download(const oclMat& d_mat, vector& vec) d_mat.download(mat); } -static void drawArrows(Mat& frame, const vector& prevPts, const vector& nextPts, const vector& status, Scalar line_color = Scalar(0, 0, 255)) +static void drawArrows(Mat& frame, const vector& prevPts, const vector& nextPts, const vector& status, + Scalar line_color = Scalar(0, 0, 255)) { for (size_t i = 0; i < prevPts.size(); ++i) { @@ -104,7 +105,7 @@ int main(int argc, const char* argv[]) cout << "Usage: pyrlk_optical_flow [options]" << endl; cout << "Available options:" << endl; cmd.printParams(); - return 0; + return EXIT_SUCCESS; } bool defaultPicturesFail = false; @@ -136,7 +137,7 @@ int main(int argc, const char* argv[]) Mat frame0Gray, frame1Gray; Mat ptr0, ptr1; - if(vdofile == "") + if(vdofile.empty()) capture = cvCaptureFromCAM( inputName ); else capture = cvCreateFileCapture(vdofile.c_str()); @@ -144,14 +145,12 @@ int main(int argc, const char* argv[]) int c = inputName ; if(!capture) { - if(vdofile == "") + if(vdofile.empty()) cout << "Capture from CAM " << c << " didn't work" << endl; else cout << "Capture from file " << vdofile << " failed" <= 0 ) - goto _cleanup_; + break; } - waitKey(0); - -_cleanup_: cvReleaseCapture( &capture ); } else @@ -264,5 +260,5 @@ nocamera: waitKey(); - return 0; + return EXIT_SUCCESS; } diff --git a/samples/ocl/squares.cpp b/samples/ocl/squares.cpp index 9e70924..bd1c3c3 100644 --- a/samples/ocl/squares.cpp +++ b/samples/ocl/squares.cpp @@ -13,9 +13,9 @@ using namespace cv; using namespace std; -#define ACCURACY_CHECK 1 +#define ACCURACY_CHECK -#if ACCURACY_CHECK +#ifdef ACCURACY_CHECK // check if two vectors of vector of points are near or not // prior assumption is that they are in correct order static bool checkPoints( @@ -278,27 +278,31 @@ int main(int argc, char** argv) { const char* keys = "{ i | input | | specify input image }" - "{ o | output | squares_output.jpg | specify output save path}"; + "{ o | output | squares_output.jpg | specify output save path}" + "{ h | help | false | print help message }"; CommandLineParser cmd(argc, argv, keys); string inputName = cmd.get("i"); string outfile = cmd.get("o"); - if(inputName.empty()) + + if(cmd.get("help")) { + cout << "Usage : squares [options]" << endl; cout << "Available options:" << endl; cmd.printParams(); - return 0; + return EXIT_SUCCESS; } int iterations = 10; - namedWindow( wndname, 1 ); + namedWindow( wndname, CV_LOAD_IMAGE_COLOR ); vector > squares_cpu, squares_ocl; Mat image = imread(inputName, 1); if( image.empty() ) { cout << "Couldn't load " << inputName << endl; - return -1; + return EXIT_FAILURE; } + int j = iterations; int64 t_ocl = 0, t_cpp = 0; //warm-ups @@ -307,7 +311,7 @@ int main(int argc, char** argv) findSquares_ocl(image, squares_ocl); -#if ACCURACY_CHECK +#ifdef ACCURACY_CHECK cout << "Checking ocl accuracy ... " << endl; cout << (checkPoints(squares_cpu, squares_ocl) ? "Pass" : "Failed") << endl; #endif @@ -332,5 +336,5 @@ int main(int argc, char** argv) imwrite(outfile, result); cvWaitKey(0); - return 0; + return EXIT_SUCCESS; } diff --git a/samples/ocl/stereo_match.cpp b/samples/ocl/stereo_match.cpp index 86d60d4..7a5c105 100644 --- a/samples/ocl/stereo_match.cpp +++ b/samples/ocl/stereo_match.cpp @@ -76,8 +76,9 @@ int main(int argc, char** argv) "{ l | left | | specify left image }" "{ r | right | | specify right image }" "{ m | method | BM | specify match method(BM/BP/CSBP) }" - "{ n | ndisp | 64 | specify number of disparity levels }" + "{ n | ndisp | 64 | specify number of disparity levels }" "{ o | output | stereo_match_output.jpg | specify output path when input is images}"; + CommandLineParser cmd(argc, argv, keys); if (cmd.get("help")) { @@ -85,6 +86,7 @@ int main(int argc, char** argv) cmd.printParams(); return 0; } + try { App app(cmd); @@ -96,7 +98,8 @@ int main(int argc, char** argv) { cout << "error: " << e.what() << endl; } - return 0; + + return EXIT_SUCCESS; } App::App(CommandLineParser& cmd) @@ -114,6 +117,7 @@ App::App(CommandLineParser& cmd) << "\t2/w - increase/decrease window size (for BM only)\n" << "\t3/e - increase/decrease iteration count (for BP and CSBP only)\n" << "\t4/r - increase/decrease level count (for BP and CSBP only)\n"; + l_img = cmd.get("l"); r_img = cmd.get("r"); string mstr = cmd.get("m"); diff --git a/samples/ocl/surf_matcher.cpp b/samples/ocl/surf_matcher.cpp index 4d73323..6023de9 100644 --- a/samples/ocl/surf_matcher.cpp +++ b/samples/ocl/surf_matcher.cpp @@ -14,21 +14,20 @@ const int LOOP_NUM = 10; const int GOOD_PTS_MAX = 50; const float GOOD_PORTION = 0.15f; -namespace -{ - int64 work_begin = 0; int64 work_end = 0; -void workBegin() +static void workBegin() { work_begin = getTickCount(); } -void workEnd() + +static void workEnd() { work_end = getTickCount() - work_begin; } -double getTime() + +static double getTime() { return work_end /((double)cvGetTickFrequency() * 1000.); } @@ -59,7 +58,7 @@ struct SURFMatcher } }; -Mat drawGoodMatches( +static Mat drawGoodMatches( const Mat& cpu_img1, const Mat& cpu_img2, const vector& keypoints1, @@ -129,7 +128,6 @@ Mat drawGoodMatches( return img_matches; } -} //////////////////////////////////////////////////// // This program demonstrates the usage of SURF_OCL. // use cpu findHomography interface to calculate the transformation matrix @@ -142,12 +140,14 @@ int main(int argc, char* argv[]) "{ o | output | SURF_output.jpg | specify output save path (only works in CPU or GPU only mode) }" "{ c | use_cpu | false | use CPU algorithms }" "{ a | use_all | false | use both CPU and GPU algorithms}"; + CommandLineParser cmd(argc, argv, keys); if (cmd.get("help")) { + std::cout << "Usage: surf_matcher [options]" << std::endl; std::cout << "Available options:" << std::endl; cmd.printParams(); - return 0; + return EXIT_SUCCESS; } Mat cpu_img1, cpu_img2, cpu_img1_grey, cpu_img2_grey; @@ -168,23 +168,17 @@ int main(int argc, char* argv[]) cvtColor(cpu_img2, cpu_img2_grey, CV_BGR2GRAY); img2 = cpu_img2_grey; - if(useALL) - { - useCPU = false; - useGPU = false; - } - else if(useCPU==false && useALL==false) - { + if (useALL) + useCPU = useGPU = false; + else if(!useCPU && !useALL) useGPU = true; - } if(!useCPU) - { std::cout << "Device name:" << cv::ocl::Context::getContext()->getDeviceInfo().deviceName << std::endl; - } + double surf_time = 0.; //declare input/output @@ -330,5 +324,5 @@ int main(int argc, char* argv[]) imshow("ocl surf matches", ocl_img_matches); } waitKey(0); - return 0; + return EXIT_SUCCESS; } diff --git a/samples/ocl/tvl1_optical_flow.cpp b/samples/ocl/tvl1_optical_flow.cpp index 296dc69..fabfa9a 100644 --- a/samples/ocl/tvl1_optical_flow.cpp +++ b/samples/ocl/tvl1_optical_flow.cpp @@ -96,10 +96,9 @@ int main(int argc, const char* argv[]) cout << "Usage: pyrlk_optical_flow [options]" << endl; cout << "Available options:" << endl; cmd.printParams(); - return 0; + return EXIT_SUCCESS; } - bool defaultPicturesFail = false; string fname0 = cmd.get("l"); string fname1 = cmd.get("r"); string vdofile = cmd.get("v"); @@ -113,22 +112,10 @@ int main(int argc, const char* argv[]) cv::Ptr alg = cv::createOptFlow_DualTVL1(); cv::ocl::OpticalFlowDual_TVL1_OCL d_alg; - Mat flow, show_flow; Mat flow_vec[2]; if (frame0.empty() || frame1.empty()) - { useCamera = true; - defaultPicturesFail = true; - CvCapture* capture = 0; - capture = cvCaptureFromCAM( inputName ); - if (!capture) - { - cout << "Can't load input images" << endl; - return -1; - } - } - if (useCamera) { @@ -137,22 +124,17 @@ int main(int argc, const char* argv[]) Mat frame0Gray, frame1Gray; Mat ptr0, ptr1; - if(vdofile == "") + if(vdofile.empty()) capture = cvCaptureFromCAM( inputName ); else capture = cvCreateFileCapture(vdofile.c_str()); - int c = inputName ; if(!capture) { - if(vdofile == "") - cout << "Capture from CAM " << c << " didn't work" << endl; + if(vdofile.empty()) + cout << "Capture from CAM " << inputName << " didn't work" << endl; else cout << "Capture from file " << vdofile << " failed" <= 0 ) - goto _cleanup_; + break; } - waitKey(0); - -_cleanup_: cvReleaseCapture( &capture ); } else @@ -254,5 +233,5 @@ nocamera: waitKey(); - return 0; + return EXIT_SUCCESS; } -- 2.7.4