在MATLAB中理解和实现细化算法

时间:2012-01-16 10:08:48

标签: algorithm matlab image-processing

我正在尝试在Matlab中实现自己的细化算法来理解细化算法。我正在关注http://fourier.eng.hmc.edu/e161/lectures/morphology/node2.html并实现我自己的代码,但结果不正确。

这是我的代码:

%for the sake of simplicity, the outermost pixels are ignored.
for x = 2:1:511
    for y = 2:1:511

        % if this pixel is not black, then, proceed in.
        if (frame2(y,x) > 0)                

            % the pos(1 to 8) here are for the surrounding pixels.
            pos(1) = frame2(y-1,x-1);
            pos(2) = frame2(y, x-1);
            pos(3) = frame2(y+1, x+1);
            pos(4) = frame2(y+1, x);
            pos(5) = frame2(y+1, x-1);
            pos(6) = frame2(y, x-1);
            pos(7) = frame2(y-1, x-1);
            pos(8) = frame2(y-1, x);

            nonZeroNeighbor = 0;
            transitSequence = 0;
            change = 0;

            for n = 1:1:8
                % for N(P1)
                if (pos(n) >= 1)
                    nonZeroNeighbor = nonZeroNeighbor + 1;
                end

                % for S(P1)
                if (n > 1)
                    if (pos(n) ~= change)
                        change = pos(n);
                        transitSequence = transitSequence + 1;
                    end
                else
                    change = pos(n);
                end

            end

            % also for S(P1)
            if ((nonZeroNeighbor > 1 && nonZeroNeighbor < 7) || transitSequence >= 2)
                markMatrix(y,x) = 1;
                fprintf(1, '(%d,%d) nonzero: %d transit: %d\n', y,x, nonZeroNeighbor, transitSequence);
            else %this else here is for the reverse.

            end

        end
    end
end


    for x = 2:1:511
        for y = 2:1:511
            if (markMatrix(y,x) > 0)
                frame2(y,x) = 0;
            end
        end
    end

    savePath = [path header number2 '.bmp']; 

    imwrite(frame2, savePath, 'bmp'); %output image here, replacing the original

从上面的站点,它将函数S(P1)声明为:

  

“S(P1):序列中的0到1(或1到0)个转换次数(P2,P3,...,P9)”

对于这部分,我的代码低于“%(S)(P1)”和“%(S)(P1)”注释。 我是否正确实现了此功能?我得到的输出图像只是空白。什么都没有。

对于正确的输出,我知道存在逻辑问题。关于该网站,它声明:

  

当形状的一部分只有2像素宽时,所有像素都是边界点,将被标记然后删除。

此问题暂时无视。

1 个答案:

答案 0 :(得分:3)

我已经解决了这个问题,并认为我设法让算法运行起来。我一路上做了几个小编辑(请参阅下面的代码了解详细信息),但也发现了初始实现的两个基本问题。

首先,您假设所有操作都在第1步和第2步的第一步中完成,但实际上您需要让算法在图像上工作一段时间。这是迭代形态学步骤“吃掉”图像的典型特征。这是添加while循环的原因。

其次,你计算S()的方法是错误的;它计算了从0到1和1到0的两个步骤,当它不应该计数两次时它并没有处理P(2)和P(9)周围的对称性。

我的代码:

%Preliminary setups
close all; clear all;
set(0,'DefaultFigureWindowStyle','Docked')

%Read image
frame2 = imread('q1.jpg');

%Code for spesific images
%frame2(:,200:end) = [];
%frame2 = rgb2gray(frame2);

%Make binary
frame2(frame2 < 128) = 1;
frame2(frame2 >= 128) = 0;

%Get sizes and set up mark
[Yn Xn] = size(frame2);
markMatrix = zeros(Yn,Xn);

%First visualization
figure();imagesc(frame2);colormap(gray)
%%

%While loop control
cc = 0; 
changed = 1;
while changed && cc < 50;

    changed = 0;
    cc = cc + 1;
    markMatrix = zeros(Yn,Xn);

    for x = 2:1:Xn-1
        for y = 2:1:Yn-1

            % if this pixel is not black, then, proceed in.
            if (frame2(y,x) > 0)                

                % the pos(2 to 9) here are for the surrounding pixels.
                pos(1) = frame2(y,   x);
                pos(2) = frame2(y-1, x);
                pos(3) = frame2(y-1, x+1);
                pos(4) = frame2(y,   x+1);
                pos(5) = frame2(y+1, x+1);
                pos(6) = frame2(y+1, x);
                pos(7) = frame2(y+1, x-1);
                pos(8) = frame2(y,   x-1);
                pos(9) = frame2(y-1, x-1);

                nonZeroNeighbor = 0;
                transitSequence = 0;
                change = pos(9);

                for n = 2:1:9

                    %N()
                    nonZeroNeighbor = sum(pos(2:end));

                    %S()
                    if (double(pos(n)) - double(change)) < 0
                        transitSequence = transitSequence + 1;
                    end
                    change = pos(n);

                end

                %Test if pixel is to be removed
                if ~( nonZeroNeighbor == 0 || nonZeroNeighbor == 1 ...
                    ||nonZeroNeighbor == 7 || nonZeroNeighbor == 8 ...
                    ||transitSequence >= 2)

                        markMatrix(y,x) = 1;
                        fprintf(1, '(%d,%d) nonzero: %d transit: %d\n', ...
                            y,x, nonZeroNeighbor, transitSequence);
                end

            end
        end
    end

    %Mask out all pixels found to be deleted
    frame2(markMatrix > 0) = 0;

    %Check if anything has changed
    if sum(markMatrix(:)) > 0;changed = 1;end

end

%Final visualization
figure();imagesc(frame2);colormap(gray)

Lines before algorithm is run

Lines after algorithm is run