提取线坐标数组(C ++ OpenCV的) [英] Extract Array of Coordinates from Line (C++ OpenCV)

查看:864
本文介绍了提取线坐标数组(C ++ OpenCV的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++ / OpenCV的我画使用 CV ::行,现在我试图提取其坐标阵列的图像的一行。我试过分配行 CV ::垫,但我得到一个错误,说明我不能从虚空转换为 CV ::垫。有一个简单的方法来获取这些坐标?

Using C++ / OpenCV I've drawn a line on an image using cv::line and now I'm trying to extract an array of its coordinates. I've tried assigning the line to cv::Mat but I get an error stating I cannot convert from void to cv::Mat. Is there a simple way to obtain these coordinates?

感谢您的帮助!

推荐答案

您至少有一对夫妇的选择。假设你知道两个端点 A B 行:

You have at least a couple of options. Assuming that you know the two endpoints A and Bof the line:

1)绘制与行(...)对图像的大小相同的初始化为零屏蔽线,并检索就行了点(其中将与 findNonZero唯一的白穴上面罩)(...)

1) Draw the line with line(...) on an zero initialized mask of the same size of your image, and retrieve the points on the line (which will be the only white points on the mask) with findNonZero(...).

2)使用 LineIterator 检索点,而不需要把他们拉也创造了面具。

2) Use LineIterator to retrieve the points, without the need of drawing them nor creating a mask.

您需要存储您的积分在矢量<点方式>

You need to store your points in a 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 ++ OpenCV的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆