zoukankan      html  css  js  c++  java
  • c#高效比对大量图片

    比较图片的方法

      以前传统的比较方式是遍历图片中的每一个像素,然后进行比对。这样的比对在少量图片的比对上虽然效率低一点,但是也没有什么不好。但是在大量图片比对的时候,过长的反应时间和对服务器比较高的消耗肯定是不行的。

      所以微软给我们提供了另外一种比对的方法,能够正确,高效的比对出两张图片的是否相同。这种方法的原理是:将图片保存到数据流中然后使用Convert.ToBase64String将数据流转换为字符串,那么我们只需要比对两张图片的字符串就ok了。代码如下:

     1   public bool CheckImg(string filePath1, string filePath2)
     2         {
     3         
     4            
     5             MemoryStream ms1 = new MemoryStream();
     6             Image image1 = Image.FromFile(filePath1);
     7             image1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
     8 
     9             string img1 = Convert.ToBase64String(ms1.ToArray());
    10 
    11             Image image2 = Image.FromFile(filePath2);
    12             image2.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
    13             string img2 = Convert.ToBase64String(ms1.ToArray());
    14 
    15             if (img1.Equals(img2))
    16             {
    17                return true;
    18             }
    19             else
    20             {
    21                 return false;
    22             }
    23         }
    比较图片

         这种方法我使用控制台测试了下时间,时间大概为0.015s左右比较一张图片的时间。还是比较高效的。

    大量图片的比较

         比较两张图片能够满足需求,那么大量呢? 我这边也做了这种测试的。在450张图片中,选择出其中重复的图片,并展示出来。大概时间是16s左右,基本能够满足大部分的需求了。

      比较方法是,先讲450张图片,全部一次性转换为string类型,存放到一个数组里面,这样就避免了每次循环都要额外的去转换,使程序的效率降低很多。

       

     1   public static List<Dictionary<string, string>> chekImgss(string filePath)
     2         {
     3             
     4             List<Dictionary<string, string>> liststr = new List<Dictionary<string, string>>();
     5             DirectoryInfo dir = new DirectoryInfo(filePath);
     6             FileInfo[] files = dir.GetFiles();
     7             foreach (FileInfo fileInfo in files)
     8             {
     9                 Dictionary<string, string> dic = new Dictionary<string, string>();
    10                 string ex = fileInfo.Extension;
    11                 if (ex == ".jpg" || ex == ".png")
    12                 {
    13                     MemoryStream ms1 = new MemoryStream();
    14                     Image image2 = Image.FromFile(filePath + fileInfo.Name);
    15                     image2.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
    16                    
    17                     string imgBase64 = Convert.ToBase64String(ms1.ToArray());
    18 
    19                     dic["base64"] = imgBase64;
    20                     dic["imgName"] = fileInfo.Name;
    21                     liststr.Add(dic);
    22                 }
    23             }
    24 
    25             return liststr;
    26         }

            将图片的base64string和图片的名称存储在一个dictionary中,然后存储到list数组中。这样我们在比较的时候,只需要判断两个字符串是否相等就可以了。

       

     1         /// <summary>
     2         /// 对数组进行深拷贝
     3         /// </summary>
     4         /// <param name="files"></param>
     5         /// <returns></returns>
     6         public static List<Dictionary<string, string>> CopyList(List<Dictionary<string, string>> files)
     7         {
     8             MemoryStream ms = new MemoryStream();//序列化
     9             BinaryFormatter bf = new BinaryFormatter();
    10             bf.Serialize(ms, files);
    11             ms.Position = 0;
    12 
    13             List<Dictionary<string, string>> array3 = (List<Dictionary<string, string>>)bf.Deserialize(ms);  //反序列化
    14             return array3;
    15         }
    16 
    17         /// <summary>
    18         /// 比较图片
    19         /// </summary>
    20         /// <param name="listDic"></param>
    21         /// <param name="filePath"></param>
    22         /// <returns></returns>
    23         public static List<Dictionary<object, string>> chekImg2(List<Dictionary<string, string>> listDic,string filePath)
    24         {
    25             List<Dictionary<object, string>> list = new List<Dictionary<object, string>>();
    26 
    27             DirectoryInfo dir = new DirectoryInfo(filePath);
    28             var files = dir.GetFiles().ToList();
    29 
    30             for (int j = 0; j < listDic.Count; j++)
    31             {
    32                 var file = listDic[j];
    33 
    34                 
    35                 var fileList = CopyList(listDic);
    36                 var index = 0;
    37                 var isFirst = false;
    38                 Dictionary<object, string> dic = new Dictionary<object, string>();
    39                 for (int i = 0; i < fileList.Count; i++)
    40                 {
    41                     var fileInfo = fileList[i];
    42 
    43                     if (file["imgName"] == fileInfo["imgName"])
    44                     {
    45                         fileList.Remove(fileInfo);
    46                         i -= 1;
    47                         continue;
    48                     }
    49                     //使用equals比普通的,string==string 高效很多倍
    50                     if (file["base64"].Equals(fileInfo["base64"]))
    51                     {
    52                         if (!isFirst)
    53                         {
    54                             dic[++index] = file["imgName"];
    55                             isFirst = true;
    56                         }
    57                         dic[++index] = fileInfo["imgName"];
    58 
    59                         fileList.Remove(fileInfo);
    60 
    61                         listDic.Remove(listDic.Where(c => c.Values.Contains(fileInfo["imgName"])).FirstOrDefault());
    62                         i -= 1;
    63                     }
    64                     else
    65                     {
    66                         fileList.Remove(fileInfo);
    67                         i -= 1;
    68                     }
    69 
    70                 }
    71 
    72                 if (dic.Keys.Count > 0)
    73                 {
    74                     list.Add(dic);
    75                     listDic.Remove(file);
    76                     j -= 1;
    77                 }
    78                 else
    79                 {
    80                     listDic.Remove(file);
    81                     j -= 1;
    82                 }
    83             }
    84             return list;
    85         }

            这样,我们就可以将完全相同的图片都取出来存放在一个dictionary中,每一组相同的都是一个dictionary,然后存放到list数组中。需要的时候遍历出来就可以了。

    总结

          大量比对的时候,最好先将图片转换为string吧。如果在两个for里面实现这个转换,时间会是现在的很多倍,因为造成了许多重复的转换。然后在比对的过程中,尽量将已经比对过的图片从数组中剔除,这样比对,你会发现越来越快。

  • 相关阅读:
    Using Resource File on DotNet
    C++/CLI VS CSharp
    JIT VS NGen
    [Tip: disable vc intellisense]VS2008 VC Intelisense issue
    UVa 10891 Game of Sum(经典博弈区间DP)
    UVa 10723 Cyborg Genes(LCS变种)
    UVa 607 Scheduling Lectures(简单DP)
    UVa 10401 Injured Queen Problem(简单DP)
    UVa 10313 Pay the Price(类似数字分解DP)
    UVa 10635 Prince and Princess(LCS N*logN)
  • 原文地址:https://www.cnblogs.com/ylchen/p/3363243.html
Copyright © 2011-2022 走看看