如何逐个排列这个像素地址?

时间:2014-01-29 14:35:25

标签: image-processing matlab

我在做地图处理。我解开了迷宫,得到了转弯的地址。以下是我的结果。

(28,136)
(122,136)
(344,391)
(548,493)
(548,191)
(344,191)
(122,391)

但地址顺序不正确。我的预期输出是:

28 136
122 136
122 391
344 391
344 191
548 191
549 493

Map

我的节目如下。我编程也得到方向

for(i=1:tblob)
    if (shape(i).order==1)
        map(i).cen=shape(i).cen;
    end
end

White=[255 255 255];
i=1;
order=1;

left=0;
right=0;
up=0;
down=0;

left(1)=0;
right(1)=0;
up(1)=0;
down(1)=0;
left(2)=0;
right(2)=0;
up(2)=0;
down(2)=0;
left(3)=0;
right(3)=0;
up(3)=0;
down(3)=0;
left(4)=0;
right(4)=0;
up(4)=0;
down(4)=0;

left(5)=0;
right(5)=0;
up(5)=0;
down(5)=0;
left(6)=0;
right(6)=0;
up(6)=0;
down(6)=0;
left(7)=0;
right(7)=0;
up(7)=0;
down(7)=0;  
left(8)=0;
right(8)=0;
up(8)=0;
down(8)=0;

x=map(i).cen(1,:);
y=map(i).cen(2,:);

for(z=1:100)
    xi=x+1;
    xj=x-1;
    yi=y+1;
    yj=y-1;

    colorvalxi=color(y,xi,RGB);
    colorvalxi
    colorvalxj=color(y,xj,RGB);
    colorvalxj
    colorvalyi=color(yi,x,RGB);
    colorvalyi
    colorvalyj=color(yj,x,RGB);
    colorvalyj

    if(i>1)
        s=i-1;
    else
        s=1;
    end

    if (left(s)==0)
        if(colorvalxi==White)
            n=1;
        end
    else
        if (right(s)==0)
            if(colorvalxj==White)
                n=2;
            end
        else
            if(up(s)==0)
                if (colorvalyi==White)
                    n=3;
                end
            else
                if(down(s)==0)
                    if(colorvalyj==White)
                        n=4;
                    end
                else
                    break;
                end
            end
        end
    end

    switch(n)
        case 1 %RIGHT
            for(ks=1:1000)
                color_val=color(y,x,RGB);
                if(color_val==White)
                    x=x+1;
                else
                    right(i)=1;
                    instruction(i)=1;
                    i=i+1;
                    x=x-1;
                    map(i).cen(1,1)=x;
                    map(i).cen(2,1)=y;
                    break;
                end
            end

        case 2 %LEFT
            for(ks=1:1000)
                color_val==color(y,x,RGB)
                if(color_val==White)
                    x=x-1;
                else
                    left(i)=1;
                    instruction(i)=2;
                    i=i+1;
                    x=x+1;
                    map(i).cen(1,1)=x;
                    map(i).cen(2,1)=y;
                    break;
                end
            end

        case 3 %DOWN
            for(ks=1:1000)
                color_val=color(y,x,RGB);
                if(color_val==White)
                    y=y+1;
                else
                    down(i)=1;
                    instruction(i)=3;
                    i=i+1;
                    y=y-1;
                    map(i).cen(1,1)=x;
                    map(i).cen(2,1)=y;
                    break;
                end
            end

        case 4 %UPWARD
            for(ks=1:1000)
                color_val=color(y,x,RGB);
                if(color_val==White)
                    y=y-1;
                else
                    up(i)=1;
                    instruction(i)=4;
                    i=i+1;
                    y=y-1;
                    map(i).cen(1,1)=x;
                    map(i).cen(2,1)=y;
                    break;
                end
            end

    end                

end

0 个答案:

没有答案