Search code examples
matlabimage-processingimage-segmentationwatershed

How to prevent inaccurate segmentation of enclosed background regions in Watershed Algorithm?


I'm using the watershed algorithm to segment bright spots on a dark background. The code is provided below, along with some images it generates.

In the second image, I've marked with red crosses the areas of enclosed background which are segmented as 'cells' (they're not biological cells, just using the word) - this is incorrect, they're part of the background, just enclosed by 'cells'. I see that this creates a false minimum, any help on how to prevent this?

    % Improve contrast, binarize
RFP_adjust = imadjust(RFP_blur, stretchlim(RFP_blur, 0.001));
figure, imshow(RFP_adjust), title('Contrast adjust');
RFP_binarized = imbinarize(RFP_adjust);
RFP_perimeters = bwperim(RFP_binarized);
% figure, imshow(RFP_binarized), title('Otsu thresholding');

    %2B - SEGMENTATION BY WATERSHED METHOD
    % Discover putative cell centroids and process
RFP_maxs = imextendedmax(RFP_adjust,  3000);
RFP_maxs = imclose(RFP_maxs, strel('disk',5));
RFP_maxs = imfill(RFP_maxs, 'holes');
RFP_maxs = bwareaopen(RFP_maxs, 5);
RFP_max_overlay = imoverlay(RFP_adjust, RFP_perimeters | RFP_maxs, [1 .3 .3]);
figure, imshow(RFP_max_overlay), title('Maxima');

    % Obtain complement - maxima become low-points (required for watershed)
RFP_comp = imcomplement(RFP_adjust);
RFP_imposemin = imimposemin(RFP_comp, ~RFP_binarized | RFP_maxs);
figure, imshow(RFP_imposemin), title('Inverted Maxima');

    % Apply watershed
RFP_watershed = watershed(RFP_imposemin);
mask = im2bw(RFP_watershed, 1);
overlay3 = imoverlay(RFP_adjust, mask, [1 .3 .3]);
figure, imshow(overlay3), title('Segmented cells');

    % Segment
RFP_cc = bwconncomp(RFP_watershed);
RFP_label_matrix = labelmatrix(RFP_cc);
whos labeled;
RFP_label = label2rgb(RFP_label_matrix, @spring, 'c', 'shuffle');
figure, imshow(RFP_label), title('Cells segmented');

Image 0 - result for image titled 'Maxima' (i.e. adjusted original image with maxima and outlines overlaid).enter image description here

Image 1 - the result for image titled 'inverted maxima' Result for image titled 'Inverted Maxima'

Image 2 - the result for image titled 'Cells segmented' enter image description here


Solution

  • I would suggest something like what is done in the example included for the watershed function: use the background mask to set those pixels to Inf, perform the watershed operation, then set the background pixels in the result to 0. I believe you could change the watershed section of your code like so to achieve this:

    % Apply watershed
    RFP_watershed = RFP_imposemin;             % Added
    RFP_watershed(~RFP_binarized) = Inf;       % Added
    RFP_watershed = watershed(RFP_watershed);  % Modified
    RFP_watershed(~RFP_binarized) = 0;         % Added
    mask = im2bw(RFP_watershed, 1);
    overlay3 = imoverlay(RFP_adjust, mask, [1 .3 .3]);
    figure, imshow(overlay3), title('Segmented cells');