Reputation: 543
I have b/w image spited into 4 segments. I need to know how many regions are there at each segment.
Here is an example with better explanation.
I am wondering if there is ready-made API available for this in any of most popular c++ image processing libraries? ( like gd, cimg, opencv?)
Upvotes: 0
Views: 461
Reputation: 208003
You can just use OpenCV's connectedComponents()
and the answer you seek is one less than the number it returns:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv)
{
if(argc!=2){
std::cerr << "ERROR: Please supply an image name" << std::endl;
exit(1);
}
// Load up the image and check
Mat image = imread(argv[1], IMREAD_GRAYSCALE);
if(image.empty()){
std::cerr << "ERROR: Unable to load image" << std::endl;
exit(1);
}
// Calculate connected components
Mat label;
int n=connectedComponents(image,label,8,CV_16U);
std::cout << "n=" << n << std::endl;
}
Returns 5.
Returns 4.
Returns 3.
Returns 3.
Upvotes: 1