在OpenCV(C++)中,我有一张黑白图像,其中一些形状填充为白色(255)。在此情况下,如何获得图像中这些对象的坐标点?我想获取所有白色像素的坐标。是否有比这更简洁的方法?
std::vector<int> coordinates_white; // will temporaly store the coordinates where "white" is found
for (int i = 0; i<img_size.height; i++) {
for (int j = 0; j<img_size.width; j++) {
if (img_tmp.at<int>(i,j)>250) {
coordinates_white.push_back(i);
coordinates_white.push_back(j);
}
}
}
// copy the coordinates into a matrix where each row represents a *(x,y)* pair
cv::Mat coordinates = cv::Mat(coordinates_white.size()/2,2,CV_32S,&coordinates_white.front());