【问题标题】:Need some deciphering opencv line angles需要一些破译opencv线角度
【发布时间】:2019-03-29 08:51:37
【问题描述】:

整个下午一直困扰着我。下面是一个代码 sn-p,我正在尝试将代码分类为具有一定容差角度的垂直 (linesY_acc) 和水平线 (linesX_acc)。

我不知道为什么只有前面的linesX_acc 被填充,而linesY_acc 什么都没有?也许这是一个小错误,但我就是不明白为什么

需要你的帮助! 提前致谢

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;
    }

}

我的角码是

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);
  } 

【问题讨论】:

  • 用调试器进入你的代码,更清楚地看到它做了什么,以及它与你假设它应该做的有什么不同。
  • 将输出从 myAngle 转换为 0 到 360 ,这将使比较变得简单。 stackoverflow.com/questions/1311049/…
  • 调试实际值。手动添加一些行并手动计算您的预期结果。
  • @micka ,谢谢你的帮助......会这样做......

标签: c++ opencv stdvector


【解决方案1】:

错误在这里:

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

但是在这里你使用迭代器:

linesY_acc.push_back((*itlines));

但是你的代码很难,试试这个:

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    相关资源
    最近更新 更多