libxelar.so
libxelar.so

Reputation: 543

number of distinct regions in the binary image (c++)

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. ocr

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

Answers (1)

Mark Setchell
Mark Setchell

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;
}

enter image description here

Returns 5.


enter image description here

Returns 4.


enter image description here

Returns 3.


enter image description here

Returns 3.

Upvotes: 1

Related Questions