I need to use Python and Numpy to take a grayscale image (as a Numpy array), then iterate through it by pixel in order to differentiate the image in the X a direction. I can't use any functions to do this automatically, I need to iterate unfortunately. I need to use the derivative: F(x, y) = F(x, y+1) - F(x, y) to each pixel and return the output in the form of a new image (in a Numpy array).
A simple 4 pixel example 10 15 5 25 Would return 5 10 20
I want to take the absolute value (eliminating negative values) and have the output width be 1 column shorter than the input (since no calculation can be performed on the last column).
I can read the array using np.nditer, but I could really use some help figuring out how to apply that calculation to each element and return the results.
I figured this out in Java fairly quickly, can any of the Python gurus out there help me convert this?
public class ArrayTest {
public static void main(String[] args) {
int[] arrayOne = { 5, 10, 20, 5 };
int[] newArray = new int[arrayOne.length];
for (int i = 0; i < arrayOne.length - 1; i++) {
newArray[i] = Math.abs(arrayOne[i + 1] - arrayOne[i]);
System.out.println(newArray[i]);
}
}
}
np.abs(np.diff(F,axis=1))?np.gradient