0
votes

I would like to ask that what's the origin pixel in the image coordinate system used in poly2mask function in MATLAB.

It's been asked by others in OpenCV and wonder if it's the same in MATLAB. Let me repeat the person's question:

In general there may be two kinds of such systems, for the first (0,0) is defined as the center of the upper left pixel, which means the upper left corner of the upper left pixel is (-0,5, -0.5) and the center of the image is ((cols - 1) / 2, (rows - 1) / 2).

The other one is that (0,0) is the upper left corner of the upper left pixel, so the center of the upper left pixel is (0.5, 0.5) and the center of the image is (cols / 2, rows / 2).

My question is which system is adopted in poly2mask function in MATLAB?

1
MATLAB uses 1-based indexing. The center of the top left pixel is typically (1,1). Doesn't the documentation specify this?Cris Luengo
I am not talking about indexing. I wonder where the (0,0) point for polygon coordinates as an input for poly2mask function in MATLAB. In opencv, it is upper left corner so the coordinate of the center of the first (upper left) pixel becomes (0.5,0.5)DJ Rhee
All coordinates in all of MATLAB are based on how you index into a matrix. You can have proof in my answer.Cris Luengo

1 Answers

0
votes

MATLAB uses 1-based indexing, not 0-based indexing. Consequently, the top-left pixel has the center at (1,1). poly2mask follows the same convention, they're pretty consistent at the MathWorks.

This experiment shows this:

>> poly2mask([0.8,1.2,1.2,0.8],[0.8,0.8,1.2,1.2],3,5)
ans =
  3×5 logical array
   1   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0

I drew a polygon closely around the (1,1) point, and the mask shows the top-left pixel marked. If you draw a polygon around the (0.5,0.5) point, no pixel is marked:

>> poly2mask([0.8,1.2,1.2,0.8]-0.5,[0.8,0.8,1.2,1.2]-0.5,3,5)
ans =
  3×5 logical array
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0

The one thing that is easy to make mistakes with is the order of the axes. The first index is y the second is x. But many functions in the Image Processing Toolbox (and elsewhere) take coordinates as (x,y), rather than as (y,x). For example:

>> poly2mask([0.8,1.2,1.2,0.8]+1,[0.8,0.8,1.2,1.2],3,5)
ans =
  3×5 logical array
   0   1   0   0   0
   0   0   0   0   0
   0   0   0   0   0

Here, x = 2 and y = 1, and the pixel ans(1,2) is set!