This reminds me on one example from book "Opencv 2 computer vision application programming cookbook". All you should do was to do this:
// Get the binary map
cv::Mat binary;
//binary = cv::imread("binary.bmp", 0); // prevent loading of pre-converted image
cvtColor(image, binary, CV_BGR2GRAY); // instead convert original
binary = binary < 65; // apply threshold
Whole code (excluding water segmentation header) would be something like this:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "watershedSegmentation.h"
int main()
{
// Read input image
cv::Mat image = cv::imread("group.jpg");
if (!image.data)
return 0;
// Display the image
cv::namedWindow("Original Image");
cv::imshow("Original Image", image);
// // Get the binary map
cv::Mat binary;
//binary = cv::imread("binary.bmp", 0); // prevent loading of pre-converted image
cvtColor(image, binary, CV_BGR2GRAY); // instead convert original
binary = binary < 60; // apply threshold
// Display the binary image
cv::namedWindow("Binary Image");
cv::imshow("Binary Image", binary);
// Eliminate noise and smaller objects
cv::Mat fg;
cv::erode(binary, fg, cv::Mat(), cv::Point(-1, -1), 6);
// Display the foreground image
cv::namedWindow("Foreground Image");
cv::imshow("Foreground Image", fg);
// Identify image pixels without objects
cv::Mat bg;
cv::dilate(binary, bg, cv::Mat(), cv::Point(-1, -1), 6);
cv::threshold(bg, bg, 1, 128, cv::THRESH_BINARY_INV);
// Display the background image
cv::namedWindow("Background Image");
cv::imshow("Background Image", bg);
// Show markers image
cv::Mat markers(binary.size(), CV_8U, cv::Scalar(0));
markers = fg + bg;
cv::namedWindow("Markers");
cv::imshow("Markers", markers);
// Create watershed segmentation object
WatershedSegmenter segmenter;
// Set markers and process
segmenter.setMarkers(markers);
segmenter.process(image);
// Display segmentation result
cv::namedWindow("Segmentation");
cv::imshow("Segmentation", segmenter.getSegmentation());
// Display watersheds
cv::namedWindow("Watersheds");
cv::imshow("Watersheds", segmenter.getWatersheds());
cv::waitKey();
return 0;
}