matlabimage-processingimage-enhancement

How do i smoothen the edges of a multicomponent image?


I have an image in which i would like to smoothen its edges. There was a bit of a challenge in getting a more accurate segmentation. I however got a solution by adapting the suggestion from: What can I do to enhance my image quality?.

The original images is here: Original image

and segmented image as well Segmented image

The code i used is as follows:

%# Read in image
Img = imread('image_name.png');

%# Apply filter
h   = fspecial('average');
Img = imfilter(Img, h);

%# Segment image
Img    = rgb2gray(Img);
thresh = multithresh(Img, 2);
Iseg   = imquantize(Img, thresh);    
figure, imshow(Iseg,[]), title('Segmented Image'); 

%# separate channels
blackPixels = (Iseg == 1);
grayPixels  = (Iseg == 2);
whitePixels = (Iseg == 3);


%# grow white channel
whitePixels_dilated = imdilate(whitePixels, strel('disk', 4, 4));

%# Add all channels 
Iseg(whitePixels | whitePixels_dilated) = 3;            
figure, imshow(Iseg,[]);

My challenge right now is to smoothen the edges of the solid (whitePixels) or the edges of all objects. I have no idea how to do this. I have tried filtering but that only takes off the small spots. Please any help, ideas, or suggestions or advice is greatly appreciated. Thank you.


Solution

  • I would suggest applying a rectangular filter multiple times. Here's an approach of how to do this:

    I=imread('Orl1r.png');
    I_gray=rgb2gray(I);
    I_filtered=I_gray; % initialize the filtered image
    for ii=1:10
       I_filtered=imfilter(I_filtered,1/25*ones(5)); % apply rectangular-filter multiple times
    end
    figure
    subplot(1,2,1)
    imshow(I,[0 255]);
    subplot(1,2,2);
    imshow(I_filtered,[0 255])
    

    Here's what the filtered image would look like: enter image description here

    Hope this helps.

    EDIT: Instead of the rectangular filter you could also use a Gaussian one. But the general idea of applying multiple times persists. You can create a Gaussian filter for exapmle using f=fspecial('gaussian',5,6) which creates a 5x5 filtermask with sigma=6.