Skip to content

Commit 4c2ef47

Browse files
tsenstalalek
authored andcommitted
Merge pull request #2209 from tsenst:update_EdgeAwareInterpolator
Enhance EdgeAwareInterpolation no. matches limitation (#2209) * sparse_match_interpolators EdgeAwareInterpolation - enhance limitation of the maximal number of matches to be interpolated from SHORT_MAX to INT_MAX by substituting short by int types * sparse_match_interpolators EdgeAwareInterpolation - enhance limitation of the maximal number of matches to be interpolated from SHORT_MAX to INT_MAX by substituting short by int types * fix wrong allocation of labels mats CV16S to CV_32S
1 parent e92f31c commit 4c2ef47

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

modules/ximgproc/src/sparse_match_interpolators.cpp

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ struct SparseMatch
5757

5858
bool operator<(const SparseMatch& lhs,const SparseMatch& rhs);
5959

60-
void weightedLeastSquaresAffineFit(short* labels, float* weights, int count, float lambda, SparseMatch* matches, Mat& dst);
61-
void generateHypothesis(short* labels, int count, RNG& rng, unsigned char* is_used, SparseMatch* matches, Mat& dst);
62-
void verifyHypothesis(short* labels, float* weights, int count, SparseMatch* matches, float eps, float lambda, Mat& hypothesis_transform, Mat& old_transform, float& old_weighted_num_inliers);
60+
void weightedLeastSquaresAffineFit(int* labels, float* weights, int count, float lambda, SparseMatch* matches, Mat& dst);
61+
void generateHypothesis(int* labels, int count, RNG& rng, unsigned char* is_used, SparseMatch* matches, Mat& dst);
62+
void verifyHypothesis(int* labels, float* weights, int count, SparseMatch* matches, float eps, float lambda, Mat& hypothesis_transform, Mat& old_transform, float& old_weighted_num_inliers);
6363

6464
struct node
6565
{
6666
float dist;
67-
short label;
67+
int label;
6868
node() {}
69-
node(short l,float d): dist(d), label(l) {}
69+
node(int l,float d): dist(d), label(l) {}
7070
};
7171

7272
class EdgeAwareInterpolatorImpl CV_FINAL : public EdgeAwareInterpolator
@@ -78,7 +78,6 @@ class EdgeAwareInterpolatorImpl CV_FINAL : public EdgeAwareInterpolator
7878
protected:
7979
int w,h;
8080
int match_num;
81-
8281
//internal buffers:
8382
vector<node>* g;
8483
Mat labels;
@@ -186,9 +185,9 @@ void EdgeAwareInterpolatorImpl::interpolate(InputArray from_image, InputArray fr
186185
CV_Assert(match_num<SHRT_MAX);
187186

188187
Mat src = from_image.getMat();
189-
labels = Mat(h,w,CV_16S);
188+
labels = Mat(h,w,CV_32S);
190189
labels = Scalar(-1);
191-
NNlabels = Mat(match_num,k,CV_16S);
190+
NNlabels = Mat(match_num,k,CV_32S);
192191
NNlabels = Scalar(-1);
193192
NNdistances = Mat(match_num,k,CV_32F);
194193
NNdistances = Scalar(0.0f);
@@ -218,7 +217,7 @@ void EdgeAwareInterpolatorImpl::preprocessData(Mat& src, vector<SparseMatch>& ma
218217
y = min((int)(matches[i].reference_image_pos.y+0.5f),h-1);
219218

220219
distances.at<float>(y,x) = 0.0f;
221-
labels.at<short>(y,x) = (short)i;
220+
labels.at<int>(y,x) = (int)i;
222221
}
223222

224223
computeGradientMagnitude(src,cost_map);
@@ -234,7 +233,7 @@ void EdgeAwareInterpolatorImpl::computeGradientMagnitude(Mat& src, Mat& dst)
234233
Mat dx,dy;
235234
Sobel(src, dx, CV_16S, 1, 0);
236235
Sobel(src, dy, CV_16S, 0, 1);
237-
float norm_coef = src.channels()*4.0f*255.0f;
236+
float norm_coef = src.channels()*4*255.0f;
238237

239238
if(src.channels()==1)
240239
{
@@ -272,8 +271,8 @@ void EdgeAwareInterpolatorImpl::geodesicDistanceTransform(Mat& distances, Mat& c
272271
int i,j;
273272
float *dist_row, *cost_row;
274273
float *dist_row_prev, *cost_row_prev;
275-
short *label_row;
276-
short *label_row_prev;
274+
int *label_row;
275+
int *label_row_prev;
277276

278277
#define CHECK(cur_dist,cur_label,cur_cost,prev_dist,prev_label,prev_cost,coef)\
279278
{\
@@ -287,7 +286,7 @@ void EdgeAwareInterpolatorImpl::geodesicDistanceTransform(Mat& distances, Mat& c
287286
{
288287
//first pass (left-to-right, top-to-bottom):
289288
dist_row = distances.ptr<float>(0);
290-
label_row = labels.ptr<short>(0);
289+
label_row = labels.ptr<int>(0);
291290
cost_row = cost_map.ptr<float>(0);
292291
for(j=1;j<w;j++)
293292
CHECK(dist_row[j],label_row[j],cost_row[j],dist_row[j-1],label_row[j-1],cost_row[j-1],c1);
@@ -297,8 +296,8 @@ void EdgeAwareInterpolatorImpl::geodesicDistanceTransform(Mat& distances, Mat& c
297296
dist_row = distances.ptr<float>(i);
298297
dist_row_prev = distances.ptr<float>(i-1);
299298

300-
label_row = labels.ptr<short>(i);
301-
label_row_prev = labels.ptr<short>(i-1);
299+
label_row = labels.ptr<int>(i);
300+
label_row_prev = labels.ptr<int>(i-1);
302301

303302
cost_row = cost_map.ptr<float>(i);
304303
cost_row_prev = cost_map.ptr<float>(i-1);
@@ -321,7 +320,7 @@ void EdgeAwareInterpolatorImpl::geodesicDistanceTransform(Mat& distances, Mat& c
321320

322321
//second pass (right-to-left, bottom-to-top):
323322
dist_row = distances.ptr<float>(h-1);
324-
label_row = labels.ptr<short>(h-1);
323+
label_row = labels.ptr<int>(h-1);
325324
cost_row = cost_map.ptr<float>(h-1);
326325
for(j=w-2;j>=0;j--)
327326
CHECK(dist_row[j],label_row[j],cost_row[j],dist_row[j+1],label_row[j+1],cost_row[j+1],c1);
@@ -331,8 +330,8 @@ void EdgeAwareInterpolatorImpl::geodesicDistanceTransform(Mat& distances, Mat& c
331330
dist_row = distances.ptr<float>(i);
332331
dist_row_prev = distances.ptr<float>(i+1);
333332

334-
label_row = labels.ptr<short>(i);
335-
label_row_prev = labels.ptr<short>(i+1);
333+
label_row = labels.ptr<int>(i);
334+
label_row_prev = labels.ptr<int>(i+1);
336335

337336
cost_row = cost_map.ptr<float>(i);
338337
cost_row_prev = cost_map.ptr<float>(i+1);
@@ -360,8 +359,8 @@ void EdgeAwareInterpolatorImpl::buildGraph(Mat& distances, Mat& cost_map)
360359
{
361360
float *dist_row, *cost_row;
362361
float *dist_row_prev, *cost_row_prev;
363-
short *label_row;
364-
short *label_row_prev;
362+
int *label_row;
363+
int *label_row_prev;
365364
int i,j;
366365
const float c1 = 1.0f/2.0f;
367366
const float c2 = sqrt(2.0f)/2.0f;
@@ -387,7 +386,7 @@ void EdgeAwareInterpolatorImpl::buildGraph(Mat& distances, Mat& cost_map)
387386
}
388387

389388
dist_row = distances.ptr<float>(0);
390-
label_row = labels.ptr<short>(0);
389+
label_row = labels.ptr<int>(0);
391390
cost_row = cost_map.ptr<float>(0);
392391
for(j=1;j<w;j++)
393392
CHECK(dist_row[j],label_row[j],cost_row[j],dist_row[j-1],label_row[j-1],cost_row[j-1],c1);
@@ -397,8 +396,8 @@ void EdgeAwareInterpolatorImpl::buildGraph(Mat& distances, Mat& cost_map)
397396
dist_row = distances.ptr<float>(i);
398397
dist_row_prev = distances.ptr<float>(i-1);
399398

400-
label_row = labels.ptr<short>(i);
401-
label_row_prev = labels.ptr<short>(i-1);
399+
label_row = labels.ptr<int>(i);
400+
label_row_prev = labels.ptr<int>(i-1);
402401

403402
cost_row = cost_map.ptr<float>(i);
404403
cost_row_prev = cost_map.ptr<float>(i-1);
@@ -442,7 +441,7 @@ void EdgeAwareInterpolatorImpl::buildGraph(Mat& distances, Mat& cost_map)
442441
}
443442

444443
if(!found)
445-
g[neighbors[j].label].push_back(node((short)i,neighbors[j].dist));
444+
g[neighbors[j].label].push_back(node((int)i,neighbors[j].dist));
446445
}
447446
}
448447
}
@@ -453,18 +452,18 @@ struct nodeHeap
453452
// children: 2*i, 2*i+1
454453
// parent: i>>1
455454
node* heap;
456-
short* heap_pos;
455+
int* heap_pos;
457456
node tmp_node;
458-
short size;
459-
short num_labels;
457+
int size;
458+
int num_labels;
460459

461-
nodeHeap(short _num_labels)
460+
nodeHeap(int _num_labels)
462461
{
463462
num_labels = _num_labels;
464463
heap = new node[num_labels+1];
465464
heap[0] = node(-1,-1.0f);
466-
heap_pos = new short[num_labels];
467-
memset(heap_pos,0,sizeof(short)*num_labels);
465+
heap_pos = new int[num_labels];
466+
memset(heap_pos,0,sizeof(int)*num_labels);
468467
size=0;
469468
}
470469

@@ -477,15 +476,15 @@ struct nodeHeap
477476
void clear()
478477
{
479478
size=0;
480-
memset(heap_pos,0,sizeof(short)*num_labels);
479+
memset(heap_pos,0,sizeof(int)*num_labels);
481480
}
482481

483482
inline bool empty()
484483
{
485484
return (size==0);
486485
}
487486

488-
inline void nodeSwap(short idx1, short idx2)
487+
inline void nodeSwap(int idx1, int idx2)
489488
{
490489
heap_pos[heap[idx1].label] = idx2;
491490
heap_pos[heap[idx2].label] = idx1;
@@ -500,8 +499,8 @@ struct nodeHeap
500499
size++;
501500
heap[size] = n;
502501
heap_pos[n.label] = size;
503-
short i = size;
504-
short parent_i = i>>1;
502+
int i = size;
503+
int parent_i = i>>1;
505504
while(heap[i].dist<heap[parent_i].dist)
506505
{
507506
nodeSwap(i,parent_i);
@@ -515,8 +514,8 @@ struct nodeHeap
515514
node res = heap[1];
516515
heap_pos[res.label] = 0;
517516

518-
short i=1;
519-
short left,right;
517+
int i=1;
518+
int left,right;
520519
while( (left=i<<1) < size )
521520
{
522521
right = left+1;
@@ -543,7 +542,7 @@ struct nodeHeap
543542
heap[i] = heap[size];
544543
heap_pos[heap[i].label] = i;
545544

546-
short parent_i = i>>1;
545+
int parent_i = i>>1;
547546
while(heap[i].dist<heap[parent_i].dist)
548547
{
549548
nodeSwap(i,parent_i);
@@ -562,9 +561,9 @@ struct nodeHeap
562561
{
563562
if(heap_pos[n.label])
564563
{
565-
short i = heap_pos[n.label];
564+
int i = heap_pos[n.label];
566565
heap[i].dist = min(heap[i].dist,n.dist);
567-
short parent_i = i>>1;
566+
int parent_i = i>>1;
568567
while(heap[i].dist<heap[parent_i].dist)
569568
{
570569
nodeSwap(i,parent_i);
@@ -587,7 +586,7 @@ void EdgeAwareInterpolatorImpl::GetKNNMatches_ParBody::operator() (const Range&
587586
{
588587
int start = std::min(range.start * stripe_sz, inst->match_num);
589588
int end = std::min(range.end * stripe_sz, inst->match_num);
590-
nodeHeap q((short)inst->match_num);
589+
nodeHeap q((int)inst->match_num);
591590
int num_expanded_vertices;
592591
unsigned char* expanded_flag = new unsigned char[inst->match_num];
593592
node* neighbors;
@@ -600,8 +599,8 @@ void EdgeAwareInterpolatorImpl::GetKNNMatches_ParBody::operator() (const Range&
600599
num_expanded_vertices = 0;
601600
memset(expanded_flag,0,inst->match_num);
602601
q.clear();
603-
q.add(node((short)i,0.0f));
604-
short* NNlabels_row = inst->NNlabels.ptr<short>(i);
602+
q.add(node((int)i,0.0f));
603+
int* NNlabels_row = inst->NNlabels.ptr<int>(i);
605604
float* NNdistances_row = inst->NNdistances.ptr<float>(i);
606605
while(num_expanded_vertices<inst->k && !q.empty())
607606
{
@@ -625,7 +624,7 @@ void EdgeAwareInterpolatorImpl::GetKNNMatches_ParBody::operator() (const Range&
625624
delete[] expanded_flag;
626625
}
627626

628-
void weightedLeastSquaresAffineFit(short* labels, float* weights, int count, float lambda, SparseMatch* matches, Mat& dst)
627+
void weightedLeastSquaresAffineFit(int* labels, float* weights, int count, float lambda, SparseMatch* matches, Mat& dst)
629628
{
630629
double sa[6][6]={{0.}}, sb[6]={0.};
631630
Mat A (6, 6, CV_64F, &sa[0][0]),
@@ -672,7 +671,7 @@ void weightedLeastSquaresAffineFit(short* labels, float* weights, int count, flo
672671
MM.reshape(2,3).convertTo(dst,CV_32F);
673672
}
674673

675-
void generateHypothesis(short* labels, int count, RNG& rng, unsigned char* is_used, SparseMatch* matches, Mat& dst)
674+
void generateHypothesis(int* labels, int count, RNG& rng, unsigned char* is_used, SparseMatch* matches, Mat& dst)
676675
{
677676
int idx;
678677
Point2f src_points[3];
@@ -703,7 +702,7 @@ void generateHypothesis(short* labels, int count, RNG& rng, unsigned char* is_us
703702
getAffineTransform(src_points,dst_points).convertTo(dst,CV_32F);
704703
}
705704

706-
void verifyHypothesis(short* labels, float* weights, int count, SparseMatch* matches, float eps, float lambda, Mat& hypothesis_transform, Mat& old_transform, float& old_weighted_num_inliers)
705+
void verifyHypothesis(int* labels, float* weights, int count, SparseMatch* matches, float eps, float lambda, Mat& hypothesis_transform, Mat& old_transform, float& old_weighted_num_inliers)
707706
{
708707
float* tr = hypothesis_transform.ptr<float>(0);
709708
Point2f a,b;
@@ -749,12 +748,12 @@ void EdgeAwareInterpolatorImpl::RansacInterpolation_ParBody::operator() (const R
749748
start = tmp-1;
750749
}
751750

752-
short* KNNlabels;
751+
int* KNNlabels;
753752
float* KNNdistances;
754753
unsigned char* is_used = new unsigned char[inst->k];
755754
Mat hypothesis_transform;
756755

757-
short* inlier_labels = new short[inst->k];
756+
int* inlier_labels = new int[inst->k];
758757
float* inlier_distances = new float[inst->k];
759758
float* tr;
760759
int num_inliers;
@@ -765,7 +764,7 @@ void EdgeAwareInterpolatorImpl::RansacInterpolation_ParBody::operator() (const R
765764
if(inst->g[i].empty())
766765
continue;
767766

768-
KNNlabels = inst->NNlabels.ptr<short>(i);
767+
KNNlabels = inst->NNlabels.ptr<int>(i);
769768
KNNdistances = inst->NNdistances.ptr<float>(i);
770769
if(inc>0) //forward pass
771770
{
@@ -846,11 +845,11 @@ void EdgeAwareInterpolatorImpl::ransacInterpolation(vector<SparseMatch>& matches
846845
parallel_for_(Range(0,ransac_num_stripes),RansacInterpolation_ParBody(*this,transforms,weighted_inlier_nums,eps,&matches.front(),ransac_num_stripes,-1));
847846

848847
//construct the final piecewise-affine interpolation:
849-
short* label_row;
848+
int* label_row;
850849
float* tr;
851850
for(int i=0;i<h;i++)
852851
{
853-
label_row = labels.ptr<short>(i);
852+
label_row = labels.ptr<int>(i);
854853
Point2f* dst_row = dst_dense_flow.ptr<Point2f>(i);
855854
for(int j=0;j<w;j++)
856855
{

0 commit comments

Comments
 (0)