connected components in OpenCV

前端 未结 6 2061
既然无缘
既然无缘 2020-11-27 04:58

I am looking for an OpenCV function that can find connected components and perform a few tasks on them ( like getting the number of pixels, contour, list of pixels in the ob

6条回答
  •  天涯浪人
    2020-11-27 05:20

    You can use cv::connectedComponentsWithStats() function.

    Here is an example.

        // ...
        cv::Mat labels, stats, centroids;
        int connectivity = 8; // or 4
        int label_count = cv::connectedComponentsWithStats(src, labels, stats, centroids, connectivity);
        for (int i = 0; i < label_count; i++)
        {
            int x = stats.at(i, cv::CC_STAT_LEFT);
            int y = stats.at(i, cv::CC_STAT_TOP);
            int w = stats.at(i, cv::CC_STAT_WIDTH);
            int h = stats.at(i, cv::CC_STAT_HEIGHT);
            int area = stats.at(i, cv::CC_STAT_AREA);
            double cx = centroids.at(i, 0);
            double cy = centroids.at(i, 1);
    
            // ...
        }
    

提交回复
热议问题