When the plot is made, it is using the new, reversed Y-axis, thus explaining why it is flipped. This corresponds to making the Y-axis increase in the same direction as image raster coordiantes. The reason is when imagesc is used, MATLAB has reversed the Y-axis so that Y gets larger as you move down. You will notice that the plot and the Y-axis are now upside down but the image is displayed properly. The hold on command is issued and then the (x, y) data are plotted. Here, the imagesc command is issued in imagesc(x,y,C) form, which will specify the range of axes labels that the image is displayed within (determined here by and ). The first example that shows an image as well as a MATLAB plot. Sometimes the result is wrong and here are some examples with an explanation of why. It is necessary to understand a bit about what MATLAB is doing when you tell it to show an image and add a plot on top of it. In MATLAB's internal memory, (0, 0) corresponds to subscript (1, 1). The image array is stored with (0, 0) as the top left corner of the image. The coordinates given by (Raster Row, Raster Column) are the 0-based image coordinates. (0,0) starts at the bottom left as expected for a regular plot.īefore getting started with some examples, first show the image in a MATLAB plot to take a look at how the image array (matrix) is actually stored in MATLAB:
The following code creates the plot itself. The code examples that follow use the above data and variables. The above code defines the axes range that the image is stretched to and creates some (x, y) data to plot. % set the range of the axes % The image will be stretched to this. In this example, I used a gray scale version of one of the images in my artwork gallery, specifically Gentoo Kung Fu, because it is easy to see which way is up in the image unless you think it is possible for a penguin to take on three guys in hand-to-hand combat while standing upside down.