gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:11:42 25 4
gpt4 key购买 nike

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

感谢您的帮助!

最佳答案

您至少有几个选择。假设您知道两个端点 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;
}

关于c++ - 从线中提取坐标数组 (C++ OpenCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com