Difference between imshow and mesh/surf

34 vues (au cours des 30 derniers jours)
Jiayun Liu
Jiayun Liu le 18 Nov 2022
Commenté : Jiayun Liu le 21 Nov 2022
I am trying to understand why the result from imshow and mesh looks different. It seems that surf flips the row and column of the matrix I am trying to plot. However, plotting the transpose of the matrix doesn't seem to give me the correct result as well. Since my matrix is a image file, I can see that imshow is correct while mesh flips one of the axis.
Transposed matrix
Is there any way to plot with mesh function that gives the correct row and column? Why transposing doesn't work?

Réponse acceptée

Walter Roberson
Walter Roberson le 18 Nov 2022
imshow() sets the axes YDir property to 'reverse' (unless "hold on" is in effect)
Note that array row indices correspond to Y coordinates, and array column indices correspond to X coordinates. It is common for people to pass row indices as x and column indices as Y, perhaps
surf(1:rows, 1:cols, Z)
but that is wrong and should be
surf(1:cols, 1:rows, Z)
getting that indexing wrong has the effect of transposing the coordinate system.
  2 commentaires
Jiayun Liu
Jiayun Liu le 18 Nov 2022
I guess by wrong you are saying it is the wrong way to interpret the matrix that is read by mesh/surf function. If I have an input image that looks like this,
isn't it intuitive that the (1,1) point correspond to the top left corner of the image while (end,end) the bottom right? If I use imshow, I get the exact same image but mesh swaps the coordinate.
This isn't a transpose of the matrix. This corresponds to flipping the vertical axis although if you read off the coordinate it is a transpose A(i,j) [mesh coordinate] = B(j,i) [matrix coordinate]
Does this mean that if I plot 1 matrix with imshow and the other with mesh/surf, the only way to make them look the same is to use 'axis ij' ?
Walter Roberson
Walter Roberson le 18 Nov 2022
There are two competing coordinate systems in common use. One numbers rows from the top of the screen; the other numbers rows from the bottom of the screen.
It is common that images are constructed with the first row of the data array intended to be at the top -- numbering from the top of the screen. This is also consistent with tables of values, which (at least in the "Western World") numbers data rows from the top. When you imshow() a data array, imshow() understands this context and arranges so that the low-numbered rows are displayed towards the top.
But... cartesian coordinates are traditionally numbered from the bottom, with y=0 at the bottom and increasing y coordinate values at the top.
mesh() and most of MATLAB uses cartesian coordinates, so data items at (1,1) are going to be below data items at (1,20) for example.
Neither way is "wrong". The two different methods of numbering have been in competition for centuries, and it is unlikely that humans will choose one over the other any time soon.
If you want mesh() to display the same way that imagesc() does, then set the axes YDir property to 'reverse'
(Oddly enough, when Descart created Cartesian Coordinates, the axes were turned sideways from what we use now, with x coordinates vertically and increasing y coordinates towards the right.)

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 18 Nov 2022
Surf is a surface rendering of the image where is maps the image intensity into a height above a plane. So surf is like a topographic 2.5-D rendering of a 3-D surface onto the flat 2-D plane of your monitor's screen.
imshow is just like looking at a flat photograph of your scene, not a 2.5-D rendering of it.
Plus surf and imagesc apply some weird, usually unwanted, colormap to pseudocolor the image.
  3 commentaires
Image Analyst
Image Analyst le 18 Nov 2022
That's just the convention. With things plotted like x-y, the origin is at the lower left. This is the way it's been done for centuries. With images, it's an array/matrix. With matrixes the origin (row 1, column 1) is at the upper left. Again it's been done that way for centuries. And surf uses the x-y convention and images use the "row/line 1 at top" convention. You can switch between them after you display the data with axis ij or xy
axis ij; % Origin at upper left
axis xy; % Origin at lower left
Jiayun Liu
Jiayun Liu le 21 Nov 2022
Hmm ok thanks

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by