From 71ca501bb6f0912b32b2e7401ed851cb929432a1 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Wed, 18 May 2011 13:21:45 +0000 Subject: [PATCH] added parameters controlling resolution into opencv_stitching --- modules/stitching/main.cpp | 96 ++++++++++++++++++++++++++++++---- modules/stitching/matchers.cpp | 8 +-- modules/stitching/matchers.hpp | 4 +- 3 files changed, 92 insertions(+), 16 deletions(-) diff --git a/modules/stitching/main.cpp b/modules/stitching/main.cpp index 491650344..83d01a659 100644 --- a/modules/stitching/main.cpp +++ b/modules/stitching/main.cpp @@ -13,6 +13,9 @@ void printUsage() { cout << "Rotation model images stitcher.\n\n"; cout << "Usage: opencv_stitching img1 img2 [...imgN]\n" + << "\t[--trygpu (yes|no)]\n" + << "\t[--work_megapix ]\n" + << "\t[--compose_megapix ]\n" << "\t[--matchconf ]\n" << "\t[--ba (ray|focal_ray)]\n" << "\t[--conf_thresh ]\n" @@ -23,16 +26,17 @@ void printUsage() << "\t[--output ]\n\n"; cout << "--matchconf\n" << "\tGood values are in [0.2, 0.8] range usually.\n\n"; - cout << "--conf_thresh\n" - << "\tGood values are in [0.3, 1.0] range usually.\n"; } int main(int argc, char* argv[]) { cv::setBreakOnError(true); + vector img_names; vector images; - string result_name = "result.png"; + bool trygpu = true; + double work_megapix = -1; + double compose_megapix = -1; int ba_space = BundleAdjuster::FOCAL_RAY_SPACE; float conf_thresh = 1.f; bool wave_correct = true; @@ -41,6 +45,10 @@ int main(int argc, char* argv[]) float match_conf = 0.55f; int seam_find_type = SeamFinder::VORONOI; int blend_type = Blender::MULTI_BAND; + string result_name = "result.png"; + + double work_scale = 1, compose_scale = 1; + bool is_work_scale_set = false, is_compose_scale_set = true; if (argc == 1) { @@ -50,7 +58,37 @@ int main(int argc, char* argv[]) for (int i = 1; i < argc; ++i) { - if (string(argv[i]) == "--result") + if (string(argv[i]) == "--work_megapix") + { + work_megapix = atof(argv[i + 1]); + break; + } + } + + for (int i = 1; i < argc; ++i) + { + if (string(argv[i]) == "--trygpu") + { + if (string(argv[i + 1]) == "no") + trygpu = false; + else if (string(argv[i + 1]) == "yes") + trygpu = true; + else + { + cout << "Bad --trygpu flag value\n"; + return -1; + } + i++; + } + else if (string(argv[i]) == "--work_megapix") + i++; + else if (string(argv[i]) == "--compose_megapix") + { + compose_megapix = atof(argv[i + 1]); + is_compose_scale_set = false; + i++; + } + else if (string(argv[i]) == "--result") { result_name = argv[i + 1]; i++; @@ -87,7 +125,7 @@ int main(int argc, char* argv[]) wave_correct = true; else { - cout << "Bad wave correct flag value\n"; + cout << "Bad --wavecorrect flag value\n"; return -1; } i++; @@ -144,13 +182,26 @@ int main(int argc, char* argv[]) } else { - Mat img = imread(argv[i]); - if (img.empty()) + img_names.push_back(argv[i]); + Mat full_img = imread(argv[i]); + if (full_img.empty()) { cout << "Can't open image " << argv[i] << endl; return -1; } - images.push_back(img); + if (work_megapix < 0) + images.push_back(full_img); + else + { + if (!is_work_scale_set) + { + work_scale = min(1.0, sqrt(work_megapix * 1000000 / full_img.size().area())); + is_work_scale_set = true; + } + Mat img; + resize(full_img, img, Size(), work_scale, work_scale); + images.push_back(img); + } } } @@ -163,18 +214,24 @@ int main(int argc, char* argv[]) LOGLN("Finding features..."); vector features; - SurfFeaturesFinder finder; + SurfFeaturesFinder finder(trygpu); finder(images, features); LOGLN("Pairwise matching..."); vector pairwise_matches; - BestOf2NearestMatcher matcher; + BestOf2NearestMatcher matcher(trygpu); if (user_match_conf) matcher = BestOf2NearestMatcher(true, match_conf); matcher(images, features, pairwise_matches); leaveBiggestComponent(images, features, pairwise_matches, conf_thresh); + num_images = static_cast(images.size()); + if (num_images < 2) + { + cout << "Need more images\n"; + return -1; + } LOGLN("Estimating rotations..."); HomographyBasedEstimator estimator; @@ -214,6 +271,24 @@ int main(int argc, char* argv[]) nth_element(focals.begin(), focals.end(), focals.begin() + focals.size() / 2); float camera_focal = static_cast(focals[focals.size() / 2]); + if (work_megapix > 0 || compose_megapix > 0) + { + for (int i = 0; i < num_images; ++i) + { + Mat full_img = imread(img_names[i]); + if (!is_compose_scale_set) + { + compose_scale = min(1.0, sqrt(compose_megapix * 1000000 / full_img.size().area())); + is_compose_scale_set = true; + } + Mat img; + resize(full_img, img, Size(), compose_scale, compose_scale); + images[i] = img; + cameras[i].focal *= compose_scale / work_scale; + } + camera_focal *= static_cast(compose_scale / work_scale); + } + vector masks(num_images); for (int i = 0; i < num_images; ++i) { @@ -250,3 +325,4 @@ int main(int argc, char* argv[]) LOGLN("Finished"); return 0; } + diff --git a/modules/stitching/matchers.cpp b/modules/stitching/matchers.cpp index b17ea4540..0aaa16389 100644 --- a/modules/stitching/matchers.cpp +++ b/modules/stitching/matchers.cpp @@ -105,10 +105,10 @@ namespace } } -SurfFeaturesFinder::SurfFeaturesFinder(bool gpu_hint, double hess_thresh, int num_octaves, int num_layers, +SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int num_octaves, int num_layers, int num_octaves_descr, int num_layers_descr) { - if (gpu_hint && getCudaEnabledDeviceCount() > 0) + if (try_use_gpu && getCudaEnabledDeviceCount() > 0) impl_ = new GpuSurfFeaturesFinder(hess_thresh, num_octaves, num_layers, num_octaves_descr, num_layers_descr); else impl_ = new CpuSurfFeaturesFinder(hess_thresh, num_octaves, num_layers, num_octaves_descr, num_layers_descr); @@ -274,9 +274,9 @@ namespace } } -BestOf2NearestMatcher::BestOf2NearestMatcher(bool gpu_hint, float match_conf, int num_matches_thresh1, int num_matches_thresh2) +BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2) { - if (gpu_hint && getCudaEnabledDeviceCount() > 0) + if (try_use_gpu && getCudaEnabledDeviceCount() > 0) impl_ = new GpuMatcher(match_conf); else impl_ = new CpuMatcher(match_conf); diff --git a/modules/stitching/matchers.hpp b/modules/stitching/matchers.hpp index c3788d0e2..9ebc5f253 100644 --- a/modules/stitching/matchers.hpp +++ b/modules/stitching/matchers.hpp @@ -26,7 +26,7 @@ protected: class SurfFeaturesFinder : public FeaturesFinder { public: - explicit SurfFeaturesFinder(bool gpu_hint = true, double hess_thresh = 500.0, + explicit SurfFeaturesFinder(bool try_use_gpu = true, double hess_thresh = 500.0, int num_octaves = 3, int num_layers = 4, int num_octaves_descr = 4, int num_layers_descr = 2); @@ -70,7 +70,7 @@ protected: class BestOf2NearestMatcher : public FeaturesMatcher { public: - explicit BestOf2NearestMatcher(bool gpu_hint = true, float match_conf = 0.55f, int num_matches_thresh1 = 5, int num_matches_thresh2 = 5); + explicit BestOf2NearestMatcher(bool try_use_gpu = true, float match_conf = 0.55f, int num_matches_thresh1 = 5, int num_matches_thresh2 = 5); protected: void match(const cv::Mat &img1, const ImageFeatures &features1, const cv::Mat &img2, const ImageFeatures &features2,