banner



How To Input A Background Image For Matlab Pcolor Plot

Plot over an image background in MATLAB

T his is a brusque tutorial that documents how to make a MATLAB plot on tiptop of an image background. This can exist useful for a diversity of things simply when I first learned almost it, I was a chip confused by how the axes seem to exist flipped sometimes when you do this. In this tutorial I will requite several examples that should illustrate how to plot on top of an epitome properly. I have used this technique for a few things, such every bit visualizing 1D and 2D classifier decision boundaries when I was preparing my thesis. I have also written a tutorial on how to practice a 3D version of this in which an epitome is shown every bit a plane in a surface plot.

MATLAB plot Plotting on an image

Getting started

First, make a new .m file and load any image you desire. In this example, I used a greyness 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 paradigm unless you think it is possible for a penguin to take on three guys in paw-to-hand combat while continuing upside downward. You should endeavor it with your ain image. Put the following into the .m file:

            % supplant with an image of your choice            img =            imread            (            'myimage.png'            );            % set the range of the axes            % The prototype volition exist stretched to this.            min_x =            0; max_x =            eight; min_y =            0; max_y =            6;            % make data to plot - just a line.            10 = min_x:max_x; y =            (            6/8            )*x;

The above code defines the axes range that the image is stretched to and creates some (x, y) data to plot. The code examples that follow use the to a higher place data and variables.

The post-obit code creates the plot itself. (0,0) starts at the bottom left every bit expected for a regular plot.

            plot            (x,y,'b-*','linewidth',1.5            );

Desired plot to be shown over the image

Before getting started with some examples, first show the image in a MATLAB plot to take a wait at how the prototype array (matrix) is really stored in MATLAB:

            figure; imagesc(img);            xlabel            (            'Raster Cavalcade'            );            ylabel            (            'Raster Row'            );            colormap            (            greyness            );

The above code produces this:

Raster coordinates of the image

The coordinates given past (Raster Row, Raster Column) are the 0-based image coordinates. The image array is stored with (0, 0) as the top left corner of the image. In MATLAB's internal memory, (0, 0) corresponds to subscript (1, one).

Some wrong examples

It is necessary to understand a scrap about what MATLAB is doing when you tell it to prove an prototype and add a plot on summit of it. Sometimes the event is wrong and here are some examples with an caption of why.

Example 1

The offset case that shows an image likewise as a MATLAB plot.

imagesc(            [min_x max_x],            [min_y max_y], img);            hold            on;            plot            (ten,y,'b-*','linewidth',one.5            );

Hither, the imagesc command is issued in imagesc(x,y,C) form, which volition specify the range of axes labels that the image is displayed within (determined here by [min_x max_x] and [min_y max_y]). The agree on command is issued and and then the (x, y) information are plotted. This is the result:

Example 1 - Y-axis reversed

You will notice that the plot and the Y-axis are now upside down but the image is displayed properly. The reason is when imagesc is used, MATLAB has reversed the Y-axis so that Y gets larger equally you move down. This corresponds to making the Y-axis increase in the same direction as image raster coordiantes. When the plot is made, it is using the new, reversed Y-centrality, thus explaining why information technology is flipped.

Example 2

We can tell MATLAB to flip the axis dorsum to normal, equally in the following code with set(gca,'ydir','normal'):

imagesc(            [min_x max_x],            [min_y max_y], img);            hold            on;            plot            (ten,y,'b-*','linewidth',1.v            );            % set the y-axis dorsum to normal.            set            (            gca,'ydir','normal'            );          

This is the result:

Example 2 - Image reversed

Now the image is flipped. That's because when the Y-centrality is set up back to normal, it affects how MATLAB displays the effigy. Y is pocket-sized at the bottom and big at the top, so MATLAB volition show the prototype upside downward. Information technology turns out the Y-centrality direction specifies how MATLAB displays the image; the epitome raster row axes is always displayed in the same direction every bit the Y-axis. In other words, the paradigm is displayed in such a way that the smallest raster row (which is 0) is at the smallest Y value visible on the axes, and the largest raster row is at the largest Y value visible on the axes.

Example 3

The agree on command should be issued later on the image is displayed. Otherwise, the figure will retain the default fix of axes when yous display the image, without adjusting to the width and meridian of the paradigm.

For example, try moving concord on to earlier the imagesc command in Case ii:

            hold            on;            % concur on before the image causes incorrect results            imagesc(            [min_x max_x],            [min_y max_y], img);            plot            (x,y,'b-*','linewidth',1.five            );            % prepare the y-axis dorsum to normal.            ready            (            gca,'ydir','normal'            );          

The result will look like this:

Hold on should be issued after the image is displayed

The correct mode

In social club to resolve the problem from Instance two, the image can be flipped before displaying with the flipud command:

            % Flip the epitome upside downward earlier showing it            imagesc(            [min_x max_x],            [min_y max_y],            flipud            (img)            );            % Note: if your image is RGB, you should use flipdim(img, i) instead of flipud.            hold            on;            plot            (10,y,'b-*','linewidth',1.five            );            % gear up the y-centrality back to normal.            set            (            gca,'ydir','normal'            );

Here, the image is flipped showtime before displaying, so that the bottom of the image is now Y = 0. Then, the y-axis is set back to normal (where Y = 0 is at the bottom). This means that the bottom of the image (which is now Y = 0 due to flipud) is at the lesser of the plot. The result:

Correctly plotting on top of an image

This correctly shows the prototype and the plot. A few readers have pointed out that if your prototype is RGB, you tin can employ the flipdim command instead of flipud:

            flipdim            (img,i            );

One caveat: Although Example 1 and Example two from the previous section are chosen "wrong", they may really give you the right result in some cases. The image in this tutorial depicts a scene that independently has a "right side up". Image Row 0 must e'er exist displayed at the top. There may be cases where the paradigm you want to show is created inside MATLAB and Image Row 0 might simply demand to exist associated with the smallest Y-coordinate shown on the axes. In that example, Case 1 or Example two might give you want you lot want. An example might be when you need to visualize classifier decision boundaries. In any case, the information in this tutorial should be enough to figure it out if needed.

How To Input A Background Image For Matlab Pcolor Plot,

Source: http://www.peteryu.ca/tutorials/matlab/plot_over_image_background

Posted by: palmerdrationotled.blogspot.com

0 Response to "How To Input A Background Image For Matlab Pcolor Plot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel