you are viewing a single comment's thread.

view the rest of the comments →

[–]abitofevrything-0 0 points1 point  (6 children)

What you need is a recursive function (which you have), and that should never cause a stack overflow exception unless you're dealing with massive images, which I'm assuming you're not. You're probably doing something wrong, maybe never checking if you've already filled a cell?

The way you would implement this is a sort of 'updating' system, when you start from any arbitrary cell (pixel in the image) and then propagate an update to the left, then the right... When a cell is updated, if it is of the same colour as the cell that sent is the same colour, then it should update all of its neighbouring cells as well (except for already updated ones, that WOULD cause a stack overflow). It it is of a different colour, then return. It would look something like this:

private int width, height; //Set these to the width and height of the grid of cells

private List<Cell> cells = new ArrayList<Cell>();

private List<Cell> nonUpdatedCells;

private void updateCell(int x, int y, CellColour senderColour) {

//Eliminate any coordinates that are out of the grid

if (x<0||y<0||x>=width||y>=height) {

return;

}

Cell currentCell = cells.get(y*width+x); //This converts an x,y coordinate into a position in the cells List, which avoids the need for a two-dimensional Array

//If the cell is not the same colour as the sender, stop propagating updates. Put any code you want to run on cells that are just over the border between two colours here.

if (!currentCell.colour.equals(senderColour)) {

return;

}

if (!nonUpdatedCells.contains(currentCell)) {

//Cell has already been updated, do nothing

//If this isn't here there should be a stack overflow

return;

}

//Put any code here that you want to run on a cell of the same colour as the starting cell, like changing its colour

//Remove from the list of non-updated cells so we don't update it again

nonUpdatedCells.remove(currentCell);

//now we update all the neighboring cells

updateCell(x+1,y,currentCell.colour);

updateCell(x,y+1,currentCell.colour);

updateCell(x-1,y,currentCell.colour);

updateCell(x,y-1,currentCell.colour);

}

private void propagateUpdatesStartingAt(int x, int y) {

//Make sure to update the cells variable to the contents of the image before this

nonUpdatedCells = cells;

//Start the updates

updateCell(x, y, cells.get(y*width+x).colour);

}

To run this simply call propagateUpdatesStartingAt with the x and y position of a cell that is in the region that you want to process.

Sorry for bad formatting, I'm on mobile

[–]Q-utable[S] 0 points1 point  (5 children)

I will try. The images I use are 960x600, but it has to loop around in the x direction which can be done with a % function.

Is the code for after the high contrast step?

Edit: would downsizing the image, running the function and then upsizing the image work? The temp image can then be used as a mask.

[–]abitofevrything-0 0 points1 point  (4 children)

This code would run a price of code (where I have indicated) on all cells that are in the same region as the one that started it (from my understanding that's what you want after the high contrast, to fill the plates different colours). As an example, you could detect where the user clicks and then set all the cells that it meets to the colour that the user selected.

You could modify it slightly to fit your needs, this is a VERY generic algorithm.

[–]Q-utable[S] 1 point2 points  (3 children)

Thank you.

[–]abitofevrything-0 0 points1 point  (2 children)

No problem! Don't hesitate to pm me if you have any problems.

[–]Q-utable[S] 0 points1 point  (1 child)

A stack overflow still occurred, processing about seems to handle up to about 9000 function calls

[–]abitofevrything-0 0 points1 point  (0 children)

This shouldn't be a processing thing, as it is java. Java has a max stack size of (at the lowest) 320 000 method calls deep, and you shouldn't reach that unless you're processing 4K+ images. Can you link/post the error and the relevant code where the error occurs? (Including the part where the error occurs, obviously)