首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >flood_fill算法在Matlab语言中的实现

flood_fill算法在Matlab语言中的实现
EN

Stack Overflow用户
提问于 2014-10-07 15:09:38
回答 1查看 83关注 0票数 0

我现在正在用MATLAB开发一个flood_fill算法,我有一个小问题,花了我很多时间:

代码语言:javascript
复制
function [ homoPoints ] = area2Points( matrix )
%AREA2POINTS makes an area of one's to only one one in the middle of the area
% Input    :     Matrix with areas of one's
% Output   :     result has one point in the middle of every former area   

myMatrix = matrix;
[row, col] = find(myMatrix);
curPoint = [ row(1),col(1) ];
area = [curPoint(1),curPoint(2)];
myMatrix(curPoint(1),curPoint(2)) = 0;
myMatrix = fill(curPoint(1),curPoint(2),myMatrix);

%Problem
    function[ matrix ] = fill(x,y,matrix)
        area
        %matrix(x,y) = 0;    %theoretisch unnötig
        %If the pixel under curPoint is a 1..
        if(matrix(x + 1 , y) == 1)
            area = vertcat(area,[x+1 , y]);
            matrix(x+1,y) = 0;
            fill(x+1,y,matrix);
        end
        %If the pixel left from curPoint is a 1..
        if(matrix(x, y - 1) == 1)
            area = vertcat(area,[x , y-1]);
            matrix(x,y-1) = 0;
            fill(x,y-1,matrix);
        end
        %If the pixel over curPoint is a 1..
        if(matrix(x - 1, y) == 1)
            area = vertcat(area,[x-1 , y]);
            matrix(x-1,y) = 0;
            fill(x-1,y,matrix);
        end
        %If the pixel right from curPoint is a 1..
        if(matrix(x , y + 1) == 1)
            area = vertcat(area,[x , y+1]);
            matrix(x,y+1) = 0;
            fill(x,y+1,matrix);
        end
        return
    end

所以问题是: flood_fill在所有像素上正确运行,但当所有像素都设置为0时,它不会停止!例如,在这个矩阵中:

代码语言:javascript
复制
testMatrix = zeros(20);
testMatrix(5:10,5:10) = 1;

..it在第一列下降,在第六列上升,在第七列下降..第10位向上(7,10 -> 6,10 -> 5,10),然后是(8,10 -> 9,10 -> 10,10 -> 10,9 ...)。这种效果从何而来?

EN

回答 1

Stack Overflow用户

发布于 2014-10-07 15:32:02

对不起。我把注意力集中在错误的事情上..我只是通过直接访问myMatrix而不是参数来解决它。它工作得很好!

致以最好的问候,R93!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26230426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档