more refactoring of opencv_stitching

This commit is contained in:
Alexey Spizhevoy 2011-05-21 14:22:06 +00:00
parent aadb1669a7
commit 97530caa53
2 changed files with 45 additions and 30 deletions

View File

@ -31,34 +31,28 @@ void printUsage()
<< "\tTry bigger values for --work_megapix if something is wrong.\n\n"; << "\tTry bigger values for --work_megapix if something is wrong.\n\n";
} }
int main(int argc, char* argv[])
// Command line args
vector<string> img_names;
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;
int warp_type = Warper::SPHERICAL;
bool user_match_conf = false;
float match_conf = 0.6f;
int seam_find_type = SeamFinder::VORONOI;
int blend_type = Blender::MULTI_BAND;
string result_name = "result.png";
int parseCmdArgs(int argc, char** argv)
{ {
int64 app_start_time = getTickCount();
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;
int warp_type = Warper::SPHERICAL;
bool user_match_conf = false;
float match_conf = 0.6f;
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 = false;
if (argc == 1) if (argc == 1)
{ {
printUsage(); printUsage();
return 0; return -1;
} }
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
@ -70,7 +64,6 @@ int main(int argc, char* argv[])
} }
} }
int64 t = getTickCount();
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
{ {
if (string(argv[i]) == "--trygpu") if (string(argv[i]) == "--trygpu")
@ -188,7 +181,20 @@ int main(int argc, char* argv[])
else else
img_names.push_back(argv[i]); img_names.push_back(argv[i]);
} }
return 0;
}
int main(int argc, char* argv[])
{
int64 app_start_time = getTickCount();
cv::setBreakOnError(true);
int retval = parseCmdArgs(argc, argv);
if (retval)
return retval;
// Check if have enough images
int num_images = static_cast<int>(img_names.size()); int num_images = static_cast<int>(img_names.size());
if (num_images < 2) if (num_images < 2)
{ {
@ -196,8 +202,12 @@ int main(int argc, char* argv[])
return -1; return -1;
} }
// We do all matching in work_scale sclae, and compositing in compose_scale scale
double work_scale = 1, compose_scale = 1;
bool is_work_scale_set = false, is_compose_scale_set = false;
LOGLN("Reading images and finding features..."); LOGLN("Reading images and finding features...");
t = getTickCount(); int64 t = getTickCount();
vector<Mat> images(num_images); vector<Mat> images(num_images);
vector<ImageFeatures> features(num_images); vector<ImageFeatures> features(num_images);
SurfFeaturesFinder finder(trygpu); SurfFeaturesFinder finder(trygpu);
@ -237,6 +247,7 @@ int main(int argc, char* argv[])
matcher(features, pairwise_matches); matcher(features, pairwise_matches);
LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
// Leave only images we are sure are from the same panorama
vector<int> indices = leaveBiggestComponent(features, pairwise_matches, conf_thresh); vector<int> indices = leaveBiggestComponent(features, pairwise_matches, conf_thresh);
vector<Mat> img_subset; vector<Mat> img_subset;
vector<string> img_names_subset; vector<string> img_names_subset;
@ -248,6 +259,7 @@ int main(int argc, char* argv[])
images = img_subset; images = img_subset;
img_names = img_names_subset; img_names = img_names_subset;
// Check if we still have enough images
num_images = static_cast<int>(img_names.size()); num_images = static_cast<int>(img_names.size());
if (num_images < 2) if (num_images < 2)
{ {
@ -308,12 +320,14 @@ int main(int argc, char* argv[])
vector<Size> sizes(num_images); vector<Size> sizes(num_images);
vector<Mat> masks(num_images); vector<Mat> masks(num_images);
// Preapre original images masks
for (int i = 0; i < num_images; ++i) for (int i = 0; i < num_images; ++i)
{ {
masks[i].create(images[i].size(), CV_8U); masks[i].create(images[i].size(), CV_8U);
masks[i].setTo(Scalar::all(255)); masks[i].setTo(Scalar::all(255));
} }
// Warp images and their masks
Ptr<Warper> warper = Warper::createByCameraFocal(camera_focal, warp_type); Ptr<Warper> warper = Warper::createByCameraFocal(camera_focal, warp_type);
for (int i = 0; i < num_images; ++i) for (int i = 0; i < num_images; ++i)
{ {
@ -324,6 +338,7 @@ int main(int argc, char* argv[])
INTER_NEAREST, BORDER_CONSTANT); INTER_NEAREST, BORDER_CONSTANT);
} }
// Convert to float for blending
vector<Mat> images_warped_f(num_images); vector<Mat> images_warped_f(num_images);
for (int i = 0; i < num_images; ++i) for (int i = 0; i < num_images; ++i)
images_warped[i].convertTo(images_warped_f[i], CV_32F); images_warped[i].convertTo(images_warped_f[i], CV_32F);
@ -333,6 +348,7 @@ int main(int argc, char* argv[])
LOGLN("Finding seams..."); LOGLN("Finding seams...");
t = getTickCount(); t = getTickCount();
// Find seams
Ptr<SeamFinder> seam_finder = SeamFinder::createDefault(seam_find_type); Ptr<SeamFinder> seam_finder = SeamFinder::createDefault(seam_find_type);
seam_finder->find(images_warped_f, corners, masks_warped); seam_finder->find(images_warped_f, corners, masks_warped);

View File

@ -169,11 +169,13 @@ void FeaturesMatcher::operator ()(const vector<ImageFeatures> &features, vector<
dists[j].idx = j; dists[j].idx = j;
} }
// Leave near images
vector<bool> is_near(num_images, false); vector<bool> is_near(num_images, false);
for (int j = 0; j < num_images; ++j) for (int j = 0; j < num_images; ++j)
if (dists[j].dist < 0.6) if (dists[j].dist < 0.6)
is_near[dists[j].idx] = true; is_near[dists[j].idx] = true;
// Leave k-nearest images
int k = min(4, num_images); int k = min(4, num_images);
nth_element(dists.begin(), dists.end(), dists.begin() + k); nth_element(dists.begin(), dists.end(), dists.begin() + k);
for (int j = 0; j < k; ++j) for (int j = 0; j < k; ++j)
@ -181,6 +183,7 @@ void FeaturesMatcher::operator ()(const vector<ImageFeatures> &features, vector<
for (int j = i + 1; j < num_images; ++j) for (int j = i + 1; j < num_images; ++j)
{ {
// Ignore poor image pairs
if (!is_near[j]) if (!is_near[j])
continue; continue;
@ -213,7 +216,6 @@ namespace
{ {
public: public:
inline CpuMatcher(float match_conf) : match_conf_(match_conf) {} inline CpuMatcher(float match_conf) : match_conf_(match_conf) {}
void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info); void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
private: private:
@ -257,15 +259,12 @@ namespace
{ {
public: public:
inline GpuMatcher(float match_conf) : match_conf_(match_conf) {} inline GpuMatcher(float match_conf) : match_conf_(match_conf) {}
void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info); void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
private: private:
float match_conf_; float match_conf_;
GpuMat descriptors1_; GpuMat descriptors1_;
GpuMat descriptors2_; GpuMat descriptors2_;
GpuMat trainIdx_, distance_, allDist_; GpuMat trainIdx_, distance_, allDist_;
}; };