Question Is there any flood fill function that returns filled color points?

thippu

Active member
Joined
Mar 6, 2019
Messages
27
Location
Bangalore
Programming Experience
Beginner
I want to get the points from the flood filling algorithm and Is there any flood fill method that returns the filled points?

  1. I have a rectangle in a circle and the rectangle is divided by a line and if a user clicks on any part of the rectangle I will get clicked mouse point and I want to color the clicked part of the rectangle.image
  2. I would like to save the filled color points also for further use.
  3. I implemented code using flood filling algorithm in c# but it is not working as expected I'm getting out of memory exception, I could not able to find what is wrong with the code.
code:
void FillZone(Bitmap image,int initialX, int initialY,Color fillColor,Color boundaryColor,List<Point> savePoint)

{

Stack<Point> points=new Stack<Point>();

points.Push(new Point(initialX,initialY));

while(points.Count>0)

{

Point currentPoin=poins.Pop();

int x=currentPoin.X;

int y=currentPoin.Y;

if((x>0&& x<pictureBoxImage.Width)&&(y>0&&y<pictureBoxImage.Height))

{

Color current=image.GetPixel(x,y);

if((current!=boundaryColor))&&(current!=fillColor))

{

image.SetPixel(x,y,fillColor);

savePoints.Add(new Point(x,y));

points.Push(new Point(x+1,y));

points.Push(new Point(x-1,y));

points.Push(new Point(x,y-1));

points.Push(new Point(x,y+1));

}

}

}

}



private void MenuItem_Select_Click(object sender,EventArgs e)

{

Point p=new Point();

p.X=m_right_button_X1;

p.Y=m_right_button_Y1;

Bitmap pic=(Bitmap)pictureBoxImage.Image.Clone();

List<Point>regionPoints=new List<Point>();

FillZone(pic,p.X,p.Y,Color.Red,Color.Black,regionPoints);

}
 
Last edited:
How big is the area that you trying to flood fill? In general each Point take up at least 8 bytes due to it storing 2 32-bit integers. If you are running your code as a 32-bit application, then you are limited to only about 2GB of RAM to play with. Check your build settings. Even if you have "Any CPU" set, if the "Prefer 32-bits" is checked then there you have it.

If whatever you are doing causes you to have an area that you'll end up with a lot of memory usage, consider looking at some the scan-line fill algorithms instead of the simple generic flood fill algorithms. If it's not obvious, the reason why the scan line algorithms will save you memory is because you only need to start the beginning and ending X coordinates for each scan line. Even if in the end you still need all the points, what you can do is create a method that returns an IEnumerable<Point> that generates the points on the fly. As long as you never call ToList() or ToArray() on the returned enumerable, any memory usage should be minimized.
 
Last edited:
picture box is using 640*480 image
How big is the area that you trying to flood fill? In general each Point take up at least 8 bytes due to it storing 2 32-bit integers. If you are running your code as a 32-bit application, then you are limited to only about 2GB of RAM to play with. Check your build settings. Even if you have "Any CPU" set, if the "Prefer 32-bits" is checked then there you have it.

If whatever you do, you'll end up with a lot of memory usage, consider looking at some the scan-line fill algorithms instead of the simple generic flood fill algorithms.
 
picture box is using 640*480 image
But how much of that is actually within your boundary color? Anyway, even if you had "leaks" in your boundary color pixels, you shouldn't be using more than 2.5MB of RAM for your savePoints.

On closer look, though, it looks like your points stack is growing geometrically because you are covering points that you have already done previously. Probably the root cause of your out of memory condition.
 
Back
Top Bottom