fixed bug in opencv_stitching (corrected resize images step), added matches checking (both 1->2 and 2->1 must be presented)

pull/13383/head
Alexey Spizhevoy 14 years ago
parent 3ed42fcd23
commit ace94d2ebf
  1. 21
      modules/stitching/main.cpp
  2. 8
      modules/stitching/matchers.cpp
  3. 2
      modules/stitching/motion_estimators.cpp
  4. 4
      modules/stitching/seam_finders.cpp
  5. 3
      modules/stitching/warpers.hpp
  6. 16
      modules/stitching/warpers_inl.hpp
  7. 53
      samples/gpu/driver_api_stereo_multi.cpp

@ -75,7 +75,7 @@ void printUsage()
" --work_megapix <float>\n"
" Resolution for image registration step. The default is 0.6 Mpx.\n"
" --match_conf <float>\n"
" Confidence for feature matching step. The default is 0.7.\n"
" Confidence for feature matching step. The default is 0.65.\n"
" --conf_thresh <float>\n"
" Threshold for two images are from the same panorama confidence.\n"
" The default is 1.0.\n"
@ -320,11 +320,14 @@ int main(int argc, char* argv[])
Mat full_img, img;
vector<Mat> images(num_images);
vector<Size> full_img_sizes(num_images);
double seam_work_aspect = 1;
for (int i = 0; i < num_images; ++i)
{
full_img = imread(img_names[i]);
full_img_sizes[i] = full_img.size();
if (full_img.empty())
{
LOGLN("Can't open image " << img_names[i]);
@ -376,14 +379,17 @@ int main(int argc, char* argv[])
vector<int> indices = leaveBiggestComponent(features, pairwise_matches, conf_thresh);
vector<Mat> img_subset;
vector<string> img_names_subset;
vector<Size> full_img_sizes_subset;
for (size_t i = 0; i < indices.size(); ++i)
{
img_names_subset.push_back(img_names[indices[i]]);
img_subset.push_back(images[indices[i]]);
full_img_sizes_subset.push_back(full_img_sizes[indices[i]]);
}
images = img_subset;
img_names = img_names_subset;
full_img_sizes = full_img_sizes_subset;
// Check if we still have enough images
num_images = static_cast<int>(img_names.size());
@ -519,16 +525,21 @@ int main(int argc, char* argv[])
warper = Warper::createByCameraFocal(warped_image_scale, warp_type);
// Update corners and sizes
Rect dst_roi = resultRoi(corners, sizes);
for (int i = 0; i < num_images; ++i)
{
// Update camera focal
cameras[i].focal *= compose_work_aspect;
// Update corner and size
corners[i] = dst_roi.tl() + (corners[i] - dst_roi.tl()) * compose_seam_aspect;
sizes[i] = Size(static_cast<int>((sizes[i].width + 1) * compose_seam_aspect),
static_cast<int>((sizes[i].height + 1) * compose_seam_aspect));
Size sz = full_img_sizes[i];
if (abs(compose_scale - 1) > 1e-1)
{
sz.width = cvRound(full_img_sizes[i].width * compose_scale);
sz.height = cvRound(full_img_sizes[i].height * compose_scale);
}
Rect roi = warper->warpRoi(sz, static_cast<float>(cameras[i].focal), cameras[i].R);
corners[i] = roi.tl();
sizes[i] = roi.size();
}
}
if (abs(compose_scale - 1) > 1e-1)

@ -311,7 +311,7 @@ namespace
const DMatch& m1 = pair_matches[i][1];
if (m0.distance < (1.f - match_conf_) * m1.distance)
{
matches_info.matches.push_back(m0);
//matches_info.matches.push_back(m0);
matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
}
}
@ -326,7 +326,7 @@ namespace
const DMatch& m0 = pair_matches[i][0];
const DMatch& m1 = pair_matches[i][1];
if (m0.distance < (1.f - match_conf_) * m1.distance)
if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) != matches.end())
matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
}
}
@ -352,7 +352,7 @@ namespace
const DMatch& m1 = pair_matches[i][1];
if (m0.distance < (1.f - match_conf_) * m1.distance)
{
matches_info.matches.push_back(m0);
//matches_info.matches.push_back(m0);
matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
}
}
@ -368,7 +368,7 @@ namespace
const DMatch& m0 = pair_matches[i][0];
const DMatch& m1 = pair_matches[i][1];
if (m0.distance < (1.f - match_conf_) * m1.distance)
if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) != matches.end())
matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
}
}

@ -435,7 +435,7 @@ vector<int> leaveBiggestComponent(vector<ImageFeatures> &features, vector<Match
LOG(indices_removed[0]+1);
for (size_t i = 1; i < indices_removed.size(); ++i)
LOG(", " << indices_removed[i]+1);
LOGLN(")");
LOGLN("). Try decrease --match_conf value.");
features = features_subset;
pairwise_matches = pairwise_matches_subset;

@ -208,7 +208,7 @@ void GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &
}
// Set regular edge weights
const float weight_eps = 1e-3f;
const float weight_eps = 1.f;
for (int y = 0; y < img_size.height; ++y)
{
for (int x = 0; x < img_size.width; ++x)
@ -258,7 +258,7 @@ void GraphCutSeamFinder::Impl::setGraphWeightsColorGrad(
}
// Set regular edge weights
const float weight_eps = 1e-3f;
const float weight_eps = 1.f;
for (int y = 0; y < img_size.height; ++y)
{
for (int x = 0; x < img_size.width; ++x)

@ -53,6 +53,7 @@ public:
virtual ~Warper() {}
virtual cv::Point warp(const cv::Mat &src, float focal, const cv::Mat& R, cv::Mat &dst,
int interp_mode = cv::INTER_LINEAR, int border_mode = cv::BORDER_REFLECT) = 0;
virtual cv::Rect warpRoi(const cv::Size &sz, float focal, const cv::Mat &R) = 0;
};
@ -75,6 +76,8 @@ public:
cv::Point warp(const cv::Mat &src, float focal, const cv::Mat &R, cv::Mat &dst,
int interp_mode, int border_mode);
cv::Rect warpRoi(const cv::Size &sz, float focal, const cv::Mat &R);
protected:
// Detects ROI of the destination image. It's correct for any projection.
virtual void detectResultRoi(cv::Point &dst_tl, cv::Point &dst_br);

@ -78,6 +78,22 @@ cv::Point WarperBase<P>::warp(const cv::Mat &src, float focal, const cv::Mat &R,
}
template <class P>
cv::Rect WarperBase<P>::warpRoi(const cv::Size &sz, float focal, const cv::Mat &R)
{
src_size_ = sz;
projector_.size = sz;
projector_.focal = focal;
projector_.setTransformation(R);
cv::Point dst_tl, dst_br;
detectResultRoi(dst_tl, dst_br);
return cv::Rect(dst_tl, cv::Point(dst_br.x + 1, dst_br.y + 1));
}
template <class P>
void WarperBase<P>::detectResultRoi(cv::Point &dst_tl, cv::Point &dst_br)
{

@ -32,7 +32,11 @@ int main()
#include <cuda.h>
#include <cuda_runtime.h>
#include <GL/gl.h>
#include <cudaGL.h>
#include "opencv2/core/internal.hpp" // For TBB wrappers
#include "tbb/tbb.h"
#include "tbb/mutex.h"
using namespace std;
using namespace cv;
@ -54,7 +58,7 @@ inline void safeCall_(int code, const char* expr, const char* file, int line)
}
// Each GPU is associated with its own context
CUcontext contexts[2];
CUcontext contexts[/*2*/1];
void inline contextOn(int id)
{
@ -76,6 +80,10 @@ GpuMat d_result[2];
// CPU result
Mat result;
int some[2];
tbb::mutex mutex;
int main(int argc, char** argv)
{
if (argc < 3)
@ -85,11 +93,11 @@ int main(int argc, char** argv)
}
int num_devices = getCudaEnabledDeviceCount();
if (num_devices < 2)
{
std::cout << "Two or more GPUs are required\n";
return -1;
}
// if (num_devices < 2)
// {
// std::cout << "Two or more GPUs are required\n";
// return -1;
// }
for (int i = 0; i < num_devices; ++i)
{
@ -123,13 +131,14 @@ int main(int argc, char** argv)
// Create context for GPU #0
CUdevice device;
safeCall(cuDeviceGet(&device, 0));
safeCall(cuCtxCreate(&contexts[0], 0, device));
safeCall(cuGLCtxCreate(&contexts[0], 0, device));
//safeCall(cuCtxCreate(&contexts[0], 0, device));
contextOff();
// Create context for GPU #1
safeCall(cuDeviceGet(&device, 1));
safeCall(cuCtxCreate(&contexts[1], 0, device));
contextOff();
// // Create context for GPU #1
// safeCall(cuDeviceGet(&device, 0));
// safeCall(cuCtxCreate(&contexts[1], 0, device));
// contextOff();
// Split source images for processing on GPU #0
contextOn(0);
@ -139,15 +148,20 @@ int main(int argc, char** argv)
contextOff();
// Split source images for processing on the GPU #1
contextOn(1);
contextOn(0);
d_left[1].upload(left.rowRange(left.rows / 2, left.rows));
d_right[1].upload(right.rowRange(right.rows / 2, right.rows));
bm[1] = new StereoBM_GPU();
contextOff();
some[0] = some[1] = 0;
// Execute calculation in two threads using two GPUs
int devices[] = {0, 1};
parallel_do(devices, devices + 2, Worker());
vector<int> devices;
for (int i = 0; i < 4; ++i)
devices.push_back(rand()%2);
tbb::parallel_do(&devices[0], &devices[devices.size() - 1], Worker());
cout << some[0] << " " << some[1] << endl;
// Release the first GPU resources
contextOn(0);
@ -159,7 +173,7 @@ int main(int argc, char** argv)
contextOff();
// Release the second GPU resources
contextOn(1);
contextOn(0);
imshow("GPU #1 result", Mat(d_result[1]));
d_left[1].release();
d_right[1].release();
@ -175,7 +189,9 @@ int main(int argc, char** argv)
void Worker::operator()(int device_id) const
{
contextOn(device_id);
mutex.lock();
contextOn(0);
bm[device_id]->operator()(d_left[device_id], d_right[device_id],
d_result[device_id]);
@ -184,13 +200,16 @@ void Worker::operator()(int device_id) const
<< "): finished\n";
contextOff();
mutex.unlock();
}
void destroyContexts()
{
safeCall(cuCtxDestroy(contexts[0]));
safeCall(cuCtxDestroy(contexts[1]));
//safeCall(cuCtxDestroy(contexts[1]));
}
#endif

Loading…
Cancel
Save