openCV watershed

howdy,
i’m trying to implement this watershed algorithm in opencv (c flavour) in order to do some segmenting on a depth image but am stumped re: the last step.
https://docs.opencv.org/3.4/d3/db4/tutorial_py_watershed.html
does anybody have any idea what the equivalent function is for cv.connectedComponents()?
thanks++ for any tips
jeremy

Hello @mantissa . The current openCV version in OF is 4.0.1

The function you are looking for is included
https://docs.opencv.org/4.0.1/d3/dc0/group__imgproc__shape.html#gaedef8c7340499ca391d459122e51bef5

At the bottom of the page an example is linked, maybe it can be helpful to you?

@mantissa I think you will need to use the C++ version for that function.

When I am using the C++ opencv api in ofxOpenCv I do something like this:

    //using this approach - https://answers.opencv.org/question/56042/detach-two-blob/
    cv::Mat inputChar = cv::cvarrToMat(myofxCvImage.getCvImage());
    cv::Mat inputFloat;
 
    cv::Mat kernel = cv::Mat::ones(mErodeX, mErodeY, CV_8UC1);
    erode(inputChar, inputChar, kernel);
       
    cv::distanceTransform(inputChar, inputFloat, CV_DIST_L2, 5);
    cv::normalize(inputFloat, inputFloat, 0, 255., cv::NORM_MINMAX);
    
    inputFloat.convertTo(inputChar, CV_8UC1);
    
    erode(inputChar, inputChar, kernel);

    //now convert back to old C style ofxOpenCv friendly image 
    IplImage dumb = cvIplImage(inputChar);
    
    cvCopy(&dumb, myofxCvImage.getCvImage());
    myofxCvImage.flagImageChanged(); // just incase 

I think something like above will work the same for you.
I am guessing cv::connectedComponents will be what you are after?

Btw the code above does something similar, create interior blobs from shapes that are touching.
You might need to play with mErodeX, and mErodeY ( as ofParameters ) but it should do a similar thing as the article. Note: we do a blur and threshold with ofxOpenCv after the steps above to shrink the interior blob.