0

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.

Community
  • 1
  • 1
User2201
  • 103
  • 5
  • What kind of filtering did you try? – Max Feb 22 '17 at 11:43
  • If you smooth the edges, it won't be a segmented (indexed) image anymore. Is that what you want? – beaker Feb 22 '17 at 16:42
  • @user2201 Maybe instead of smoothing the edges you should try to apply a circle-fitting algorithm on your indexed image. Than you can plot the circles that you found over your indexed image with a little thicker `LineWidth` and you will get sharper contours on your circles. And then - depending on the precision you need - you can index the circle-fitted pixels as either inside or outside the segment. – Max Feb 23 '17 at 11:05

1 Answers1

0

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.

Max
  • 1,471
  • 15
  • 37
  • I do realize as **beaker** pointed out that this won't be an indexed image any longer, although with median filter i don't get the edges smoothed but the labels remain and some of the tiny spots at the bottom left corner are removed as well. Nevertheless, many thanks for this suggestion i have learnt something from it, especially as i could use the resulting image for visualization purposes. – User2201 Feb 23 '17 at 03:21
  • @User2201 Well, the median filter is not made to smooth pictures. The median filter is designed to remove the so called salt-and-peper noise. Even applying it multiple times wouldn't smoothen the edges of the image which is actually the good thing about it. Glad I could help anyway and good luck with your project. – Max Feb 23 '17 at 10:11