How to calculate mean and standard deviation for hue values from 0 to 360? - colors

Suppose 5 samples of hue are taken using a simple HSV model for color, having values 355, 5, 5, 5, 5, all a hue of red and "next" to each other as far as perception is concerned. But the simple average is 75 which is far away from 0 or 360, close to a yellow-green.
What is a better way to calculate this mean and associated std?

The simple solution is to convert those angles to a set of vectors, from polar coordinates into cartesian coordinates.
Since you are working with colors, think of this as a conversion into the (a*,b*) plane. Then take the mean of those coordinates, and then revert back into polar form again. Done in matlab,
theta = [355,5,5,5,5];
x = cosd(theta); % cosine in terms of degrees
y = sind(theta); % sine with a degree argument
Now, take the mean of x and y, compute the angle, then
convert back from radians to degrees.
meanangle = atan2(mean(y),mean(x))*180/pi
meanangle =
3.0049
Of course, this solution is valid only for the mean angle. As you can see, it yields a consistent result with the mean of the angles directly, where I recognize that 355 degrees really wraps to -5 degrees.
mean([-5 5 5 5 5])
ans =
3
To compute the standard deviation, it is simplest to do it as
std([-5 5 5 5 5])
ans =
4.4721
Yes, that requires me to do the wrap explicitly.

I think the method proposed by user85109 is a good way to compute the mean, but not the standard deviation:
imagine to have three angles: 180, 180, 181
the mean would be correctly computed, as a number aproximately equal to 180
but from [180,180,-179] you would compute a high variance when in fact it is near zero
At first glance, I would compute separately the means and variances for the half positive angles , [0 to 180] and fot the negative ones [0,-180] and later I would compute the combined variance
https://www.emathzone.com/tutorials/basic-statistics/combined-variance.html
taking into account that the global mean and the difference between it and the local means has to be computed in both directions: clockwise and counterclockwise, and the the correct one has to be chosen.

Related

How to normalize samples of an ongoing cumulative sum?

For simplicity let's assume we have a function sin(x) and calculated 1000 samples between -1 and 1 with it. We can plot those samples. Now in the next step we want to plot the integral of sin(x) which would be - cos(x) + C. Now i can calculate the integral with my existing samples like this:
y[n] = x[n] + y[n-1]
Because it's a cumulative sum we will need to normalize it to get samples between -1 and 1 on the y axis.
y = 2 * ( x - min(x) / max(x) - min(x) ) - 1
To normalize we need a maximum and a minimum.
Now we want to calculate the next 1000 samples for sin(x) and calculate the integral again. Because it's a cumulative sum we will have a new maximum which means we will need to normalize all of our 2000 samples.
Now my question basically is:
How can i normalize samples in this context without knowing the maximum and minimum?
How can i prevent, to normalize all previous samples again, if i have a new set of samples with a new maximum/minimum?
I've found a solution :)
I also want to mention: This is about periodic functions like Sine, so basically the maximum and minimum should be always the same, right?
In a special case this isn't true:
If you samples don't contain a full period of the function (with global maximum and minimum of the function). This can happen when you choose a very low frequency.
What can you do:
Simply calculate the samples for a function like sin(x) with a
frequency of 1. It will contain the global maximum and minimum of the function (it's important that y varies between -1 and 1, not 0 and 1!).
Then you calculate the integral with the cumulative sum.
get maximum and minimum of the samples
you can scale it up or down: maximum/frequency, minimum/frequency
can be used now to normalize samples which were calculated with any other frequency.
It only need to be calculated once at the beginning.

In DDA, why are lines sampled in unit intervals of x if gradient <= 1

from Wikipedia,
A linear DDA starts by calculating the smaller of dy or dx for a unit increment of the other. A line is then sampled at unit intervals in one coordinate and corresponding integer values nearest the line path are determined for the other coordinate.
Considering a line with a positive slope, if the slope is less than or equal to 1, we sample at unit x intervals (dx=1) [...]
For lines with a slope greater than 1, we reverse the role of x and y i.e. we sample at dy=1 [...]
Similar calculations are carried out to determine pixel positions along a line with a negative slope
How does the slope (positive or negative) affect the algorithm?
why is the gradient being less or equal to 1 important?
If your gradient is negative (in one of the dimensions) and you walk along that direction with unit increments, you have to adapt your loop to count backwards.
If you walk along the wrong dimension (with unit increments), you will end up with gaps on the line. E.g., if you have slope 2 and you walk along the x-direction, only every second row will contain a pixel.

How to convert longitude/latitude (WGS-84) to X / Y coordinates (Pixmap)

I've got a big number of nodes (lon,lat) in WGS-84 and I need to draw them on a Pixmap, so I have read a lot of Q&A here, but haven't found the algorithm how to convert lon/lat from WGS-84 to a x/y coordinates. By the way, I need to draw a simple scheme of map. Any ideas?
To go from WGS-84 latitude and longitude to an two-dimensional map, you first need to consider what kind of projection you have. This is because one minute of arc, for example, could mean different distance changes over a particular projection. You're mapping an ellipsoid to a plane, so you're going to get some distortion somewhere.
But for a simple case, let's say that your area is small enough, and close enough to the equator, that the change in angle (latitude or longitude) corresponds to a constant change in distance on the map (Y or X).
So, if you have a 600*600 image of a particular area, and it corresponds to a 10-minute by 10-minute area of the earth that has an upper-left corner at 30 degrees north, 40 degrees west.
To locate the pixel where 29 degrees, 55 minutes north, 39 degrees, 57 minutes west, we use a proportion for both latitude and longitude:
5' / 10' = Y / 600 pixels ---> Y = 300 (from the top edge)
3' / 10' = X / 600 pixels ---> X = 180 (from the left edge)
Hope that helps.

Position from the latitude and longitude of nearby places

How can I calculate the latitude-longitude of a place, when the latitude-longitude information of its 3 nearby places, and the distances to the place are given?
E.g, A,B and C are three places with latitude-longitude (x1,y1), (x2,y2) and (x3,y3). Let D be at distance k1, k2 and k3-km from A, B and C respectively. How can one determine the latitude-longitude of D.
In case, all these points, A,B,C and D are on a plane, then this can be calculated, by determining the point of intersection of the three circles, from A, B and C with radius k1, k2 and k3, respectively. However, how to determine the position, when the points are on a sphere and not on a circle?
Starting with a plane, here is an alternative method to your suggested 3 circles:
Since you know the coordinates of A,B and C, you can calculate the length of AB,AC and BC.
Lets mark by X the point point we want to find.
we know that AX=k1, BX=k2 and CX=k3
Now draw the 3 triangles: ABX, ACX, EDX. We know the length of all their edges, therefore we can calculate the angles.
Knowing all coordinates, lengths and angles we can calculate the coordinates of X.
For a sphere- almost the same idea can be used. However:
Coordinates should be in Lat/Lon radians
Distances should be calculated using great circle distance formula. See Aviation Formulary.
Total angles in a triangle is not 180 - It is between 180 and 540.
Knowing the edges lenghts, angles can be calculated using the spherical law of cosines
Now, coordinates of X can be calculated.
It takes some time to get used to working with spherical trigonometry, however this is the right tool for such problems.
I'll assume k1, k2, k3 are distances on the sphere.
You may be able to solve this most precisely by using a special case of the Vincenty formula. With your three sets of values for x, y, and k, you can set up a system of equations to solve for the latitude and longitude of the fourth point. Solving this system would probably be extremely tricky due to its complexity, but there may be computational tools that could perform such a solve.

Rating the straightness of a line

I have a data set that defines a set of points on a 2-dimensional Cartesian plane. Theoretically, those points should form a line, but that line may be perfectly horizontal, perfectly vertical, and anything in between.
I would like to design an algorithm that rates the 'straightness' of that line.
For example, the following data sets would be perfectly straight:
Y = 2/3x + 4
X | Y
---------
-3 | 2
0 | 4
3 | 6
Y = 4
X | Y
---------
1 | 4
2 | 4
3 | 4
X = -1
X | Y
---------
-1 | 7
-1 | 8
-1 | 9
While this one would not:
X | Y
---------
-3 | 2
0 | 5
3 | 6
I think it would work to minimize the sum of the squares of the distances of each point from to a line (usually called a regression line), then determine the average distance of each point to the line. Thus, a perfectly straight line would have an average distance of 0.
Because the data can represent a line that is vertical, as I understand it, the usual least-squares regression line won't work for this data set. A perpendicular least-squares regression line might work, but I've had little luck finding an implementation of one.
I am working in Excel 2010 VBA, but I should be able to translate any reasonable algorithm.
Thanks,
PaulH
The reason things like RSQ and LinEst won't work for this is because I need a universal measurement that includes vertical lines. As a line's slope approaches infinity (vertical), their RSQ approaches 0 even if the line is perfectly straight or nearly so.
-PaulH
Sounds like you are looking for R2, the coefficient of determinism.
Basically, you take the residual sum of squares, divide by the sum of squares and subtract from 1.
Use a Linear Regression. The "straightness" of the line is the R^2 value.
A value of 0 for the R^2 value implies it is perfectly straight. Increasing values imply increasing error in the regression, and thus the line is less and less "straight"
Could you try to catch the case of the vertical line before moving the least squares regression? If all x-values are the same, then the line is perfectly straight, no need to calculate an r^2 value.
Rough idea:
1. translate all coordinates to absolute values
2. calculate tan of current x/y
3. calculate tan of difference in x/y between current x/y and next x/y
4. difference in tan can give running deviation
Yes, use ordinary least squares method. Just use the Slope and Intercept functions in a worksheet. I expect there is a simple way to call these from the VBA codebehind.
Here's the VBA info. for R-Squared: http://www.pcreview.co.uk/forums/thread-1009945.php

Resources