【问题标题】:Accessing the values of a line in opencv在opencv中访问一行的值
【发布时间】:2013-12-16 17:47:47
【问题描述】:

我有一个程序可以检测帧中的一条线,我的问题是:如何访问形成这条线的像素的值,我有这条线的极坐标:角度和到 0 的距离:这是我获取行位置的代码:

....................
cv::Canny(dilationResult,canny,50,200,3);
cv::HoughLines(canny,lineQ,1,CV_PI/180,200);
    for( size_t i = 0; i < lineQ.size(); i++ )
        {
          float rho = lineQ[i][0], theta = lineQ[i][1];
          cv::Point pt1, pt2;
          double a = cos(theta), b = sin(theta);
          double x0 = a*rho, y0 = b*rho;
          pt1.x = cvRound(x0 + 1000*(-b));
          pt1.y = cvRound(y0 + 1000*(a));
          pt2.x = cvRound(x0 - 1000*(-b));
          pt2.y = cvRound(y0 - 1000*(a));
          angle = atan2f((pt2.y-pt1.y),(pt2.x-pt1.x))*180.0/CV_PI;  // getting the angle of the lines 

         std::cout << "angle " << angle<< std::endl;
          line( mask, pt1, pt2, cv::Scalar(0,0,255), 3, CV_AA);
        }

假设我得到了这个框架 如何获取行的值?

提前感谢您的帮助!

【问题讨论】:

  • 这些值是什么意思?你想要每个像素的坐标?你的目标是什么?
  • 在上面的例子中,所有的值都是 0,因为这条线是黑色的,为此还需要位置!
  • 您有 rho 和 theta,因此您可以使用它们来计算线上的点。另见stackoverflow.com/questions/18782873/…

标签: c++ opencv


【解决方案1】:

您可以使用LineIterator 获取线上的每个点。例如(假设3通道图像):

cv::LineIterator it(dilationResult, pt1, pt2, 8);
std::vector<cv::Vec3b> buf(it.count);
std::vector<cv::Point> points(it.count);

for(int i = 0; i < it.count; i++, ++it)
{
    buf[i] = *(const cv::Vec3b)*it;
    points[i] = it.pos();
}

另外,因为您使用的是 Canny,所以会检测到线条的两个边缘。

【讨论】:

    【解决方案2】:

    要遍历行像素,请按照 B... 显示。

    补充一点:如果您需要亚像素精度的线条位置,您可以使用参数 alpha [0 到 1] 参数化线条:

    cv::Point2f subpixel;
    subpixel.x = alpha*pt1.x + (1.0f - alpha)*pt2.x
    subpixel.y = alpha*pt1.y + (1.0f - alpha)*pt2.y
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      • 2012-02-14
      • 2011-03-18
      • 2013-07-28
      相关资源
      最近更新 更多