Question

I have to apply the method of erosion to an image. However, I'm having some difficulties. I'm starting with a basic example, but at this point all pixels of my image will stay with the value of the first pixel. How to solve this problem?

public static int[] erosion(int array[])
{
    //int array1[] = new int[array.length];  
    // System.arraycopy(array, 0, array1, 0, array.length);          
    for(int i=1; i < array.length; i++)
    {
        if (array[i-1] < array[i]) {
            array[i] = array[i-1];
        }
        if (array[i+1] < array[i]) {
            array[i] = array[i+1];
        }
    }
    return array;       
}
Was it helpful?

Solution

The problem is that the previous value in the array has been assigned at the next iteration pass. For in-place substitution you can try something like this:

int previous = array[0];
for (int i = 0; i < array.length - 1; i++) {
    int res = Math.min(previous, array[i]);
    res = Math.min(res, array[i + 1]);
    previous = array[i];
    array[i] = res;
}
array[array.length - 1] = Math.min(previous, array[array.length - 1];

Edit: extended to first and last values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top