The Wolfram tutorial for image processing gives these equations to convert between an image y coordinate and an array row index, r



But these equations are not algebraically consistent. If you solve the first for y, you don't get the second!

It seems to me that h is used differently in the two equations. In the first equation h is actually the number of array rows, and in the second it's really the number of rows+1, which corresponds to the depiction of h in the earlier figures of the tutorial as the topmost "coordinate" in an image. If this is so, surely a different letter should be used.

Is there really this very basic error in the documentation? Or am I missing something fundamental?


Yes, this is a mistake. The first equation should be $r = h - (y+1/2)$.

The continuous domain covers $[0,w]\times[0,h]$. Pixels are a discretization of the space. As the existence of both ImageValuePositions and PixelValuePositions shows, there are two different ways to specify pixels. If you specify a pixel by its coordinate then the bottom left pixel is $(0.5, 0.5)$. If you specify a pixel by its indices, it will be $(x_p,y_p) = (x+0.5, y+0.5)$ where $(x,y)$ are the corresponding coordinates. That is, the bottom left pixel will be specified by $(1,1)$.

In the formulas you mention, $y$ is the pixel coordinate. $y_p = y+0.5$ is the corresponding index. If $r=0$ then we see that, according to the first equation, $y-1/2 = h$. This is wrong because in this case $y$ is a coordinate outside the domain. We know that the index should be $y_p = h$, and therefore we can see that the equation should be $y+1/2 = h$.

It would perhaps have been clearer to write the equations in terms of the pixel indices, in this case the equations are $$ r = h - y_p,\quad y_p= h -r. $$

  • 1
    $\begingroup$ Thanks. These off-by-one bugs are painful. In some way they are the hardest bugs to appreciate because they deal with things that seem, on the surface, to be so simple. $\endgroup$ – Chris Nadovich Jun 30 '17 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.