如何在opencv中构造只有端点的关键线?

pn9klfpd  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(132)

Opencv提供了一个叫做keyline的结构,它是由线段检测器检测出来的。这个结构有各种公共成员,比如Angular ,长度等。
以下类成员没有明确解释,例如class_id、endPointX、endPointY、ePointInOctaveX、ePointInOctaveY、octave、startPointX、startPointY等。

我的问题是,如果我有线的端点,可以构造keyline的结构成员吗?

cv::Point P1 (0,0);
cv::Point P2 (10,10);

cv::line_descriptor::KeyLine L; // How to construct L from P1 and P2?
L.octave = ??;
L.endPointX = ??;
L.endPointY = ??;
L.ePointInOctaveX = ??;
L.ePointInOctaveY = ??;
L.startPointX = ??;
L.startPointY = ??
klr1opcd

klr1opcd1#

我的问题是,如果我有线的端点,可以构造keyline的结构成员吗?
是的你可以

cv::Point P1 (0,0);
cv::Point P2 (10,10);

cv::line_descriptor::KeyLine L;

在上面的代码中,P1P2的x,y坐标对应于下面代码中的extremes变量,L对应于下面的L1octave取0,其余成员变量根据下面代码计算。

// 测试keyLine构建
    cv::Mat testImg = cv::Mat::zeros(500, 500, CV_8UC3);
    cv::line_descriptor::KeyLine L1;

    /* fill KeyLine's fields */
    L1.startPointX = P1.x;
    L1.startPointY = P1.y;
    L1.endPointX = P2.x;
    L1.endPointY = P2.y;
    L1.octave = 0;
    L1.sPointInOctaveX = L1.startPointX;
    L1.sPointInOctaveY = L1.startPointY;
    L1.ePointInOctaveX = L1.endPointX;
    L1.ePointInOctaveY = L1.endPointY;
    L1.lineLength = (float)sqrt(pow(L1.startPointX - L1.endPointX, 2) + pow(L1.startPointY - L1.endPointY, 2));

    /* compute number of pixels covered by line */
    cv::LineIterator li(testImg, Point2f(L1.startPointX, L1.startPointY), Point2f(L1.endPointX, L1.endPointY));
    L1.numOfPixels = li.count;

    L1.angle = atan2((L1.endPointY - L1.startPointY), (L1.endPointX - L1.startPointX));
    L1.class_id = 0;
    L1.octave = 0;
    L1.size = (L1.endPointX - L1.startPointX) * (L1.endPointY - L1.startPointY);
    L1.response = L1.lineLength / max(testImg.cols, testImg.rows);
    L1.pt = Point2f((L1.endPointX + L1.startPointX) / 2, (L1.endPointY + L1.startPointY) / 2);

参考源代码在这里。

相关问题