c++ - OpenCV: Computing superpixel centroids -
background:
i have computed slic superpixels of image using gslicr, gives "per-pixel map" of image superpixels indices (0 number of superpixels-1).
this map pointer integer const array (const int*
) containing indices.
i want compute centroids of each superpixel using opencv.
coming matlab background, using regionprops
:
segments = vl_slic(myimage, regionsize, regularizer); stats = regionprops(segments, 'centroid'); centroids = cat(1, stats.centroid);
i don't know how done using opencv.
questions:
(i) how convert const int*
array cv::mat
?
(ii) how compute superpixel centroids matrix in (i)?
as first questions seems answered, focus on second question. used following code compute mean coordinates (i.e. spatial centroids) of each superpixel:
/** \brief compute mean coordinates of each superpixel (i.e. spatial centroids). * \param[in] labels matrix of type cv_32sc1 holding labels each pixel * \param[out] means spatial centroids (or means in y , x axes) of superpixels */ void getmeans(const cv::mat &labels, std::vector<cv::vec2f> &means) { // count superpixels or highest superpixel index: int superpixels = 0; (int = 0; < labels.rows; ++i) { (int j = 0; j < labels.cols; ++j) { if (labels.at<int>(i, j) > superpixels) { superpixels = labels.at<int>(i, j); } } } superpixels++; // setup means 0 vectors. means.clear(); means.resize(superpixels); (int k = 0; k < superpixels; k++) { means[k] = cv::vec2f(0, 0); } std::vector<int> counts(superpixels, 0); // sum y , x coordinates each superpixel: (int = 0; < labels.rows; ++i) { (int j = 0; j < labels.cols; ++j) { means[labels.at<int>(i, j)][0] += i; // computing mean (i.e. row or y axis) means[labels.at<int>(i, j)][1] += j; // computing mean j (i.e. column or x axis) counts[labels.at<int>(i, j)]++; } } // obtain averages dividing size (=number of pixels) of superpixels. (int k = 0; k < superpixels; ++k) { means[k] /= counts[k]; } } // means ...
if need mean color, method require image argument, remaining code can adapted computing mean colors.
Comments
Post a Comment