c++ - Get coordinates of white pixels (OpenCV) -


in opencv (c++) have b&w image shapes appear filled white (255). knowing this, how can coordinate points in image theses objects are? i'm interested on getting white pixels coordinates.

is there cleaner way this?

std::vector<int> coordinates_white; // temporaly store coordinates "white" found  (int = 0; i<img_size.height; i++) {     (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 coordinates matrix each row represents *(x,y)* pair cv::mat coordinates = cv::mat(coordinates_white.size()/2,2,cv_32s,&coordinates_white.front()); 

there built-in function cv::findnonzero

returns list of locations of non-zero pixels.

given binary matrix (likely returned operation such cv::threshold(), cv::compare(), >, ==, etc) returns of non-zero indices cv::mat or std::vector<cv::point>

for example:

cv::mat binaryimage; // input, binary image cv::mat locations;   // output, locations of non-zero pixels cv::findnonzero(binaryimage, locations); // access pixel coordinates point pnt = locations.at<point>(i); 

or

cv::mat binaryimage; // input, binary image vector<point> locations;   // output, locations of non-zero pixels cv::findnonzero(binaryimage, locations); // access pixel coordinates point pnt = locations[i]; 

Comments