使用 OpenCV 从线中提取点

Extracting Points from Lines using OpenCV

本文关键字:提取 OpenCV 使用      更新时间:2023-10-16

>我正在尝试使用C++语言中的openCV从图像上的线条中提取点。 该行被编程为显示在图像上,但我需要知道如何从线条中提取点并将其输入到文本文件中?

您可以使用

cv::LineIterator类来获取栅格线的每个点,例如:

// grabs pixels along the line (pt1, pt2)
// from 8-bit 3-channel image to the buffer
LineIterator it(img, pt1, pt2, 8);
LineIterator it2 = it;
vector<Vec3b> buf(it.count);
for(int i = 0; i < it.count; i++, ++it)
    buf[i] = *(const Vec3b)*it;
// alternative way of iterating through the line
for(int i = 0; i < it2.count; i++, ++it2)
{
    Vec3b val = img.at<Vec3b>(it2.pos());
    CV_Assert(buf[i] == val);
}