i hope to make a erosion code, without using a imerode. what's the problem? this code, help me
Show older comments
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
Walter Roberson
on 5 Jun 2016
Please post the exact error message, everything in red.
JongHwan Do
on 5 Jun 2016
Edited: Image Analyst
on 5 Jun 2016
JongHwan Do
on 5 Jun 2016
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()?
Answers (2)
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
on 6 Jun 2016
Edited: Ahmet Cecen
on 6 Jun 2016
This can be done faster with a convolution as well, although I am writing this on the fly so there might be an error or two:
B = [ 0 1 0; 1 1 1; 0 1 0]; % Neighborhood Matrix
Filtered = fftshift(ifftn(fftn(IMAGE).*conj(fftn(padarray(B,(sizeIMAGE-sizeB))))));
This has the benefit of giving you both basic morphological operations at once:
%%Erosion
figure;imagesc(Filtered==sum(B(:)));
%%Dilation
figure;imagesc(Filtered>0);
Image Analyst
on 6 Jun 2016
I don't see how that's the same as an erosion. Fourier filtering and convolution are linear operations and erosion is non-linear. Fourier filtering cannot do non-linear operations like erosion and dilation. You're simply filtering the image spectrum by multiplying it by the spectrum of the structuring element, which has the effect of convolving the image with the B structuring element. This will blur the image, not extract local minimums.
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
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.
JongHwan Do
on 6 Jun 2016
0 votes
Categories
Find more on Morphological Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!