Combining Euclidian distance by measuring colour and edge distance - java

Say I have calculated the euclidean distance between two images using colour as a feature and also calculated the distance between the two images using their edges. I want to test to see if combining these two distance values will give a better representation of how similar the images are. To combine these two distance measures is it as simple as colourDistance + edgeDistance / 2? Or is there a more sophisticated way of combing distance values?

Any function of colourDistance and edgeDistance could work. You could think of what you described as testing three possible functions:
f1(colourDistance, edgeDistance) = colourDistance
f2(colourDistance, edgeDistance) = edgeDistance
f3(colourDistance, edgeDistance) = (colourDistance + edgeDistance) / 2
You could, in theory, test any other function. One thing that comes immediately to mind is linear combinations:
g(colourDistance, edgeDistance) = w1 * colourDistance + w2 * edgeDistance
For various values of w1, w2. This will allow you to experiment with the visual importance of the two features. Your f3 is one case of this function, with w1=w2=0.5
You might found out that the weight of the features isn't linear, for example, a 1-point difference for very small values is much more (or less) significant than a 1-point difference for large values. You could try functions like:
h(colourDistance, edgeDistance) = w1 * log(colourDistance) + w2 * log(edgeDistance)
Final advice, it's not clear to me if the distances you have are on the same scale. If one distance metric goes from 0-10 and the other from 0-1000, you probably need to either normalize the values, or compensate by the choice of w1 and w2.

Related

Calculate Distance between two points- Java

I have inherited a code and it has this small function written to calculate distance between two points.I'm wondering , what it does. I know the lat long data is in decimal degrees. Could anyone please throw some insights, if this calculation is right?
private double calculateDistance(QuoteItem quoteItem, RouteInfo routeInfo) {
final double distance =
((Math.max(routeInfo.getLatitude(), quoteItem.getLatitude()) - Math.min (routeInfo.getLatitude(), quoteItem.getLatitude())) +
(Math.max(routeInfo.getLongitude(), quoteItem.getLongitude()) - Math.min(routeInfo.getLongitude(), quoteItem.getLongitude()))) * 60.0;
return distance;
}
This is a variant of the Manhattan distance calculation where it's not a true Euclidean hypotenuse distance calculation, but rather a simple sum of the two sides of the right triangle multiplied by some multiplier, 60. I usually see it written more simply as
Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y)
Which is essentially what your calculation is, except you're also multiplying it by some scaling factor, 60.0.
I've used this in programs where I want to get a quick and dirty estimate of distance with an emphasis on quick since it involves no square roots. For instance, I used it once for very rough (and incorrect but correct enough for the purposes) calculation of the differences between pixel colors, where I had to make this calculation repeatedly for the pixels contained two images, in real time, and where need for speed trumped the need for accuracy.

Minimize complex linear multivariable function in java

I need to minimize a complex linear multivariable function under some constraints.
Let x be an array of complex numbers of length L.
a[0], a[1], ..., a[L-1] are complex coefficients and
F is the complex function F(x)= x[0]*a[0] + x[1]*a[1] + ... + x[L-1]*a[L-1] that has to be minimized.
b[0], b[1], ..., b[L-1] are complex coefficients and there is a constraint
1 = complexConjuate(x[0])*x[0] + complexConjuate(x[1])*x[1] + ... + complexConjuate(x[L-1])*x[L-1] that has to be fulfilled.
I already had a detailed look at http://math.nist.gov/javanumerics/ and went through many documentations. But I couldn't find a library which does minimization for complex functions.
You want to minimize a differentiable real-valued function f on a smooth hypersurface S. If such a minimum exists - in the situation after the edit it is guaranteed to exist because the hypersurface is compact - it occurs at a critical point of the restriction f|S of f to S.
The critical points of a differentiable function f defined in the ambient space restricted to a manifold M are those points where the gradient of f is orthogonal to the tangent space T(M) to the manifold. For the general case, read up on Lagrange multipliers.
In the case where the manifold is a hypersurface (it has real codimension 1) defined (locally) by an equation g(x) = 0 with a smooth function g, that is particularly easy to detect, the critical points of f|S are the points x on S where grad(f)|x is collinear with grad(g)|x.
Now the problem is actually a real (as in concerns the real numbers) problem and not a complex (as in concerning complex numbers) one.
Stripping off the unnecessary imaginary parts, we have
the hypersurface S, which conveniently is the unit sphere, globally defined by (x|x) = 1 where (a|b) denotes the scalar product a_1*b_1 + ... + a_k*b_k, the gradient of g at x is just 2*x
a real linear function L(x) = (c|x) = c_1*x_1 + ... + c_k*x_k, the gradient of L is c independent of x
So there are two critical points of L on the sphere (unless c = 0 in which case L is constant), the points where the line through the origin and c intersects the sphere, c/|c| and -c/|c|.
Obviously L(c/|c|) = 1/|c|*(c|c) = |c| and L(-c/|c|) = -1/|c|*(c|c) = -|c|, so the minimum occurs at -c/|c| and the value there is -|c|.
Each complex variable x can be considered as two real variables, representing the real and imaginary part, respectively, of x.
My recommendation is that you reformulate your objective function and constraint using the real and imaginary parts of each variable or coefficient as independent components.
According to the comments, you only intend to optimize the real part of the objective function, so you can end up with a single objective function subject to optimization.
The constraint can be split into two, where the "real" constraint should equal 1 and the "imaginary" constraint should equal 0.
After having reformulated the optimization problem this way, you should be able to apply any optimization algorithm that is applicable to the reformulated problem. For example, there is a decent set of optimizers in the Apache Commons Math library, and the SuanShu library also contains some optimization algorithms.

Find nearest point relative to an object without using the distance formula in Java

If I have an object with properties of x an y, how can I tell which point in an array is the closest without using the distance formula?
You can't get an accurate result without using some variant of the distance formula. But you can save a few cycles by not taking the square root after the fact; the comparison will remain valid.
r = dx2 + dy2
If you don't care about the exact distance, you could perhaps take the difference between the x and y coordinates of your source and destination points to provide you with some ordering.
//The following code does not return the closest point,
//but it somewhat does what you need and complies with
//your requirement to not use the distance formula
//it finds the sum of x and y displacements
Point destination=...
Point nearestPoint= points.get(0);
for (Point p : points){
closenessCoefficient= Math.abs(destination.x-p.x) + Math.abs(a.destination-p.y);
nearestPoint=Math.Min(closenessCoefficient, nearestPoint);
}
return nearestPoint;
If you have to find exactly the closest neighbour, there is no way around evaluating the distance formula, at least for a couple of points. As already pointed out, you can avoid evaluating the expensive sqrt for most of the time when simply comparing the distance-squared r^2 = x^2 + y^2.
However, if you have a big number of points spread out over a large range of distances, you can first use an approximation like the ones shown here http://www.flipcode.com/archives/Fast_Approximate_Distance_Functions.shtml . Then you can calculate the real distance formula only for the points closest as given by the approximation. On architectures where also the multiplication is expensive, this can make a big difference. On modern x86/x86-64 architectures this should not matter much though.

Get from java hashmap based on 2d distance

I am making a strategy game in android with lots of units and I have come to a point where I need to check the position of every single object against every other one to see if the two are close enough together that they should start fighting. Right now the only way I can determine if two units are close enough is in this method:
public boolean inProximity(float x2, float y2) {
return Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y)) <= proximityRadius;
}
I don't want to be iterating through all the units an n-squared amount of times. I was looking at a TreeMap to store the positions, but then how would I (if it's even possible) get the keys based of distance from a unit?
You might want to reconsider the data structure here and take a look at quadtrees. They essentially allow you to partition 2D space and do collision detections which seems to be your use case.
http://en.wikipedia.org/wiki/Quadtree
Just doing a google on java quadtree produces some hits on implementations. I haven't used any of them so I can't really vouch for them, but that should give you something to go on.
1) As a first thing to think about, you don't need to take the square root. The square and the square root both increase as the numbers increase, so that would be one optimization.
2) This is known as the 'Closest Pair' problem, and there are divide-and-conquer based algorithms to solve it.
Take a look at: http://en.wikipedia.org/wiki/Closest_pair_of_points_problem

What are the differences between MFCC and BFCC?

I have implemented MFCC algorithm and want to implement BFCC. What are the differences between them and is it enough just to use another function instead of frequency to mel (2595 * Math.log10(1 + frequency / 700) ) and mel to frequency functions (700 * (Math.pow(10, mel / 2595) - 1) ) I follow that code: MFCC
PS: Does it need to change the code for triangular filters?
These are just different scales of representing the frequency spacings of the filters. MFCC uses filters whose center frequencies are spaced along the mel scale, while BFCC will use filters with center frequencies spaced along the bark scale.
The bark scale would simply be represented as:
Bark(f)=13*arctan(0.00076*f)+3.5*arctan((f/(7500))*(f/(7500)))
where f is the frequency in Hz.
Though you can use the bark scale to represent the center frequency spacings, research shows that using either mfcc or bfcc to represent feature vectors of an input speech sample has very little effect on ASR systems performance. The industry standard remains MFCC. In fact, I have not heard much of the BFCC.
If the code for the computation of filter coefficients is relatively generic and it takes in center frequencies as an input parameter, then I would say that you are OK. But, it is always best to double-check. Use MATLAB and plot frequency responses and check! You can check the [following paper][1] out for a comparison between MFCC, BFCC and uniform scale frequency spacings.
Update 1: The center frequency of a filter is either the arithmetic/geometric mean between the upper and lower cutoff frequencies of a band-pass/band-stop filter.
Also, the reverse equation to solve for f given the Bark frequencies is not trivial. It will be a quadratic equation that will need to be solved. One way would be to have a table constructed for different values of f and Bark and then do a table lookup. But I have not been able to find any links to the reverse equation.
[1]: http://148.204.64.201/paginas%20anexas/voz/articulos%20interesantes/front%20end/MFCC/a-comparative-study-of.pdf
You could just instead select the frequencies by hand of each bark critical band (a bounch of if's and else's), since there is no exact equation for bark critical bands (for mel's either, but there is a pretty close one), then get the logarithm of the value for each band, and then apply dct, remember this is for each frame, mel scale uses also logarithmic scale, so there is not much point between doing mfcc or bfcc.

Categories

Resources