コード例 #1
0
        public void CalculateDistance(BasicImage TrainImage, string metric)
        {
            double distance = 0;

            if (metric == "Euclidean")
            {// metryka euklidesa
                for (int i = 0; i < Traits.Count; i++)
                {
                    distance += Math.Pow(Traits[i] - TrainImage.Traits[i], 2);
                }
                distance = Math.Sqrt(distance);
            }

            if (metric == "Manhattan")
            {// metryka uliczna
                for (int i = 0; i < Traits.Count; i++)
                {
                    distance += Math.Abs(Traits[i] - TrainImage.Traits[i]);
                }
            }

            if (metric == "Chebyshev")
            {// metryka Czebyszewa
                double maxDistance = 0;
                for (int i = 0; i < Traits.Count; i++)
                {
                    distance = Math.Abs(Traits[i] - TrainImage.Traits[i]);
                    if (maxDistance < distance)
                    {
                        maxDistance = distance;
                    }
                }
                distance = maxDistance;
            }

            if (Neighbours.Count < ImageRecognition.k)
            {
                Neighbours.Add(new Neighbour(distance, TrainImage));
            }
            else
            {
                Neighbours = Neighbours.OrderBy(x => x.Distance).ToList();
                if (distance < Neighbours[ImageRecognition.k - 1].Distance)
                {
                    Neighbours[ImageRecognition.k - 1] = new Neighbour(distance, TrainImage);
                }
            }
        }
コード例 #2
0
 public Neighbour(double distance, BasicImage image)
 {
     Distance = distance; Image = image;
 }