从线中提取坐标数组 (C++ OpenCV)

Extract Array of Coordinates from Line (C++ OpenCV)

使用 C++/OpenCV 我已经使用 cv::line 在图像上画了一条线,现在我正在尝试提取它的坐标数组。我已经尝试将行分配给 cv::Mat,但我收到一条错误消息,指出我无法从 void 转换为 cv::Mat。有没有简单的方法可以得到这些坐标?

感谢您的帮助!

你可以看到这个答案。我想这就是你的问题所需要的,Finding points in a line

Opencv 具有 Line Iterator 功能。浏览文档!

这是一个示例用法!

LineIterator it(img, pt1, pt2, 8);
for(int i = 0; i < it.count; i++, ++it)
{
    Point pt= it.pos(); 
   //Draw Some stuff using that Point pt
}

您至少有几个选择。假设您知道直线的两个端点 AB

1) 在与图像大小相同的零初始化蒙版上用 line(...) 画线,并用 findNonZero(...).

2) 使用 LineIterator 检索点,无需绘制它们或创建遮罩。

您需要将积分存储在 vector<Point> 中。

#include <opencv2/opencv.hpp>
#include <vector>

using namespace std;
using namespace cv;

int main(int, char** argv)
{
    Mat3b image(100,100); // Image will contain your original rgb image

    // Line endpoints:
    Point A(10,20);
    Point B(50,80);


    // Method: 1) Create a mask
    Mat1b mask(image.size(), uchar(0));
    line(mask, A, B, Scalar(255));

    vector<Point> points1;
    findNonZero(mask, points1);

    // Method: 2) Use LineIterator
    LineIterator lit(image, A, B);

    vector<Point> points2;
    points2.reserve(lit.count);
    for (int i = 0; i < lit.count; ++i, ++lit)
    {
        points2.push_back(lit.pos());
    }

    // points1 and points2 contains the same points now!

    return 0;
}