Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ opencv get cv::Point from index

I would like to extract data from a cv::Mat via the index of a pixel. This works fine for the colur e.g. cv::Vec3b, however when attempting to get the point information, it crashes stating:

Error: Assertion failed (elemSize() == sizeof(_Tp)) in cv::Mat::at,

Here is the code I’m using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


cv::Mat src = imread(image_path, cv::IMREAD_COLOR);
int max_index = src.size().area() // gives total amount of pixels (i.e. max index)
std::cout << src.at<cv::Vec3b>(max_index -1) << std::endl; // gives me colour of final pixel
std::cout << src.at<cv::Point>(max_index -1) << std::endl; // ERROR should give point of final pixel but crashes 

How can I fix this so that I can get the Point at a specific pixel index?

>Solution :

you can’t, using Mat::at(). (it is meant to retrieve the pixel content, not the position)

the bottom-right point would be either:

Point(src.cols-1, src.rows-1);

or in your calculation:

Point((max_index-1)/src.cols, (max_index-1)%src.cols);

(imo, the whole idea of using max_indexis somewhat impractical …)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading