#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core/operations.hpp>
#include "opencv2/core/cvdef.h"
#include "opencv2/core/version.hpp"
#include "opencv2/core/base.hpp"
#include "opencv2/core/cvstd.hpp"
#include "opencv2/core/traits.hpp"
#include "opencv2/core/matx.hpp"
#include "opencv2/core/types.hpp"
#include "opencv2/core/mat.hpp"
#include "opencv2/core/persistence.hpp"
using namespace std;
using namespace cv;
int main(void)
{
RNG rng(12345);
//Mat src = imread("daA2500-14uc__21706175__20170522_162253225_0001.tiff");
Mat src = imread("daA2500-14uc__21706175__20170522_162253225_0100.tiff");
Mat bw;
cvtColor(src,bw,CV_BGR2GRAY);
Mat thresh;
Mat thresh1;
double ret = threshold(bw,thresh,0,255,THRESH_BINARY | THRESH_OTSU);
Mat kernel = Mat(3, 3, CV_8U);
Mat opening;
morphologyEx(thresh,opening,MORPH_OPEN,kernel,Point(-1,-1),2);
Mat sure_bg;
dilate(opening,sure_bg,kernel,Point(-1,-1),3);
Mat dist_transform;
Mat labels;
distanceTransform(opening,dist_transform,labels,DIST_L2,5);
double minn,maxx;
minMaxLoc(dist_transform,&minn,&maxx);
Mat sure_fg;
double ret1 = threshold(dist_transform, sure_fg, 0.7*maxx, 255,0);
sure_fg.convertTo(sure_fg,CV_8U);
Mat unknown;
subtract(sure_bg,sure_fg,unknown);
Mat markers;
double ret2 = connectedComponents(sure_fg,markers);
markers = markers + Scalar::all(1);
//add(markers,Scalar::all(1),markers);
for(int i=0; i<unknown.rows; i++)
{
for(int j=0; i<unknown.cols; j++)
{
if (unknown.at<int>(i,j) == 255)
{
markers.at<int>(i,j) = 0;
}
}
}
watershed(src,markers);
for(int i=0; i<markers.rows; i++)
{
for(int j=0; i<markers.cols; j++)
{
if (markers.at<int>(i,j) == -1)
{
src.at<Vec3b>(i,j) = Vec3b(255, 0, 0);
}
}
}
}