C++ opencv 从索引中获取 cv::Point

C++ opencv get cv::Point from index

我想通过像素索引从 cv::Mat 中提取数据。这适用于颜色,例如cv::Vec3b,但是在尝试获取点信息时,它崩溃并指出:

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

这是我使用的代码:


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 

我该如何解决这个问题,以便我可以在特定的像素索引处获取点?

你不能,使用 Mat::at()。 (这是为了检索像素 content,而不是位置)

右下角可以是:

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

或者在你的计算中:

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

(imo,使用 max_index 的整个想法有点不切实际......)