# Confusion in the position of image coordinates

I am struggling to work out how image coordinates are calculated and how to be consistent when finding particular points in an image using these coordinates for further analysis. I don't think it is necessarily a bug, but it makes it tricky to work with and modify images.

This is best shown by the following example.

We make a test image:

testIm=ImagePad[Image[IdentityMatrix], {{0, 10}, {0, 0}}] We then replace one of the pixels (at position 20,3) with the value 2 (so it is easy to find.)

testIm2=ReplacePixelValue[testIm, {20, 3} -> 2] We now try to find the coordinates of this pixel with the value 2.

coords=Position[ImageData[testIm2], 2 | 2.]


which gives the result {13,20}? What is going on here ? How can we be sure of choosing the right pixel values ?

It seems that the following is happening. In the command ReplacePixelValue the pixels are numbered in the first coordinate from left to right, then in the second coordinate from bottom to top, i.e. a standard x-y Cartesian system. In the command Position[ImageData[]] the result is coordinates numbered firstly from top to bottom, and secondly from left to right. To convert the coordinates then we have in the first system
$$(x_i,y_i)$$ which gives coordinates $$(y_i,1+x_{dim}-x_i)$$ where $$x_{dim}$$ is the dimension of the image in the x-direction in the first coordinate system. I hope this helps other people dealing with this problem