Quick math questions

This thread is for math questions of any kind. Don’t be embarrassed. I ask silly questions all the time because I am usually forgetful and nonfunctional.


Let’s start with this one. What would you do to prevent Infs and NaNs in log ratios of channels? E.g. I have an arbitrary image that might have values ≤0. Say if I were to do M=log(G/R). I have to decide the following: Whether and what range to normalize values. Where to place epsilon(s) and what value(s). These considerations will affect the values that end up in M.

Infs and NaNs aside for a moment, you’ll need to guard against R=0… So, can you clip to >0 prior to reaching the expression?

I would say the real question is: what value do you want for M when the value of log(G/R) is indeterminate? Or what action do you want when you can’t calculate M?

Anyhow, log(G/R) is indeterminate when R=0 or (G/R)<=0. So you might have an algorithm like this:

if R==0 then R=epsilon (a small positive number)
V = G/R
if V < 0 then exit_with_error
if V < epsilon then V = epsilon
M = log(V)

The value you choose for epsilon should usually be less than the significance of your images, eg if you have 16-bit images then epsilon < 1/65536.

2 Likes

Thanks for going step by step.

I don’t know what that would be for 32-bit float images with arbitrary values and range. I tend to use PhotoFlow, RT and G’MIC, so @Carmelo_DrRaw, @heckflosse and @David_Tschumperle, what are your thoughts on my question and the epsilon. So far, I have done things arbitrarily but would like to be more considered.

A generic problem here: Let’s say I have a polar curve. Now imagine perpendicular hairs on the curve with values that are based on curve length, and a point in x,y. How can I get the value based on perpendicular hair based on the length of curve? That point in space touches on a invisible perpendicular hair.

This proves to be difficult to solve. The simplest case being archimedean spiral.

In the case of circle. That is doable via atan2(y,x). Each values represent the value of invisible perpendicular hairs.

I don’t really feel like making a illustration. Hence why my atan2(y,x) example. If you were to plot a point at x,y, and draw a line from the x,y point to the center, you get a line that is perpendicular to a target curve. Anything on the line has value equal to all other areas on the line.

Now, imagine an archimedean spiral. Plot a point anywhere there. Now, draw a perpendicular line on the closest curve section in which all points in the curve section is less than the distance from your plotted point to the center. The perpendicular line must touch the point. The value of said line is the length of archimedean curve starting from the start of the curve to the point where the line intersects with the curve.

Hopefully, that’s more clear. The closest example I saw, but has not solved it:

Posterize that, and you will reveal anomaly within the curve gradient. It’s curved and the curves are more obvious in the middle. It does not use the 1-.5*spiral + .5 * radial technique as the lines are not perpendicular with that technique.

I would say that epsilon should be small enough that it isn’t noticeable in images, but not so small that arithmetically it is indistinguishable from zero. For 32-bit floating-point, ImageMagick uses MagickEpsilon = 1e-12. I think it could be much smaller, eg 1e-16.

if R=0 and G>0, then G/R is inf and then of course log(G/R) also is inf, which is not indeterminate.

@afre Does this question even make sense when <= 0?

@Reptorian I don’t quite understand your question, even not with the example in your later post. What do you mean by ‘value’ here? A line has no ‘value’, only a length and a direction (in essence, a vector with a certain magnitude). The direction you want is perpendicular to the curve, but ‘based on curve length and a point in x,y’ is confusing me…

if R=0 and G>0, then G/R is inf and then of course log(G/R) also is inf, which is not indeterminate.

Yes, you are right, of course. I misspoke. I was lazily lumping all “bad numbers” together.

I know a line is a vector with a magnitude. The lines are shaded according to the length of curve based from the start of the reference curve to the intersection.

So, in essence imagine a circle which is the reference curve. Draw a line with one point in the center of a circle to another point arbitrary point in space. Any point besides 0,0 that resides within the line has a shade value of atan(y/x). The line can also be said to have a shade value based on length of a curve section which start at the quadrant of the circle to the intersection of the line and circle going clockwise.

Is that better?

I’m afraid it doesn’t make it fully clear now…

(where I say ‘angle’ of the green section, I could also write ‘length’)

It is closer. But, it is not based on angle, but on length. That point at the end of line has the same shade value on any point of that line except 0,0 and the shade value of line is based on the length from start of reference to intersection. I’d like to find shade value on any point using archimedian spiral. The image example in my second post shows a idea of what it would look like if you could see all shade value points at once.

I encourage people to draw figures because they are easier to grasp than words. Take @Thanatomanic’s unit circle. Suppose a fill gradient starts from the dotted line and follows the green arrow. That is, the gradient increases according to arc length from the starting line.

I think what @Reptorian wants is for the gradient to have the same value along the black line that bisects the curve perpendicularly. In a spiral, that would be a cord from the outer fold of the curve to the inner. All values along each cord must be the same value as the outer arc’s arc length from the start of the curve.

1 Like

In addition, I would like to find value using an arbitrary x,y point in polar space in context of spiral. I searched for a solution to this to no avail.

This isn’t an elegant math solution but couldn’t you simply generate the image and then query the value at that point in the image (or anything else that is based on the image; e.g. FFT transforms)?

Is that for an arbitrary polar function like r = f(\theta) ? And when you say perpendicular, do you mean the normal or just a line to the centre? Surely that’s a bit difficult… need arc length and the two closest radii for the angle from centre. I wonder if there’s a completely different approach - maybe try and explain the goal rather than a solution?

Normal.

Yes. Hence, I ended up resorting to outside help.

If you looked at my gmic thread, you’d note that the goal is to transform a cartesian image into archimedean spiral image. The problems within the archimedean-lena image is obvious.

It needs to meet these conditions to be perfect:

  1. All of the x-axis gradient is perpendicular. Verified by posterize.
  2. Gradients correspond to length.
  3. Automatically rotate and scale to fit (My filter use an approximate approach.).

Comment: Only 3 has been met.

I don’t know how to do those approach. Exhausted every option I can think of.

Ah I didn’t see the other thread, I’ll see if I can understand from that then - thanks!

@Reptorian does it help you that the tangents (and therefore the normals by swapping terms) of the Archimedean spiral can be easily calculated through some differential geometry? https://math.stackexchange.com/questions/1078185/differentiate-archimedess-spiral

(I still have a hard time understanding what you really want, unfortunately… In mathematical terms that is)

Edit: a closed firm expression for the arc length between two points on the spiral also seems to exist: https://encyclopediaofmath.org/wiki/Archimedean_spiral