Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm MATLAB中细化算法的理解与实现_Algorithm_Matlab_Image Processing - Fatal编程技术网

Algorithm MATLAB中细化算法的理解与实现

Algorithm MATLAB中细化算法的理解与实现,algorithm,matlab,image-processing,Algorithm,Matlab,Image Processing,我试图在Matlab中实现我自己的细化算法,以了解细化算法。我正在遵循并实现自己的代码,但结果不正确 这是我的密码: %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)

我试图在Matlab中实现我自己的细化算法,以了解细化算法。我正在遵循并实现自己的代码,但结果不正确

这是我的密码:

%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
%为了简单起见,将忽略最外面的像素。
对于x=2:1:511
对于y=2:1:511
%如果该像素不是黑色,则继续。
如果(帧2(y,x)>0)
%这里的位置(1到8)用于周围的像素。
位置(1)=帧2(y-1,x-1);
位置(2)=帧2(y,x-1);
位置(3)=帧2(y+1,x+1);
位置(4)=帧2(y+1,x);
位置(5)=帧2(y+1,x-1);
位置(6)=帧2(y,x-1);
位置(7)=帧2(y-1,x-1);
位置(8)=帧2(y-1,x);
非零邻居=0;
transitSequence=0;
变化=0;
对于n=1:1:8
%对于N(P1)
如果(位置(n)>=1)
非零邻居=非零邻居+1;
结束
%对于S(P1)
如果(n>1)
如果(位置(n)~=变更)
变更=位置(n);
transitSequence=transitSequence+1;
结束
其他的
变更=位置(n);
结束
结束
%同样适用于S(P1)
if((非零邻居>1和非零邻居<7)| |传输序列>=2)
markMatrix(y,x)=1;
fprintf(1,(%d,%d)非零:%d传输:%d\n',y,x,非零邻居,传输序列);
else%这里的else是相反的。
结束
结束
结束
结束
对于x=2:1:511
对于y=2:1:511
如果(矩阵(y,x)>0)
帧2(y,x)=0;
结束
结束
结束
保存路径=[path header number2'.bmp'];
imwrite(frame2,保存路径,'bmp');%在此处输出图像,替换原始图像
从上面的站点,它将功能S(P1)表示为:

S(P1):序列(P2,P3,…,P9)中0到1(或1到0)的转换数

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

对于正确的输出,我知道有一个逻辑问题。关于该网站,它指出:

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


这个问题暂时可以忽略

我尝试了一下这个问题,认为我成功地使算法工作了。在此过程中,我做了一些小的编辑(请参阅下面的代码了解详细信息),但也发现了您最初实现中的两个基本问题

首先,您假设所有操作都将在步骤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)
%初步设置
全部关闭;清除所有;
集合(0,'DefaultFigureWindowStyle','Docked')
%读取图像
frame2=imread('q1.jpg');
%特殊图像代码
%第二帧(:,200:end)=[];
%frame2=rgb2gray(frame2);
%使二进制
帧2(帧2<128)=1;
帧2(帧2>=128)=0;
%获取尺寸并设置标记
[Yn Xn]=尺寸(帧2);
markMatrix=0(Yn,Xn);
%首次可视化
图();imagesc(框架2);彩色地图(灰色)
%%
%While环控制
cc=0;
改变=1;
改变时&cc<50;
改变=0;
cc=cc+1;
markMatrix=0(Yn,Xn);
对于x=2:1:Xn-1
对于y=2:1:Yn-1
%如果该像素不是黑色,则继续。
如果(帧2(y,x)>0)
%这里的位置(2到9)用于周围的像素。
位置(1)=帧2(y,x);
位置(2)=帧2(y-1,x);
位置(3)=帧2(y-1,x+1);
位置(4)=帧2(y,x+1);
位置(5)=帧2(y+1,x+1);
位置(6)=帧2(y+1,x);
位置(7)=帧2(y+1,x-1);
位置(8)=帧2(y,x-1);
位置(9)=帧2(y-1,x-1);
非零邻居=0;
transitSequence=0;
更改=位置(9);
对于n=2:1:9
%N()
非零邻居=和(位置(2:end));
%S()
如果(双重(位置(n))-双重(变化))<0
transitSequence=transitSequence+1;
结束
变更=位置(n);
结束
%测试是否要删除像素
如果(非零邻居==0 | |非零邻居==1。。。
||非零邻居==7 | |非零邻居==8。。。
||传输序列>=2)
markMatrix(y,x)=1;
fprintf(1,(%d,%d)非零:%d传输:%d\n'。。。
y、 x,非零邻居,传输序列);
结束
结束
结束
结束
%遮罩掉所有被删除的像素
帧2(markMatrix>0)=0;
%检查是否有任何变化
如果和(markMatrix(:)>0;改变=1;结束
结束
%最终可视化
图();imagesc(框架2);彩色地图(灰色)

Pass 1规则状态:标记至少不满足一个条件的任何边缘像素P1=1。我认为在这种情况下,
transitSequence
应该<2,以便标记中心像素。此外,序列
0-1-0
1-0-1