how to use matlab to smooth the edge of a picture

2020-07-17 15:46发布

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!!!

3条回答
成全新的幸福
2楼-- · 2020-07-17 15:50

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)
查看更多
再贱就再见
3楼-- · 2020-07-17 15:58

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.

查看更多
欢心
4楼-- · 2020-07-17 16:12

You could use isocontour [1] edge detection and then average adjacent pixels along the contour to smoothen the boundary.

[1] http://www.mathworks.com/matlabcentral/fileexchange/30525-isocontour

查看更多
登录 后发表回答