Why does dilate and erode images produce black images?

11 ビュー (過去 30 日間)
Matthew Worker
Matthew Worker 2021 年 11 月 18 日
編集済み: John Kelly 2021 年 12 月 8 日
Did i do something wrong with nhood variable?
%% 1. Dilation and Erosion
%a. Apply dilation and erosion to “lung.jpg” using 3x3 structuring element (all pixel value is 1).
%Before applying dilation and erosion you must perform the thersholding to make the input image into binary one (threshold = 128).
lung = imread('lung.jpg');
bilung = imbinarize(lung,128);
nhood = strel(ones(3,3));
dilatedlung = imdilate(bilung,nhood);
erodedlung = imerode(bilung,nhood);
%b. Display original image, dilation image and erosion image within the same figure.
figure
subplot(1,3,1);
imshow(lung);
title('original img');
subplot(1,3,2);
imshow(mat2gray(dilatedlung));
title('dilated img');
subplot(1,3,3);
imshow(mat2gray(erodedlung));
title('eroded img');
%c. Get morphological gradient of “lung.jpg” and display it.
basic_gradient = dilatedlung - erodedlung;
figure
imshow(basic_gradient);
title('gradient');
  1 件のコメント
Rena Berman
Rena Berman 2021 年 12 月 8 日

(Answers Dev) Restored edit

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2021 年 11 月 18 日
Try
basic_gradient = imabsdiff(dilatedlung, erodedlung);

Image Analyst
Image Analyst 2021 年 11 月 18 日
You didn't use imbinarize correctly. Get rid of the 128.
%b. Display original image, dilation image and erosion image within the same figure.
figure
subplot(1,3,1);
imshow(lung);
title('original img');
subplot(1,3,2);
imshow(mat2gray(dilatedlung));
title('dilated img');
subplot(1,3,3);
imshow(mat2gray(erodedlung));
title('eroded img');
%c. Get morphological gradient of “lung.jpg” and display it.
basic_gradient = imabsdiff(dilatedlung , erodedlung);
figure
imshow(basic_gradient);
title('gradient');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by