需要一些解密 opencv 线角度
Need some deciphering opencv line angles
这个问题困扰了我一个下午。下面是一个代码片段,我正在尝试编写代码以将垂直线 (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);
}
错误在这里:
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;
}
}
这个问题困扰了我一个下午。下面是一个代码片段,我正在尝试编写代码以将垂直线 (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);
}
错误在这里:
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;
}
}