Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why should I use (y,x) instead of (x,y) to access a pixel in opencv?

I had to access a pixel into an image, using openCV but at first I wasn't able since python was telling me that the value I wanted to access was out of bound.

Then I searched for this error and I discovered that I should access a pixel using image[y, x] instead of image[x, y].

I found this piece of information in the comments of this page, but there is no explanation: https://www.pyimagesearch.com/2016/02/01/opencv-center-of-contour/

Adrian Rosebrock February 12, 2016 at 3:19 pm #: When accessing pixel values in OpenCV + NumPy, you actually specify them in (y, x) order rather than (x, y) order. Thus, you need to use: image[cY, cX]

So, the question is...why should I invert the coordinates while trying to access a pixel?

like image 929
gabt Avatar asked Oct 23 '25 18:10

gabt


1 Answers

find the answer in the comments. It is related to how a language store matrices:

Apart from Fortran, most languages store matrices in row-major order, so the indices are row, column, aka y, x. – Paul R Feb 6 at 8:30

like image 174
gabt Avatar answered Oct 26 '25 10:10

gabt