Skip to content

Commit

Permalink
replaced patch constants in device memory with definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzoli committed Nov 14, 2015
1 parent cb191ac commit a7d7841
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 38 deletions.
14 changes: 2 additions & 12 deletions include/rmd/mvs_device_data.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,11 @@ struct SceneData
float sigma_sq_max;
};

struct CorrPatch
{
#ifndef RMD_CORR_PATCH_SIDE
#define RMD_CORR_PATCH_SIDE 5
#define RMD_CORR_PATCH_SIDE 5
#endif
#define RMD_CORR_PATCH_OFFSET -RMD_CORR_PATCH_SIDE/2
CorrPatch()
: side(RMD_CORR_PATCH_SIDE)
, offset(make_int2(RMD_CORR_PATCH_OFFSET,
RMD_CORR_PATCH_OFFSET))
{ }
const int side;
const int2 offset;
};
#define RMD_CORR_PATCH_AREA RMD_CORR_PATCH_SIDE*RMD_CORR_PATCH_SIDE

// DeviceData struct stores pointers to dev memory.
// It is allocated and set from host.
Expand Down Expand Up @@ -104,7 +95,6 @@ struct DeviceData
size_t height;

SceneData scene;
CorrPatch patch;

// Algorithm parameters
float eta_inlier;
Expand Down
1 change: 0 additions & 1 deletion include/rmd/seed_matrix.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public:
void downloadConstTemplDenom(float *host_align_row_maj) const;
void downloadConvergence(int *host_align_row_maj) const;
void downloadEpipolarMatches(float2 *host_align_row_maj) const;
int getPatchSide() const { return dev_data_.patch.side; }
#endif

private:
Expand Down
3 changes: 1 addition & 2 deletions src/epipolar_match.cu
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ void seedEpipolarMatchKernel(
sum_img_templ += img*templ;
}
}
#define RMD_CORR_PATCH_AREA RMD_CORR_PATCH_SIDE*RMD_CORR_PATCH_SIDE
const float ncc_numerator = RMD_CORR_PATCH_AREA*sum_img_templ - sum_img*sum_templ;
const float ncc_denominator = (RMD_CORR_PATCH_AREA*sum_img_sq - sum_img*sum_img)*const_templ_denom;
#undef RMD_CORR_PATCH_AREA

const float ncc = ncc_numerator * rsqrtf(ncc_denominator + FLT_MIN);

if(ncc > best_ncc)
Expand Down
4 changes: 2 additions & 2 deletions src/seed_check.cu
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ void seedCheckKernel(mvs::DeviceData *dev_ptr)
if(x >= dev_ptr->width || y >= dev_ptr->height)
return;

if(x > dev_ptr->width-dev_ptr->patch.side-1 || y > dev_ptr->height-dev_ptr->patch.side-1 ||
x < dev_ptr->patch.side || y < dev_ptr->patch.side)
if(x > dev_ptr->width-RMD_CORR_PATCH_SIDE-1 || y > dev_ptr->height-RMD_CORR_PATCH_SIDE-1 ||
x < RMD_CORR_PATCH_SIDE || y < RMD_CORR_PATCH_SIDE)
{
dev_ptr->convergence->atXY(x, y) = ConvergenceStates::BORDER;
return;
Expand Down
12 changes: 5 additions & 7 deletions src/seed_init.cu
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,22 @@ void seedInitKernel(mvs::DeviceData *dev_ptr)
// Compute template statistics for NCC
float sum_templ = 0.0f;
float sum_templ_sq = 0.0f;
const int &side = dev_ptr->patch.side;
const int2 &offset = dev_ptr->patch.offset;
for(int patch_y=0; patch_y<side; ++patch_y)
for(int patch_y=0; patch_y<RMD_CORR_PATCH_SIDE; ++patch_y)
{
for(int patch_x=0; patch_x<side; ++patch_x)
for(int patch_x=0; patch_x<RMD_CORR_PATCH_SIDE; ++patch_x)
{
const float templ = tex2D(
ref_img_tex,
(float)(x+offset.x+patch_x)+0.5f,
(float)(y+offset.y+patch_y)+0.5f);
(float)(x+RMD_CORR_PATCH_OFFSET+patch_x)+0.5f,
(float)(y+RMD_CORR_PATCH_OFFSET+patch_y)+0.5f);
sum_templ += templ;
sum_templ_sq += templ*templ;
}
}
dev_ptr->sum_templ->atXY(x, y) = sum_templ;

dev_ptr->const_templ_denom->atXY(x, y) =
(float) ( (double) side*side*sum_templ_sq - (double) sum_templ*sum_templ );
(float) ( (double) RMD_CORR_PATCH_AREA*sum_templ_sq - (double) sum_templ*sum_templ );

// Init measurement parameters
dev_ptr->mu->atXY(x, y) = dev_ptr->scene.avg_depth;
Expand Down
26 changes: 12 additions & 14 deletions test/seed_matrix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,28 @@ TEST(RMDCuTests, seedMatrixInit)
cv::Mat ocv_sum_templ(ref_img.rows, ref_img.cols, CV_32FC1);
cv::Mat ocv_const_templ_denom(ref_img.rows, ref_img.cols, CV_32FC1);

const int side = seeds.getPatchSide();
for(size_t y=side; y<ref_img.rows-side/2; ++y)
for(size_t y=RMD_CORR_PATCH_SIDE; y<ref_img.rows-RMD_CORR_PATCH_SIDE/2; ++y)
{
for(size_t x=side; x<ref_img.cols-side/2; ++x)
for(size_t x=RMD_CORR_PATCH_SIDE; x<ref_img.cols-RMD_CORR_PATCH_SIDE/2; ++x)
{
double sum_templ = 0.0f;
double sum_templ_sq = 0.0f;
for(int patch_y=0; patch_y<side; ++patch_y)
for(int patch_y=0; patch_y<RMD_CORR_PATCH_SIDE; ++patch_y)
{
for(int patch_x=0; patch_x<side; ++patch_x)
for(int patch_x=0; patch_x<RMD_CORR_PATCH_SIDE; ++patch_x)
{
const double templ = (double) ref_img_flt.at<float>( y-side/2+patch_y, x-side/2+patch_x );
const double templ = (double) ref_img_flt.at<float>( y-RMD_CORR_PATCH_SIDE/2+patch_y, x-RMD_CORR_PATCH_SIDE/2+patch_x );
sum_templ += templ;
sum_templ_sq += templ*templ;
}
}
ocv_sum_templ.at<float>(y, x) = (float) sum_templ;
ocv_const_templ_denom.at<float>(y, x) = (float) ( ((double)(side*side))*sum_templ_sq - sum_templ*sum_templ );
ocv_const_templ_denom.at<float>(y, x) = (float) ( ((double)RMD_CORR_PATCH_AREA)*sum_templ_sq - sum_templ*sum_templ );
}
}
for(size_t r=side; r<ref_img.rows-side/2; ++r)
for(size_t r=RMD_CORR_PATCH_SIDE; r<ref_img.rows-RMD_CORR_PATCH_SIDE/2; ++r)
{
for(size_t c=side; c<ref_img.cols-side/2; ++c)
for(size_t c=RMD_CORR_PATCH_SIDE; c<ref_img.cols-RMD_CORR_PATCH_SIDE/2; ++c)
{
ASSERT_NEAR(ocv_sum_templ.at<float>(r, c), cu_sum_templ.at<float>(r, c), 0.00001f);
ASSERT_NEAR(ocv_const_templ_denom.at<float>(r, c), cu_const_templ_denom.at<float>(r, c), 0.001f);
Expand Down Expand Up @@ -217,15 +216,14 @@ TEST(RMDCuTests, seedMatrixCheck)
cv::Mat cu_convergence(ref_img.rows, ref_img.cols, CV_32SC1);
seeds.downloadConvergence(reinterpret_cast<int*>(cu_convergence.data));

const int side = seeds.getPatchSide();
for(size_t r=0; r<ref_img.rows; ++r)
{
for(size_t c=0; c<ref_img.cols; ++c)
{
if(r>ref_img.rows-side-1
|| r<side
|| c>ref_img.cols-side-1
|| c<side)
if(r>ref_img.rows-RMD_CORR_PATCH_SIDE-1
|| r<RMD_CORR_PATCH_SIDE
|| c>ref_img.cols-RMD_CORR_PATCH_SIDE-1
|| c<RMD_CORR_PATCH_SIDE)
{
ASSERT_EQ(rmd::ConvergenceStates::BORDER, cu_convergence.at<int>(r, c)) << "(r, c) = (" << r << ", " << c <<")";
}
Expand Down

0 comments on commit a7d7841

Please sign in to comment.