I have a 3-phase image in which I have segmented using automated thresholding (multithresh) and ‘imquantize’ functions. I do have lots of holes in the image without any filtering operation. However, these holes are reduced when I use a median filter although there are still quite a few of them left in-spite of the filtering.
Applying the ‘imfill’ function from this point results in “over-filling” as observed in the red-circled part of the image shown below.
The code is as follows:
%# Read in image
I = imread(‘original_image.jpg');
figure, imshow(I),axis off, title('Original Image');
%# Filter image
I = medfilt2(I);
% figure, imshow(I), title('Median-filtered image')
%# Segment image
thresh = multithresh(I, 2);
BW = imquantize(I, thresh);
figure, imshow(BW,[]),axis off, title('Segmented Image');
%# Fill holes
BW2 = imfill(BW,'holes');
figure, imshow(BW2, []); title('Filled holes image');
I am just wondering if there is any better way to handle this situation. Do you think using the ‘multithresh’ and ‘imquantize’ functions are good enough for the segmentation? Can the watershed do better and is it even necessary here?
In general, please what can I do to enhance the quality of my output image?
The reason I ask is because if you scale ‘imshow’ of the original image you will notice that most of the black phase touches the solid (white phase). However, the automated-segmentation doesn't accurately capture this as the segmented image has rings of the intermediate (grey) phase around the solid phase. How do I also handle this?
Many thanks for your anticipated help/suggestions.
Whether your approach is good enough or not depends a lot on what you want to achieve here. For example, how smooth do the borders need to be?
To your specific question: Your quantized image has three levels, 0 (black), 1 (gray), and 2 (white). You seem to want to close the little black holes in the gray area. For this, just create a separate binary image with the gray pixels only, and then combine back into your multilevel image (which you shouldn't call BW, since the Matlab documentation uses that for binary images everywhere, and you should be consistent if you can).
Similarly, you can run
bwareaopen
on the "black" channel to remove the white dots here and there.You can pull the "white" channel out similarly out of
BW
for watershed on the large spherical particles, and you'll most likely get a pretty good result (if the white particles are what you're after).If you want to make the thin gray borders around the white particles disappear, you could try morphological opening. Basically, you shrink the gray area by a few pixels (erosion), and then your re-grow it by a few pixels (dilation). A thin gray line will disappear completely, so there won't be anything to grow back from.
There will be some trade-off, since the meniscus of the gray phase will no longer be as sharp. You may be able to recover somewhat with a follow-up opening of the black channel, if needed.