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:
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 …)