anarchy99
anarchy99

Reputation: 1023

Is It Possible to Store cv::Point to cv::Mat?

I want to store Points to an opencv matrix (cv::Mat), is it possible or not? I've tried it with this code:

cv::Mat_<cv::Point> matPoint;
matPoint.at<cv::Point>(0) = cv::Point(2,2);
std::cout << matPoint.at<cv::Point>(0) << std::endl;

Actually, it was compiled successfully, but when I run the code I got an "Floating point exception". I know my above code is wrong, but I don't know the other way to do it.

Any help would be appreciated. Thank you

Upvotes: 2

Views: 5373

Answers (2)

berak
berak

Reputation: 39796

another version:

cv::Mat_<cv::Point> matPoint;
matPoint.push_back(Point(2,2));

Upvotes: 2

Niko
Niko

Reputation: 26730

The correct way to do this would be:

// Create 1x1 matrix and initialize all elements with (0,0)
cv::Mat_<cv::Point> matPoint(1, 1, cv::Point(0, 0));

// Access the element with index 0
matPoint(0) = cv::Point(2, 2);

// Alternative syntax for targeting the elements by their two-dimensional index:
std::cout << matPoint(0, 0) << std::endl;

The .at<cv::Point>(0) syntax should also work but is less convenient.

Upvotes: 2

Related Questions