整个下午一直困扰着我。下面是一个代码段,我正在尝试将代码分类为具有一定公差角的垂直线(linesY_acc)和水平线(linesX_acc)。
我不知道为什么仅前面的lineX_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);
}
答案 0 :(得分: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;
}
}