Extract array of coordinates from string (C ++ OpenCV)

Using C ++ / OpenCV I have drawn a line in the image with cv::line

and now I am trying to extract an array of my coordinates. I tried to assign a string cv::Mat

, but I get an error: I cannot convert from void to cv::Mat

. Is there an easy way to get these coordinates?

Thanks for the help!

+3


source to share


2 answers


You have at least a few options. Assuming you know the two endpoints A

and B

lines:

1) Draw a line with line(...)

on a zero initialized mask of the same size as your image and extract the points on the line (which will be the only white points on the mask) with findNonZero(...)

.

2) Use LineIterator

to extract points without having to draw or create a mask.



You need to keep your points in 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;
}

      

+3


source


You can see this answer. I suppose this is what you are after Searching for points in a string .

Opencv has a Line Iterator function . Go through the documentation!



Here's a usage example!

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
}

      

+3


source







All Articles