MATLAB pcolor/surf bilinear interpolation (shading interp)

Consider the following MATLAB code:
C = [ 0 0 0 0 0
0 1 2 1 0
0 2 4 2 0
0 1 2 1 0
0 0 0 0 0 ];
pcolor( C );
shading interp;
axis square
Note that `C` is invariant under 90 degree rotations. Also note this sentence from the help for `pcolor`: "With shading interp, each cell is colored by bilinear interpolation of the colors at its four vertices, using all elements of C."
However, the plotted image is as follows:
Note that the image is not invariant under 90 degree rotations (consider e.g. the four corners). Now, unless I horribly misunderstand bilinear interpolation, this must be wrong. MATLAB seems to be interpolating on triangles, which is not the same as bilinear interpolation.
Is there any way of working round this MATLAB bug, and getting correct bilinear interpolation? (Other than manually interpolating additional points myself, which still would not cure the issue if one zoomed in far enough.)

3 Comments

Check out this page, which discusses piecewise linear interpolation (what's going on here) and the bilinear interpolation that you are looking for.
https://blogs.mathworks.com/graphics/2014/11/18/what-is-a-surface/
Joe, can you move this down to the "Answers" section with the rest of the answers? The comments section up here is reserved for requests for clarification of the original question. Sometimes when posters see comments up here, they think they are the answers and never scroll down to see the actual answers in the Answers section.
The solution on the linked page is doing manual interpolation. The question asked for a solution other than manual interpolation...

Sign in to comment.

Answers (1)

pcolor() does NOT use all the elements in your array - it ignores the last row and last column. Just comment out the shading line of code and run it:
Is that what you want and were expecting? I'm guessing not.
So, don't use pcolor. Use imshow(). If you need higher resolution, then use imresize() to make bump up the resolution if you want to:
C2 = imresize(C, 100); % Blow up by a factor of 100;
imshow(C2, []);
axis on;
colorbar;

5 Comments

This is both wrong (pcolor does use all elements in the array when interpolation is enabled) and irrelevant (the question states that it is looking for solutions "other than manually interpolating additional points myself, which still would not cure the issue if one zoomed in far enough"; imresize is precisely such a manual interpolation).
With pcolor, the data is at the corners and crossing points (where the edge lines are) rather than being at the square element itself - a concept that is confusing to many people since it can be non-intuitive since it's nothing like how image(), imshow(), and imagesc() operate. Just wanted to make sure you understood that.
I thought that using a MATLAB function to do the interpolation would not be considered "manual" since you're not actually having to find the percentage of the way over from one line to the other and then getting the new value from that. All that bicubic or bilinear math is done internally inside the function for you. So in my mind that it not manual. But I can see that it is manual by your definition because you need to call a line MATLAB code to do it. I don't know how to do it without calling any additional code. Sorry, but good luck anyway.
The issue is not the "extra line of code". The issue is that interpolating up to a resolution sufficient for high-quality publication produces incredibly large vector images. Given that the manual says that "shading interp" is supposed to do bilinear interpolation, it seemed just plausible that there could at least be an undocumented setting to make it actually do bilinear interpolation.
Really, this is just a documentation bug I expect. It is merely mislabelled.
I want to use this method, with imresize. However I want to keep axis values from 0 to 5 (not 0 to 500 due to the resize). Is there a way to enforce that?
Thank you
marco riva: you can provide XData and YData parameters to imshow() to specify the data units where the image is to be placed. Note that XData and YData are the coordinates of the center of the lower left and upper right pixels so the image edge will be at coordinates one half pixel beyond that.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 18 Nov 2017

Commented:

on 30 Apr 2019

Community Treasure Hunt

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

Start Hunting!