Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my binary image dilation function doesn't work properly?

I'm having a kind of trouble since I'm new the concept Image Analysis and the tool Matlab. What I have in my mind does not work well as lines of codes.

I'm trying to dilation function for binary images. It has to widen the given binary images.

This is my main page:

I = imread('logo_XXXX.png');
binaryImage = im2bw(I, 0.4);
s = ones(3,3,'int8');
i = dilate(binaryImage,s);
figure, imshow(i);

This is dilate.m function:

function [i] = dilate(I,s)
[Irows,Icols] = size(I);
i=I;
Itemp = I;
for row=1:Irows
    for col=1:Icols
        x = intersectAt(Itemp,s,row,col);
        if x == 1
            i(row,col)=1;
        else
            i(row,col)=0;
        end
    end
end

And this is istersectAt.m function:

function [i] = intersectAt(I,s,row,col)
[Srows,Scols] = size(s);
[Irows,Icols] = size(I);
i=0;
rowx = row - int8(Srows/2);
colx = col - int8(Scols/2);

for r=1:Srows
    for c=1:Scols
        if rowx+r <= 0 || rowx+r > Irows || colx+c <= 0 || colx+c > Icols
            continue;
        elseif I(rowx+r,colx+c) == 1 && s(r,c)==1
            i = 1;
        end
    end
end

These codes must be widen this image:

enter image description here

however, in some point it doesn't work properly s.t:

enter image description here

If you help me fix my code I would be appriciated. If you want to know about dilation you can follow this url: http://www.mathworks.com/help/toolbox/images/f18-12508.html

Matlab has this function in its library but i need to implement my own function.

like image 725
Yunus Eren Güzel Avatar asked Mar 08 '26 08:03

Yunus Eren Güzel


1 Answers

You should avoid loops as much as possible in matlab.

If you need to write your own function just do:

s=ones(3);
i=(conv2(double(binaryImage),s,'same')>0)

From your example:

enter image description here

I can obtain:

enter image description here

like image 137
Oli Avatar answered Mar 10 '26 06:03

Oli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!