0

It has been bugging me for the whole afternoon. Below is a code snippet, I am trying to code to sort into vertical (linesY_acc) and horizontal line (linesX_acc) with some tolerance angle .

I do not know why only the preceding linesX_acc get filled while linesY_acc gets nothing? Maybe it is a minor bug but i just cannot see why

Need your help! Thanks in advance

std::vector<cv::Vec4i> linesX_acc, linesY_acc;
int boxWidth_threshold  = 35;
double threshold_anglesX = 20.0 /180 * CV_PI;
double threshold_anglesY = 20.0 /180 * CV_PI;

std::vector<cv::Vec4i>::iterator itlines;

for(itlines = lines.begin(); itlines != lines.end(); itlines++)
{
    // distlength --- calculate the line length;
    //                discard line if it is smaller than boxWidth_threshold
    if(distlength(cv::Point2f((*itlines)[0], (*itlines)[1]),
                           cv::Point2f((*itlines)[2], (*itlines)[3])) < boxWidth_threshold )
    {
        continue;
    }

    // filtering the angle of line
    // myAngle - function to caluclate angle
    double angle =std::abs(myAngle((*itlines)));

    if( (angle < threshold_anglesX ) || (angle > (CV_PI - threshold_anglesX )) )
    {
        linesX_acc.push_back(lines[i]);
    }
    else if ( (angle > (CV_PI/2 -threshold_anglesY ) ) &&
            (angle < (CV_PI/2 + threshold_anglesY ) ))
    {
        linesY_acc.push_back((*itlines));
    }

    else
    {
        continue;
    }

}

And my angle code is

double myAngle(cv::Vec4i lines)
{
    cv::Point p1, p2;
    p1=cv::Point(lines[0], lines[1]);
    p2=cv::Point(lines[2], lines[3]);

   //calculate angle in radian,  if you need it in degrees just do angle * 180 / PI
   return atan2(p2.y - p1.y, p2.x - p1.x);
  } 
user1538798
  • 1,075
  • 3
  • 17
  • 42
  • Step in your code with debugger to see more clearly what it does and how that deviates from what you assume it should do. – Öö Tiib Mar 29 '19 at 08:56
  • Convert output from myAngle to 0 to 360 , it will make the comparison simple. https://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360 – nayab Mar 29 '19 at 09:05
  • debug actual values. Add some lines manually and calculate your expected results manually. – Micka Mar 29 '19 at 09:08
  • @micka , thabks for your help...will do that.. – user1538798 Mar 29 '19 at 10:49

1 Answers1

1

The error is here:

linesX_acc.push_back(lines[i]); <--- i?!! Whats is i?

But here you are use iterator:

linesY_acc.push_back((*itlines));

But your code is very difficult, try this:

std::vector<cv::Vec4i> linesX_acc, linesY_acc;
int boxWidth_threshold  = 35;
double threshold_anglesX = 20.0 /180 * CV_PI;
double threshold_anglesY = 20.0 /180 * CV_PI;

for(const cv::Vec4i& itlines : lines)
{
    cv::Point2f p1(itlines[0], itlines[1]);
    cv::Point2f p2(itlines[2], itlines[3]);

    if (cv::norm(p1 - p2) < boxWidth_threshold)
    {
        continue;
    }

    auto angle = fabs(atan2(p2.y - p1.y, p2.x - p1.x));
    if ( (angle < threshold_anglesX ) || (angle > (CV_PI - threshold_anglesX )) )
    {
        linesX_acc.push_back(itlines);
    }
    else if ( (angle > (CV_PI/2 -threshold_anglesY ) ) &&
              (angle < (CV_PI/2 + threshold_anglesY ) ))
    {
        linesY_acc.push_back(itlines);
    }
    else
    {
        continue;
    }
}
Nuzhny
  • 1,869
  • 1
  • 7
  • 13