opencv/modules/stitching/main.cpp

377 lines
12 KiB
C++
Raw Normal View History

2011-05-04 13:09:42 +02:00
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "util.hpp"
#include "warpers.hpp"
#include "blenders.hpp"
#include "seam_finders.hpp"
#include "motion_estimators.hpp"
using namespace std;
using namespace cv;
2011-05-05 14:12:32 +02:00
void printUsage()
2011-05-04 13:09:42 +02:00
{
cout << "Rotation model images stitcher.\n\n";
cout << "Usage: opencv_stitching img1 img2 [...imgN]\n"
<< "\t[--trygpu (yes|no)]\n"
<< "\t[--work_megapix <float>]\n"
<< "\t[--compose_megapix <float>]\n"
<< "\t[--matchconf <float>]\n"
<< "\t[--ba (ray|focal_ray)]\n"
<< "\t[--conf_thresh <float>]\n"
<< "\t[--wavecorrect (no|yes)]\n"
2011-05-04 13:09:42 +02:00
<< "\t[--warp (plane|cylindrical|spherical)]\n"
<< "\t[--seam (no|voronoi|graphcut)]\n"
<< "\t[--blend (no|feather|multiband)]\n"
<< "\t[--output <result_img>]\n\n";
cout << "--matchconf\n"
<< "\tGood values are in [0.2, 0.8] range usually.\n\n";
cout << "HINT:\n"
<< "\tDefault parameters are for '--trygpu no' configuration.\n"
<< "\tTry bigger values for --work_megapix if something is wrong.\n\n";
2011-05-04 13:09:42 +02:00
}
int main(int argc, char* argv[])
{
int64 app_start_time = getTickCount();
2011-05-04 13:09:42 +02:00
cv::setBreakOnError(true);
vector<string> img_names;
// Default parameters
bool trygpu = false;
double work_megapix = 0.2;
double compose_megapix = 1;
int ba_space = BundleAdjuster::FOCAL_RAY_SPACE;
float conf_thresh = 1.f;
bool wave_correct = true;
2011-05-04 13:09:42 +02:00
int warp_type = Warper::SPHERICAL;
bool user_match_conf = false;
float match_conf = 0.6f;
int seam_find_type = SeamFinder::VORONOI;
2011-05-04 13:09:42 +02:00
int blend_type = Blender::MULTI_BAND;
string result_name = "result.png";
2011-05-21 13:24:42 +02:00
double work_scale = 1, compose_scale = 1;
bool is_work_scale_set = false, is_compose_scale_set = false;
2011-05-04 13:09:42 +02:00
if (argc == 1)
{
2011-05-05 14:12:32 +02:00
printUsage();
2011-05-04 13:09:42 +02:00
return 0;
}
for (int i = 1; i < argc; ++i)
{
if (string(argv[i]) == "--work_megapix")
{
work_megapix = atof(argv[i + 1]);
break;
}
}
int64 t = getTickCount();
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]);
i++;
}
else if (string(argv[i]) == "--result")
2011-05-04 13:09:42 +02:00
{
result_name = argv[i + 1];
i++;
}
else if (string(argv[i]) == "--matchconf")
{
user_match_conf = true;
match_conf = static_cast<float>(atof(argv[i + 1]));
2011-05-04 13:09:42 +02:00
i++;
}
else if (string(argv[i]) == "--ba")
{
if (string(argv[i + 1]) == "ray")
2011-05-04 13:09:42 +02:00
ba_space = BundleAdjuster::RAY_SPACE;
else if (string(argv[i + 1]) == "focal_ray")
2011-05-04 13:09:42 +02:00
ba_space = BundleAdjuster::FOCAL_RAY_SPACE;
else
{
cout << "Bad bundle adjustment space\n";
return -1;
}
i++;
}
else if (string(argv[i]) == "--conf_thresh")
{
conf_thresh = static_cast<float>(atof(argv[i + 1]));
i++;
}
else if (string(argv[i]) == "--wavecorrect")
{
if (string(argv[i + 1]) == "no")
wave_correct = false;
else if (string(argv[i + 1]) == "yes")
wave_correct = true;
else
{
cout << "Bad --wavecorrect flag value\n";
return -1;
}
i++;
}
2011-05-04 13:09:42 +02:00
else if (string(argv[i]) == "--warp")
{
if (string(argv[i + 1]) == "plane")
warp_type = Warper::PLANE;
else if (string(argv[i + 1]) == "cylindrical")
warp_type = Warper::CYLINDRICAL;
else if (string(argv[i + 1]) == "spherical")
warp_type = Warper::SPHERICAL;
else
{
cout << "Bad warping method\n";
return -1;
}
i++;
}
else if (string(argv[i]) == "--seam")
{
if (string(argv[i + 1]) == "no")
seam_find_type = SeamFinder::NO;
else if (string(argv[i + 1]) == "voronoi")
seam_find_type = SeamFinder::VORONOI;
else if (string(argv[i + 1]) == "graphcut")
seam_find_type = SeamFinder::GRAPH_CUT;
else
{
cout << "Bad seam finding method\n";
return -1;
}
i++;
}
else if (string(argv[i]) == "--blend")
{
if (string(argv[i + 1]) == "no")
blend_type = Blender::NO;
else if (string(argv[i + 1]) == "feather")
blend_type = Blender::FEATHER;
else if (string(argv[i + 1]) == "multiband")
blend_type = Blender::MULTI_BAND;
else
{
cout << "Bad blending method\n";
return -1;
}
i++;
}
else if (string(argv[i]) == "--output")
{
result_name = argv[i + 1];
i++;
}
else
img_names.push_back(argv[i]);
2011-05-04 13:09:42 +02:00
}
2011-05-20 10:08:55 +02:00
int num_images = static_cast<int>(img_names.size());
2011-05-04 13:09:42 +02:00
if (num_images < 2)
{
cout << "Need more images\n";
return -1;
}
LOGLN("Reading images and finding features...");
t = getTickCount();
2011-05-20 10:08:55 +02:00
vector<ImageFeatures> features(num_images);
SurfFeaturesFinder finder(trygpu);
2011-05-20 10:08:55 +02:00
Mat full_img, img;
for (int i = 0; i < num_images; ++i)
{
full_img = imread(img_names[i]);
if (full_img.empty())
{
cout << "Can't open image " << img_names[i] << endl;
return -1;
}
if (work_megapix < 0)
img = full_img;
else
{
if (!is_work_scale_set)
{
work_scale = min(1.0, sqrt(work_megapix * 1e6 / full_img.size().area()));
is_work_scale_set = true;
}
resize(full_img, img, Size(), work_scale, work_scale);
}
finder(img, features[i]);
}
LOGLN("Reading images and finding features, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
LOGLN("Pairwise matching... ");
t = getTickCount();
2011-05-04 13:09:42 +02:00
vector<MatchesInfo> pairwise_matches;
BestOf2NearestMatcher matcher(trygpu);
2011-05-04 13:09:42 +02:00
if (user_match_conf)
2011-05-18 17:05:06 +02:00
matcher = BestOf2NearestMatcher(trygpu, match_conf);
2011-05-20 10:08:55 +02:00
matcher(features, pairwise_matches);
LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
2011-05-20 10:08:55 +02:00
vector<int> indices = leaveBiggestComponent(features, pairwise_matches, conf_thresh);
vector<string> img_names_subset;
for (size_t i = 0; i < indices.size(); ++i)
img_names_subset.push_back(img_names[indices[i]]);
img_names = img_names_subset;
2011-05-20 10:08:55 +02:00
num_images = static_cast<int>(img_names.size());
if (num_images < 2)
{
cout << "Need more images\n";
return -1;
}
2011-05-04 13:09:42 +02:00
LOGLN("Estimating rotations...");
t = getTickCount();
2011-05-04 13:09:42 +02:00
HomographyBasedEstimator estimator;
vector<CameraParams> cameras;
2011-05-20 10:08:55 +02:00
estimator(features, pairwise_matches, cameras);
LOGLN("Estimating rotations, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
for (size_t i = 0; i < cameras.size(); ++i)
{
Mat R;
2011-05-16 07:11:09 +02:00
cameras[i].R.convertTo(R, CV_32F);
cameras[i].R = R;
2011-05-04 13:09:42 +02:00
LOGLN("Initial focal length " << i << ": " << cameras[i].focal);
}
LOGLN("Bundle adjustment... ");
t = getTickCount();
BundleAdjuster adjuster(ba_space, conf_thresh);
2011-05-20 10:08:55 +02:00
adjuster(features, pairwise_matches, cameras);
LOGLN("Bundle adjustment, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
if (wave_correct)
{
LOGLN("Wave correcting...");
t = getTickCount();
vector<Mat> rmats;
for (size_t i = 0; i < cameras.size(); ++i)
2011-05-16 07:11:09 +02:00
rmats.push_back(cameras[i].R);
waveCorrect(rmats);
for (size_t i = 0; i < cameras.size(); ++i)
2011-05-16 07:11:09 +02:00
cameras[i].R = rmats[i];
LOGLN("Wave correcting, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
}
2011-05-04 13:09:42 +02:00
// Find median focal length
vector<double> focals;
for (size_t i = 0; i < cameras.size(); ++i)
{
LOGLN("Camera focal length " << i << ": " << cameras[i].focal);
focals.push_back(cameras[i].focal);
}
nth_element(focals.begin(), focals.end(), focals.begin() + focals.size() / 2);
2011-05-05 14:12:32 +02:00
float camera_focal = static_cast<float>(focals[focals.size() / 2]);
2011-05-04 13:09:42 +02:00
2011-05-20 10:08:55 +02:00
vector<Mat> images(num_images);
2011-05-20 10:08:55 +02:00
LOGLN("Compose scaling...");
t = getTickCount();
2011-05-20 10:08:55 +02:00
for (int i = 0; i < num_images; ++i)
{
2011-05-20 10:08:55 +02:00
Mat full_img = imread(img_names[i]);
if (!is_compose_scale_set)
{
2011-05-20 10:08:55 +02:00
compose_scale = min(1.0, sqrt(compose_megapix * 1e6 / full_img.size().area()));
is_compose_scale_set = true;
}
2011-05-20 10:08:55 +02:00
Mat img;
resize(full_img, img, Size(), compose_scale, compose_scale);
images[i] = img;
cameras[i].focal *= compose_scale / work_scale;
}
2011-05-20 10:08:55 +02:00
camera_focal *= static_cast<float>(compose_scale / work_scale);
LOGLN("Compose scaling, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
vector<Mat> masks(num_images);
for (int i = 0; i < num_images; ++i)
{
masks[i].create(images[i].size(), CV_8U);
masks[i].setTo(Scalar::all(255));
}
vector<Point> corners(num_images);
vector<Size> sizes(num_images);
2011-05-04 13:09:42 +02:00
vector<Mat> masks_warped(num_images);
vector<Mat> images_warped(num_images);
LOGLN("Warping images... ");
t = getTickCount();
2011-05-04 13:09:42 +02:00
Ptr<Warper> warper = Warper::createByCameraFocal(camera_focal, warp_type);
for (int i = 0; i < num_images; ++i)
{
2011-05-21 13:24:42 +02:00
corners[i] = warper->warp(images[i], static_cast<float>(cameras[i].focal), cameras[i].R,
images_warped[i]);
sizes[i] = images_warped[i].size();
2011-05-21 13:24:42 +02:00
warper->warp(masks[i], static_cast<float>(cameras[i].focal), cameras[i].R, masks_warped[i],
INTER_NEAREST, BORDER_CONSTANT);
2011-05-04 13:09:42 +02:00
}
2011-05-04 13:09:42 +02:00
vector<Mat> images_f(num_images);
for (int i = 0; i < num_images; ++i)
images_warped[i].convertTo(images_f[i], CV_32F);
LOGLN("Warping images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
LOGLN("Finding seams...");
t = getTickCount();
2011-05-04 13:09:42 +02:00
Ptr<SeamFinder> seam_finder = SeamFinder::createDefault(seam_find_type);
2011-05-21 13:24:42 +02:00
seam_finder->find(images_f, corners, masks_warped);
LOGLN("Finding seams, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
LOGLN("Blending images...");
t = getTickCount();
2011-05-04 13:09:42 +02:00
Ptr<Blender> blender = Blender::createDefault(blend_type);
if (blend_type == Blender::MULTI_BAND)
{
// Ensure last pyramid layer area is about 1 pix
MultiBandBlender* mb = dynamic_cast<MultiBandBlender*>((Blender*)(blender));
mb->setNumBands(static_cast<int>(ceil(log(static_cast<double>(images_f[0].size().area())) / log(4.0))));
LOGLN("Multi-band blending num. bands: " << mb->numBands());
}
blender->prepare(corners, sizes);
for (int i = 0; i < num_images; ++i)
blender->feed(images_f[i], masks_warped[i], corners[i]);
Mat result, result_mask;
blender->blend(result, result_mask);
LOGLN("Blending images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
imwrite(result_name, result);
LOGLN("Finished, total time: " << ((getTickCount() - app_start_time) / getTickFrequency()) << " sec");
2011-05-04 13:09:42 +02:00
return 0;
}