MATLAB Answers

image(x,y,A) fails on log axis depending on axis's limits

22 views (last 30 days)
Hi The question is pretty much self explanatory given the following example
The following code works:
figure
A = rand(200,200,3);
axes('XScale','log','YScale','log')
hold on
image([1e1 1e2],[1e2 1e4],A)
The following code doesn't work:
figure
A = rand(200,200,3);
axes('XScale','log','YScale','log')
hold on
image([1e1 1e2],[1e2 1e5],A)
The only difference between the two codes is the y coordinate of the top corner (1e4 in the first example, 1e5 in the second).
Why does this happen and how can I fix it?

  3 Comments

Guillaume
Guillaume on 9 Apr 2018
Well, that's certainly interesting. Unfortunately, since image is a built-in function we can't look at the cause for the error. You would be better off raising a bug report with mathworks.
You can get the same problem by changing the YData property of your first image object:
figure
A = rand(200,200,3);
axes('XScale','log','YScale','log')
hold on
im = image([1e1 1e2],[1e2 1e4],A) %OK
im.YData = [100 1e5] %kaboom!
Luca Amerio
Luca Amerio on 9 Apr 2018
I investigated it a little more and what seams to matter is the ration y(2)/y(1) (or x(2)/x(1) ).
The command works until the ration is smaller than 398.999999999999971578...

Sign in to comment.

Accepted Answer

Luca Amerio
Luca Amerio on 17 Apr 2018

From the MATLAB Technical Support:

---------------------------------------------------------------------------------------------------

Hello Luca,

I am writing in reference to your Technical Support Case #03035296 regarding 'image(x,y,A) fails on log axis depending on corner position'.

I apologize for taking some time to respond.

After collaborating with my colleagues, we have found that currently it is a bug at our end. Thank you for reporting this issue. I apologize for any inconvenience this may have caused you.

The issue lies in how an image object is rendered. MATLAB adds a small padding on all sides of the image to better support image visualization in cases where axis scale is linear. However, this padding currently is applied as a percentage of image dimension. Hence, causes the image to overflow in the invalid range of the log scale, namely, for negative values.

One possible workaround is to carefully manage the image "XData" and "YData" to make sure that the padding does not end up on the negative side of the log scale. The padding for "XData" is computed using 1/(2*imageDimInX) * (X2- X1). This should be restricted to be greater than "X1". Similarly for YData.

This padding is then added to "XData" and "YData" before being used for rendering.

Please be assured that the development team have been made aware of the issue and will consider it. They might consider fixing it in future releases of MATLAB.

Please feel free to reply to this mail if you have any other related questions. I will be happy to reopen this case and assist you further.

Sincerely, Niraj Gadakari MathWorks Technical Support Department

---------------------------------------------------------------------------------------------------

More Answers (1)

Walter Roberson
Walter Roberson on 9 Apr 2018
XData and YData are locations of pixel centers. You have to use the upper and lower values and the resolution to calculate where the lowest edge of the pixel would be. If it would be 0 or negative then the image cannot be displayed on a log plot.

  7 Comments

Show 4 older comments
Luca Amerio
Luca Amerio on 10 Apr 2018
I correct myself regarding the last point.
The distance between two column is equal to
log(dx) = (log(x(2)) - log((x(1))) / size(A,2);
The left edge of the leftmost column of pixel should be calculated as
log(x_leftcorner) = log(x(1)) - log(dx)/2;
x_leftcorner = 10^(log(x(1)) - log(dx)/2);
that is always positive.
Walter Roberson
Walter Roberson on 10 Apr 2018
What I had observed in the past was that MATLAB continues to use the linear formula for the purpose of prediction of whether to draw the image or not, but that once it comes to draw the image, it just draws it linearly as if log axes were not in effect. I did not check this out in detail, though.
Years ago I posted,
"When the fixed half pixel outside margin is translated to coordinates in log space, the result can be outside of the current XLim. When that happens, the image is largely clipped out. If you set the XLim manually and then adjust the XData to be close to but not quite at the XLim, the image will occupy most of the plot space, disappearing if the margin would project over the XLim."
Luca Amerio
Luca Amerio on 17 Apr 2018
At the end of the day, it turned out to be a bug in the image(...) function. See below for the full explanation of the Technical Support Team

Sign in to comment.

Tags

Products


Translated by