accord-net / framework

Machine learning, computer vision, statistics and general scientific computing for .NET
http://accord-framework.net
GNU Lesser General Public License v2.1
4.49k stars 1.99k forks source link

Correlation distance normalization #99

Open fdncred opened 9 years ago

fdncred commented 9 years ago

Please excuse my ignorance if this is incorrect but I'm having a hard time using the CorrelationMatching class. It seems to me like this code below should normalize the distance[] array between 0 and 1 so that one can pass in a maxDistance for general checking. Right now I can pass in 5 for maxDistance and it works for one image but not another. If the values were normalized I'm thinking it would work more reliably. Feel free to me if I'm missing the point here.

Starting at line 371 of CorrelationMatching.cs

// We should consider points that are within
//  distance maxDistance apart

// Compute distances from the current point
//  to all points in the second image.
double[] distances = new double[idx2.Length];
for (int i = 0; i < idx2.Length; i++)
{
    double dx = p1.X - points2[idx2[i]].X;
    double dy = p1.Y - points2[idx2[i]].Y;
    distances[i] = dx * dx + dy * dy;
}

candidates = idx2.Submatrix(Matrix.Find(distances, d => d < m))

Thanks, Darren

cesarsouza commented 8 years ago

Hi there,

The correlation matching has been based almost exclusively on the MATLAB/Octave implementation of Peter Kovesi (http://www.peterkovesi.com/matlabfns/Match/matchbycorrelation.m).

As far as I understand, the maximum distance parameter is more like a shortcut to save computations when the images are not very far apart. If the images are significantly different, then maybe it would be better to leave the parameter at 0 (meaning the distances between all points will be considered, and not only the distances between the nearest ones).

If I understood your proposition correctly, what could be done is the addition of a new property, like a TopPercentMatches property, that could limit the number of distances to be computed only to X% of points that are the closest. Do you think this would help? Have you experimented with the normalization before points pruning as you have initially suggested?

Thank you a lot for the potentially useful suggestion; I hope its not too late for addressing this issue as I recognize I took a bit of time to reply.

Regards, Cesar

cesarsouza commented 8 years ago

Also, do you think you could experiment with the change described in issue #74 ? It has been suggested a long time ago, but I never managed to get a representative dataset and the time to experiment with it. That issue had been registered on a different tracker years ago and I am unfortunately not sure which reference the original reporter was referring to.

fdncred commented 8 years ago

Last year I updated your code to include this snippet just below the original code I mentioned.

//Normalize the distances array - Darren 5/20/15
double maxDist = distances.Max();
for (int i = 0; i < distances.Length; i++)
    distances[i] = distances[i] / maxDist;

By normalizing the results I was able to make your code work with all images I compared on an even scale instead of each image returning a different value which was nearly useless for what I was trying to do. No disrespect intended.

Even with this change I eventually moved to OpenCVSharp using AKAZE/KAZE to compare images and using most of OpenCVs tools to get the job done.

Unfortunately I'm too far removed from the code to work on issue #74. Perhaps if there wasn't a year lag between my question and your answer I would've been better equipped to help.

Thanks so much for answering even though it was a long time. You have a lot of great code here and I watch it closely and will probably use it in the future.

cesarsouza commented 8 years ago

Huge thanks for providing the snippet, I will definitely add it as an option in the matcher. And again, I deeply apologize for the ridiculous delay.