I 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!!!
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:
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With