Home > Software design >  Implement Boudary Filling algorithm Using Matlab
Implement Boudary Filling algorithm Using Matlab

Time:04-13

OK, it's a Computer Graphics problem.I'm learning Computer Graphics and not good at Matlab either.When I'm trying to implement the Boundary Filling algorithm, I find it does not work. There is the Pseudocode:

void BoundaryFill(int x, int y, COLORREF boundaryValue, COLORREF newValue)
{
     if(GetPixel(x,y) != boundaryValue&&
      GetPixel(x,y) != newValue)// if pixel not already filled and not reach to the 
      boundary then
     {
          SetPixel(x,y,newValue);//fill the pixel
          BoudaryFill(x,y-1,boudaryValue,newValue);
          BoudaryFill(x,y 1,boudaryValue,newValue);
          BoudaryFill(x-1,y,boudaryValue,newValue);
          BoudaryFill(x 1,y,boudaryValue,newValue);
     }
}

Ok, it is not hard to understand.but when I write it by matlab,there's an error. First, I defined a 'img' : img = ones(600,800,3); it can be understanded as a canvas but actually it's a matrix, and the third parameter can be a matrix for RGB like this: img(x,y,[0,0,0]/255); Anyway, I can use imshow(img) to show the canvas as a figure and every pixel's color can be changed. I can draw a closed figure and fill it. Let’s look at the matlab code:

%This is the script
clc;
img = ones(600,800,3);
img = DDA_line(img,3,3,3,50,[0,0,0]/255);
img = DDA_line(img,30,3,30,50,[0,0,0]/255);
img = DDA_line(img,3,3,30,3,[0,0,0]/255);
img = DDA_line(img,3,50,30,50,[0,0,0]/255);
img = Boundaryfill(img,15,26,[0,0,0]/255,[255,0,0]/255);
imshow(img)

% This is DDA_line
function img = DDA_line(img, x1, y1, x2, y2, color)
e = max(abs(x2-x1), abs(y2-y1));
dx = (x2-x1)/e;
dy = (y2-y1)/e;
img(x1,y1,:)=color;
for i = 0:e
    x1 = x1   dx;
    y1 = y1   dy;
    img(round(x1),round(y1),:)=color;
end


%This is the Boudaryfill function
function img = Boundaryfill(img, x, y, boundaryvalue, new)
if  img(x,y,:) ~= boundaryvalue && img(x,y,:) ~= new 
    img(x,y,:) = new;
    img=Boundaryfill(img,x,y-1,boundaryvalue, new);
    img=Boundaryfill(img,x,y 1,boundaryvalue, new);
    img=Boundaryfill(img,x-1,y,boundaryvalue, new);
    img=Boundaryfill(img,x 1,y,boundaryvalue, new);
end
end

It is not complex but when I trying to run this, it post an error. There's the information:

| | the operands and && operator must be able to convert logical scalar value.

it seems like a grammer mistake, but i don't know how to fix it. This can be the first question. I tried to use nested structures to represent the same logic, but the closed figure doesn't filled. It's pity that i can't insert a picture. But when i write the sentence like this

if  img(x,y,:) ~= boundaryvalue

Although it's incomplete, It filled a rectangular.but when i use this incomplet function to fill a triangle it failed and comes to an error. There is the information:

Insufficient memory. The possible reason is that there is infinite recursion in the program.

I have been trying to solve this problem all day without success, and I sincerely hope that you will give me your advice. Thanks!

CodePudding user response:

Be careful with the dimensions and orientations of matrices you are comparing.

This should help:

function img = Boundaryfill(img, x, y, boundaryvalue, new)
    temp = squeeze(img(x,y,:))';
        if  temp ~= boundaryvalue & (temp ~= new)
        img(x,y,:) = new;
        img=Boundaryfill(img,x,y-1,boundaryvalue, new);
        img=Boundaryfill(img,x,y 1,boundaryvalue, new);
        img=Boundaryfill(img,x-1,y,boundaryvalue, new);
        img=Boundaryfill(img,x 1,y,boundaryvalue, new);
    end
end
  • Related