刚接触MATLAB和图像处理。我需要知道如何将图像分割为前景和背景,然后生成二值图像作为输出。

我需要以下内容作为输出:

我已经尝试过通过在线教程来实现这一点,这就是我设法得到的:

这是一个很好的开始,但不完全是我需要的。
我的代码:
I = imread('AssignmentInput.jpg');
figure;
imshow(I);
title('Step-1: Load input image');
img_filtered = I;
for c = 1 : 3
img_filtered(:, :, c) = medfilt2(I(:, :, c), [3, 3]);
end
figure;
imshow(img_filtered);
title('Step-3:Noise Removal');
H = fspecial('gaussian'); % Create the filter kernel.
img_filtered = imfilter(img_filtered,H); % Blur the image.
Mask = im2bw(img_filtered, 0.9); % Now we are generating the binary mask.
img_filtered([Mask, Mask, Mask]) = 0; % Now we have the image.
figure;
imshow(img_filtered);
title('Step-5:Segmented Image');发布于 2016-12-02 10:56:48
为了更好地去除噪声并更清晰地区分前景和背景,您还可以添加形态学操作,如:
se = strel('square',2);
I = imclose(I,se);你也可以尝试不同版本的'strel‘类。下图是使用2个像素的平方进行非闭合操作后的图像

https://stackoverflow.com/questions/40919171
复制相似问题