opencv/opencv_contrib

fast_line_detector will not merge segments while there are only two segs on one line

Roger8 opened this issue · 0 comments

System information (version)
  • OpenCV => :4.9:
  • Operating System / Platform => :Windows 64 Bit:
  • Compiler => :Visual Studio 2019:
Detailed description

https://github.com/opencv/opencv_contrib/blob/4.x/modules/ximgproc/src/fast_line_detector.cpp#L584

    bool is_merged = false;
    int ith = (int)segments_tmp.size() - 1;
    int jth = ith - 1;
    while(ith > 1 || jth > 0)
    {
        seg1 = segments_tmp[ith];
        seg2 = segments_tmp[jth];
        SEGMENT seg_merged;
        is_merged = mergeSegments(seg1, seg2, seg_merged);
        if(is_merged == true)
        {
            seg2 = seg_merged;
            additionalOperationsOnSegment(src, seg2);
            std::vector<SEGMENT>::iterator it = segments_tmp.begin() + ith;
            *it = seg2;
            segments_tmp.erase(segments_tmp.begin()+jth);
            ith--;
            jth = ith - 1;
        }
        else
        {
            jth--;
        }
        if(jth < 0) {
            ith--;
            jth = ith - 1;
        }
    }
    segments_all = segments_tmp;
}

if two segs (they are on one line) in segmets_tmp, then segments_tmp.size()==2 .
the while loop will not enter , finally segments_all has still two segments , they are not merged.

Steps to reproduce
   // add this before while loop
   SEGMENT seg1, seg2;
   seg1.x1 = 0;
   seg1.y1 = 0;
   seg1.x2 = 100;
   seg1.y2 = 0;

   seg2.x1 = 110;
   seg2.y1  =0;
   seg2.x2 = 250;
   seg2.y2 =0;
    segments_tmp.clear();
    segments_tmp.push_back(seg1);
    segments_tmp.push_back(seg2);
    // keep the same 
    bool is_merged = false;
    int ith = (int)segments_tmp.size() - 1;
    int jth = ith - 1;
    while(ith > 1 || jth > 0)
    {
 ....
    }
Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

About how to fix , my idea: change while condition

   while(ith > 1 || jth >= 0)
{   ....
}