开发者

c# similarity help

开发者 https://www.devze.com 2023-02-27 02:54 出处:网络
Anyone can explain these codes for me? public class SimilarityImages : IComparer<SimilarityImages>, IComparable

Anyone can explain these codes for me?

public class SimilarityImages : IComparer<SimilarityImages>, IComparable
{
    private readonly ComparableImage source;
    private readonly ComparableImage destination;
    private readonly double similarity;

    public SimilarityImages(ComparableImage source, ComparableImage destination, double similarity)
    {
        this.source = source;
        this.destination = destination;
        this.similarity = similarity;
    }

    public ComparableImage Source
    {
        get
        {
            return source;
        }
    }

    public ComparableImage Destination
    {
        get
        {
            return destination;
        }
    }

    public double Similarity
    {
        get
        {
            return Math.Round(similarity * 100, 1);
        }
    }

    public static int operator !=(SimilarityImages value, SimilarityImages compare)
    {
        return value.CompareTo(compare);
    }

    public static int operator <(SimilarityImages value, SimilarityImages compare)
    {
        return value.CompareTo(compare);
    }

    public static int operator ==(SimilarityImages value, SimilarityImages compare)
    {
        return value.CompareTo(compare);
    }

    public static int operator >(SimilarityImages value, SimilarityImages compare)
    {
        return value.CompareTo(compare);
    }

    public override string ToString()
    {
        return string.Format("{0}, {1} --> {2}", source.File.Name, destination.File.Name, similarity);
    }

    #region IComparer&l开发者_开发问答t;SimilarityImages> Members

    public int Compare(SimilarityImages x, SimilarityImages y)
    {
        return x.similarity.CompareTo(y.similarity);
    }

    #endregion

    #region IComparable Members

    public int CompareTo(object obj)
    {
        SimilarityImages other = (SimilarityImages)obj;
        return this.Compare(this, other);
    }

    #endregion

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
        {
            return false;
        }

        var other = (SimilarityImages)obj;

        var equals = Source.File.FullName.Equals(other.Source.File.FullName, StringComparison.InvariantCultureIgnoreCase);

        if (!equals)
        {
            return false;
        }

        equals = Destination.File.FullName.Equals(other.Destination.File.FullName, StringComparison.InvariantCultureIgnoreCase);

        if (!equals)
        {
            return false;
        }

        return true;
    }

    public override int GetHashCode()
    {
        return string.Format("{0};{1}", Source.File.FullName, Destination.File.FullName).GetHashCode();
    }
}


It looks kind of like String.Compare()
(Comparison of strings by value.)

however the return of integers for the operators <, >, <=, >=, ==, != is a bad practice - user expect a boolean return value.

Also, if the "images" are what I think they are (images as in a matrix of pixels), then returning a value for comparison is not intuitive - What comparison algorithm are you using? - Why are you providing only one comparison algorithm and hiding its name? - What if the images are not of same size or pixel format?

0

精彩评论

暂无评论...
验证码 换一张
取 消