i hope to make a erosion code, without using a imerode. what's the problem? this code, help me

3 views (last 30 days)
clc
close all;
clear all;
s=imread('abc.tif');
g=im2bw(s);
b= [0 1 0 ; 1 1 1 ; 0 1 0];
[p q]=size(b);
[m n]=size(g);
for i = 1:m
for j = 1:n
neighborhood = getNeighborhood(s(i, j), b);
g(i, j) = min(neighborhood);
end
end
--------------------
it makes error ( can not define function 'getneighborhood' .. ) ..
how can i make erosion code (without imerode) please complete this code, and why error happend?
  4 Comments
Image Analyst
Image Analyst on 5 Jun 2016
Why do you think that there should be a getNeighborhood() function? It's not a built-in function. You'd need to write it. Perhaps you meant the obsolete getneighbors()?

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 5 Jun 2016
Try the attached code. It could be made more compact but I left in the binary image stuff that you wanted for some reason, and I made some intermediate variables just to make it clearer and more explicit.
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontsize = 24;
% Read in original image.
grayImage = imread('cameraman.tif');
% grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
subplot(2,2,1);
imshow(grayImage);
axis on;
title('Original image', 'Fontsize', fontsize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% The user wanted a binary image for some reason, though it was never used.
% So calculate it anyway though it's not used or needed for the local min image.
binaryImage=im2bw(grayImage);
subplot(2,2,2);
imshow(binaryImage);
axis on;
title('Binary image that is not used or needed', 'Fontsize', fontsize);
% Define structuring element.
se = logical([0 1 0 ; 1 1 1 ; 0 1 0]);
[p, q]=size(se);
halfHeight = floor(p/2);
halfWidth = floor(q/2);
% Initialize output image
localMinImage = zeros(size(grayImage), class(grayImage));
% Perform local min operation, which is morphological erosion.
for col = (halfWidth + 1) : (columns - halfWidth)
for row = (halfHeight + 1) : (rows - halfHeight)
% Get the 3x3 neighborhood
row1 = row-halfHeight;
row2 = row+halfHeight;
col1 = col-halfWidth;
col2 = col+halfWidth;
thisNeighborhood = grayImage(row1:row2, col1:col2);
% Apply the structuring element
pixelsInSE = thisNeighborhood(se);
localMinImage(row, col) = min(pixelsInSE);
end
end
subplot(2,2,3);
imshow(localMinImage);
axis on;
title('Eroded image', 'Fontsize', fontsize);
  4 Comments
Ahmet Cecen
Ahmet Cecen on 6 Jun 2016
It most certainly is equivalent to a textbook binary erosion, although now I am thinking there might be an issue in the grayscale case.
Ahmet Cecen
Ahmet Cecen on 6 Jun 2016
Edited: Ahmet Cecen on 6 Jun 2016
Hmm, yeah doesn't satisfy the infimum requirement in the grayscale case. Turns out I didn't actually know the proper definition of erosion, and got away with it due to eroding exclusively in binary images. (Which ends up being 1 if the pattern fits, 0 if it doesn't.)
It is still possible to do this with a convolution for small structural elements, but it becomes very ugly. Like instead of:
b = [0 1 0; 1 1 1; 0 1 0];
You can do
b = [0 e0 0; e3 e6 e9; 0 e12 0];
will work for a 0-255 case. You can then simply split the 15 digit number into 5 and find the infimum in linear time, still beating the for loop.

Sign in to comment.


JongHwan Do
JongHwan Do on 6 Jun 2016
why it's red color?;; cuz not gray image??? then, how can i make erosion from binary image ?

Community Treasure Hunt

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

Start Hunting!