OPENCV中OCL的使用
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
StereoBM_OCL bm; StereoBeliefPropagation bp; StereoConstantSpaceBP csbp;
int64 work_begin; double work_fps;
string l_img, r_img; string out_img; enum {BM, BP, CSBP} method; int ndisp; // Max disparity + 1 enum {GPU, CPU} type; };
#pragma comment(lib,"opencv_core2410d.lib") #pragma comment(lib,"opencv_highgui2410d.lib") #pragma comment(lib,"opencv_ocl2410d.lib") #pragma comment(lib,"opencv_imgproc2410d.lib")
// Show results d_disp.download(disp); workEnd();
if (method != BM) {
disp.convertTo(disp, 0); } putText(disp, text(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar::all(255)); imshow("disparity", disp); if(write_once) {
void workBegin() {
work_begin = getTickCount(); } void workEnd() {
int64 d = getTickCount() - work_begin; double f = getTickFrequency(); work_fps = f / d; } string method_str() const { switch (method) {
case BM: return "BM";
case BP: return "BP";
case CSBP: return "CSBP";
} return ""; } string text() const { stringstream ss; ss << "(" << method_str() << ") FPS: " << setiosflags(ios::left)
<< "ndisp: " << ndisp << endl; switch (method) { case BM:
cout << "win_size: " << bm.winSize << endl; cout << "prefilter_sobel: " << bm.preset << endl; break; case BP: cout << "iter_count: " << bp.iters << endl; cout << "level_count: " << bp.levels << endl; break; case CSBP: cout << "iter_count: " << csbp.iters << endl; cout << "level_count: " << csbp.levels << endl; break; } cout << endl; }
CommandLineParser cmd(argc, argv, keys); if (cmd.get<bool>("help")) {
cout << "Available options:" << endl; cmd.printParams(); return 0; }
try {
App app(cmd); cout << "Device name:" << cv::ocl::Context::getContext()->getDeviceInfo().deviceName << endl;
waitKey(0);
// Set common parameters bm.ndisp = ndisp; bp.ndisp = ndisp; csbp.ndisp = ndisp;
cout << endl; printParams();
running = true;
while (running) {
int main(int argc, char** argv) {
const char* keys = "{ h | help | false "{ l | left | "{ r | right | "{ m | method | BM
| print help message }" | specify l百度文库ft image }" | specify right image }"
cvtColor(left_src, left, CV_BGR2GRAY); cvtColor(right_src, right, CV_BGR2GRAY);
d_left.upload(left); d_right.upload(right);
imshow("left", left); imshow("right", right);
cout << "BM doesn't support color images\n"; cvtColor(left_src, left, CV_BGR2GRAY); cvtColor(right_src, right, CV_BGR2GRAY); cout << "image_channels: " << left.channels() << endl; d_left.upload(left); d_right.upload(right); imshow("left", left); imshow("right", right); } bm(d_left, d_right, d_disp); break; case BP: bp(d_left, d_right, d_disp); break; case CSBP: csbp(d_left, d_right, d_disp); break; }
imwrite(out_img, disp); write_once = false;
} handleKey((char)waitKey(3)); } }
void App::printParams() const {
cout << "--- Parameters ---\n"; cout << "image_size: (" << left.cols << ", " << left.rows << ")\n"; cout << "image_channels: " << left.channels() << endl; cout << "method: " << method_str() << endl
using namespace cv; using namespace std; using namespace ocl;
struct App {
App(CommandLineParser& cmd); void run(); void handleKey(char key); void printParams() const;
<< "\t4/r - increase/decrease level count (for BP and CSBP only)\n";
l_img = cmd.get<string>("l"); r_img = cmd.get<string>("r"); string mstr = cmd.get<string>("m"); if(mstr == "BM") method = BM; else if(mstr == "BP") method = BP; else if(mstr == "CSBP") method = CSBP; else cout << "unknown method!\n"; ndisp = cmd.get<int>("n"); out_img = cmd.get<string>("o"); write_once = false; }
| specify match
method(BM/BP/CSBP) }" "{ n | ndisp | 64 "{ o | output | stereo_match_output.jpg
images}";
| specify number of disparity levels }" | specify output path when input is
// ocl_stereo_match.cpp : 定义控制台应用程序的入口点。 //
#include "stdafx.h"
#include <iostream> #include <string> #include <sstream> #include <iomanip> #include <stdexcept> #include "opencv2/ocl/ocl.hpp" #include "opencv2/highgui/highgui.hpp"
app.run(); getchar(); } catch (const exception& e) { cout << "error: " << e.what() << endl; }
return EXIT_SUCCESS; }
App::App(CommandLineParser& cmd) : running(false),method(BM)
{ cout << "stereo_match_ocl sample\n"; cout << "\nControls:\n" << "\tesc - exit\n" << "\to - save output image once\n" << "\tp - print current parameters\n" << "\tg - convert source images into gray\n" << "\tm - change stereo match method\n" << "\ts - change Sobel prefiltering flag (for BM only)\n" << "\t1/q - increase/decrease maximum disparity\n" << "\t2/w - increase/decrease window size (for BM only)\n" << "\t3/e - increase/decrease iteration count (for BP and CSBP only)\n"
<< setprecision(4) << work_fps; return ss.str(); } private: bool running, write_once;
Mat left_src, right_src; Mat left, right; oclMat d_left, d_right;
// Prepare disparity map of specified type Mat disp; oclMat d_disp; workBegin(); switch (method) { case BM:
if (d_left.channels() > 1 || d_right.channels() > 1) {
void App::run() {
// Load images cout<<l_img; left_src = imread(l_img,1);//cvLoadImage(l_img.c_str());// right_src = imread(r_img,1);//cvLoadImage(r_img.c_str());// if (left_src.empty()) throw runtime_error("can't open file \"" + l_img + "\""); if (right_src.empty()) throw runtime_error("can't open file \"" + r_img + "\"");