Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use matlab to smooth the edge of a picture

enter image description hereI get a picture below but the picture has a very ugly edges. I want to use matlab programming to smooth the edges of the picture and make it look more pretty,any ideas or ways make sense?

thank you!!!

like image 858
user2579274 Avatar asked Dec 26 '22 16:12

user2579274


2 Answers

Here is an option (edited):

I = im2double(imread('ht4Za.jpg'));
% Segment the object:
gs = rgb2gray(I);
Object=~im2bw(gs, graythresh(gs));

% Smoothen the mask:
BW = bwmorph(bwconvhull(Object), 'erode', 5);
Mask=repmat(BW,[1,1,3]);

% Iterate opening operation:
Interp=I;
for k=1:5
    Interp=imopen(Interp, strel('disk',20));
end

% Keep original pixels, add the newly generated ones and smooth the output:
Interpolated(:,:,1)=medfilt2(imadd(I(:,:,1).*Object, Interp(:,:,1).*~(BW==Object)), [4 4]);
Interpolated(:,:,2)=medfilt2(imadd(I(:,:,2).*Object, Interp(:,:,2).*~(BW==Object)), [4 4]);
Interpolated(:,:,3)=medfilt2(imadd(I(:,:,3).*Object, Interp(:,:,3).*~(BW==Object)), [4 4]);

% Display the results:
Masked=imadd(Interpolated.*im2double(Mask), im2double(~Mask));
imshow(Masked);

Result:

enter image description here

It's a bit rough, but that'll give you a start. You can try to fiddle with the number of iteration and the size of the circular filter and the median filter. Try changing the median with average, etc.

like image 78
Cape Code Avatar answered Dec 28 '22 23:12

Cape Code


You could use imopen to morphologically open the RGB image (dilation and erosion). The second input argument to imopen function is a structuring element which defines the amount of smoothing required in the morphological operation. For example, below is a code where I apply a disk structuring element of radius 10.

img = imread('http://i.stack.imgur.com/ht4Za.jpg');
imopenBW = imopen(img, strel('disk',10));
imshow(imopenBW)
like image 34
Lokesh A. R. Avatar answered Dec 29 '22 01:12

Lokesh A. R.