3

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. Original image

Segmented Image after filtering

Applying the ‘imfill’ function from this point results in “over-filling” as observed in the red-circled part of the image shown below. FilledHoles image

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.

User2201
  • 103
  • 5

1 Answers1

1

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).

% pull out the gray "channel"
grayPixels = BW==1; % will have ones everywhere there's gray, and 0 otherwise

% to close holes up to a maximum size, invert the image (holes become islands) 
% and eliminate small islands with bwareaopen
invGray = ~grayPixels;
invGray = bwareaopen(invGray,100); % closes holes up to 100pix size - adjust
grayPixels = ~invGray; % imshow to view result

% merge gray channel back in. Note we want black->gray, 
% but we don't want white->gray. 
% Since black/gray/white are 0/1/2, if we take the maximum of the gray 
% (which is 0/1) and your "BW" (which is 0/1/2), we replace 0 in BW with 1 
% wherever we have closed a hole

BW = max(BW,double(grayPixels);
imshow(BW,[]);

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.

% take the gray "channel" again (after closing the small holes)
grayPixels = BW == 1;
grayPixelsOpened = imopen(grayPixels,strel('disk',3)); % play with the radius 3 to get the desired result

% everything that used to be gray and is no longer so needs to be turned black 
% in the original image
BW(grayPixels~=grayPixelsOpened) = 0;

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.

Jonas
  • 74,690
  • 10
  • 137
  • 177
  • thank you for your response and advise. The result doesn't quite seem to have changed that much even when i adjust the pixel size. Nevertheless, i have just realized from subsequent analysis that the major issue is the accuracy of the segmentation (see the last paragraph just before the last line of my question). Any suggestions on that please? – User2201 Jan 05 '17 at 15:45
  • @User2201: I have made a suggestion for removing the gray border. Also, when you say that the result doesn't change much with hole-filling, did you mean that your final analysis result doesn't change, or that the holes weren't filled? – Jonas Jan 05 '17 at 16:24
  • Thank you @Jonas. The meniscus as well as points of contact between the three phases are about the most important feature i am interested in. A disk size of 1 seems to be better than 2 or more. However, the details of the resulting curvature are not as fine in most parts as compared to the original. Any more suggestions as to what else to consider? \what about the type of structuring element? As for the hole-filling, i meant that the holes weren't filled. – User2201 Jan 06 '17 at 07:51
  • Strange that the holes weren't filled. But since I don't have access to the image, there's not much that I can do about that. Anyway, since it's the edges that are most important, you may want to go after them directly, for example you could run a Canny edge detector (on filtered image of course - may want to consider increasing median filter window to 5x5 or 7x7, or to use anisotropic diffusion filtering), or roll your own detection on the gradient image. – Jonas Jan 06 '17 at 10:09
  • Hi Jonas! i'm so sorry i had to go off for such a long time. I was on enormous pressure hence had to focus on the main code itself. I think i may have got a solution to the question by tweaking your approach a bit. Many thanks for that. What i want now is to find a way to smooth the edges in the images. I will make a new post for this as well as put up the code i used. Nevertheless, i'll still like your code because it give me the impetus to try. Thanks again! – User2201 Feb 22 '17 at 01:51