Difference between imshow and mesh/surf
28 views (last 30 days)
Show older comments
Jiayun Liu
on 18 Nov 2022
Commented: Jiayun Liu
on 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1197718/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1197723/image.png)
![Transposed matrix](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1197728/Transposed%20matrix.png)
Is there any way to plot with mesh function that gives the correct row and column? Why transposing doesn't work?
0 Comments
Accepted Answer
Walter Roberson
on 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 Comments
Walter Roberson
on 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.)
More Answers (1)
Image Analyst
on 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 Comments
Image Analyst
on 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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!