C#图像相似度的计算方法

主目录:https://www.v2know.com/MainPage/Category/CSharp

 

前言:

这个方法对单一色图片的识别效果其实并不好,准确地说,就是黑白不分。

这是一个相对简单的识别,准确率不高,但可以满足部分需求,然后就是一部分代码是有问题的。

 

ImageSimilarity.cs:

public class ImageSimilarity
{
    public Bitmap Resize(string imageFile, string newImageFile)
    {

        Image img = Image.FromFile(imageFile);

        Bitmap imgOutput = new Bitmap(img, 256, 256);

        imgOutput.Save(newImageFile, System.Drawing.Imaging.ImageFormat.Jpeg);

        imgOutput.Dispose();

        return (Bitmap)Image.FromFile(newImageFile);
    }
    //灰度直方图计算方法
    public int[] GetHisogram(Bitmap img)
    {

        BitmapData data = img.LockBits(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

        int[] histogram &#
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中可以使用余弦相似度算法来计算文本的相似度。余弦相似度算法是一种常用的文本相似计算方法,它可以通过计算两个文本向量之间的夹角余弦值来判断文本的相似程度。 以下是一个简单的C#代码示例,展示如何使用余弦相似度算法计算两个字符串之间的相似度: ``` using System; using System.Collections.Generic; namespace CosineSimilarity { class Program { static void Main(string[] args) { string text1 = "This is the first text"; string text2 = "This is the second text"; double similarity = CalculateCosineSimilarity(text1, text2); Console.WriteLine("The similarity between the two texts is: " + similarity); } static double CalculateCosineSimilarity(string text1, string text2) { Dictionary<string, int> vector1 = GetTermFrequencies(text1); Dictionary<string, int> vector2 = GetTermFrequencies(text2); double dotProduct = 0.0; double magnitude1 = 0.0; double magnitude2 = 0.0; foreach (KeyValuePair<string, int> entry in vector1) { string term = entry.Key; int frequency1 = entry.Value; int frequency2 = 0; if (vector2.TryGetValue(term, out frequency2)) { dotProduct += frequency1 * frequency2; } magnitude1 += Math.Pow(frequency1, 2); } foreach (KeyValuePair<string, int> entry in vector2) { int frequency2 = entry.Value; magnitude2 += Math.Pow(frequency2, 2); } magnitude1 = Math.Sqrt(magnitude1); magnitude2 = Math.Sqrt(magnitude2); if (magnitude1 == 0.0 || magnitude2 == 0.0) { return 0.0; } else { return dotProduct / (magnitude1 * magnitude2); } } static Dictionary<string, int> GetTermFrequencies(string text) { Dictionary<string, int> frequencies = new Dictionary<string, int>(); string[] words = text.Split(' '); foreach (string word in words) { if (frequencies.ContainsKey(word)) { frequencies[word]++; } else { frequencies[word] = 1; } } return frequencies; } } } ``` 在此示例中,我们首先定义了两个字符串 `text1` 和 `text2`,然后调用了 `CalculateCosineSimilarity` 方法计算这两个字符串之间的相似度。该方法接受两个字符串作为输入,并返回一个 double 类型的相似度值。在方法中,我们首先调用了 `GetTermFrequencies` 方法计算每个字符串中每个词语的出现频率,并将其存储在一个字典中。然后,我们通过计算这两个字符串的向量之间的余弦相似度来计算它们之间的相似度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值