【发布时间】:2018-06-07 13:20:57
【问题描述】:
我正在开发一个应用程序,我可以在一个列表中加载多张图片,并将该列表中的每张图片相互比较,以便找到重复的图片。
所以首先我成功获取了图片并将它们加载到IList<Bitmap>:
public IList<Bitmap> getPictures()
{
IList<Bitmap> pictures = new List<Bitmap>();
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo d = new DirectoryInfo(filepath+ "\\Phone Pictures\\");
foreach (var picture in d.GetFiles("*.png"))
{
pictures.Add(ConvertToBitmap(picture.FullName));
}
return pictures;
}
比我使用预制的图像比较算法:
public static CompareResult Compare(Bitmap bmp1, Bitmap bmp2)
{
CompareResult cr = CompareResult.ciCompareOk;
//Test to see if we have the same size of image
if (bmp1.Size != bmp2.Size)
{
cr = CompareResult.ciSizeMismatch;
}
else
{
//Convert each image to a byte array
System.Drawing.ImageConverter ic =
new System.Drawing.ImageConverter();
byte[] btImage1 = new byte[1];
btImage1 = (byte[])ic.ConvertTo(bmp1, btImage1.GetType());
byte[] btImage2 = new byte[1];
btImage2 = (byte[])ic.ConvertTo(bmp2, btImage2.GetType());
//Compute a hash for each image
SHA256Managed shaM = new SHA256Managed();
byte[] hash1 = shaM.ComputeHash(btImage1);
byte[] hash2 = shaM.ComputeHash(btImage2);
//Compare the hash values
for (int i = 0; i < hash1.Length && i < hash2.Length
&& cr == CompareResult.ciCompareOk; i++)
{
if (hash1[i] != hash2[i])
cr = CompareResult.ciPixelMismatch;
}
}
return cr;
}
现在这就是我尝试调用算法并将其应用于我加载的列表的方式:
public void ComparePictureList()
{
IList<Bitmap> picturesList = getPictures();
foreach (var picture1 in picturesList)
{
foreach( var picture2 in picturesList)
{
Compare(picture1, picture2);
}
}
}
但是有没有更好的方法将我的算法应用到我的列表中,我的意思是,不是声明 2 个循环 picture1 和 picture2 在 .NET 框架中是否有任何功能可以更好?
P.S:对于任何想知道ConvertToBitmap 是什么的人,就是这样:
public Bitmap ConvertToBitmap(string fileName)
{
Bitmap bitmap;
using (Stream bmpStream = System.IO.File.Open(fileName, System.IO.FileMode.Open))
{
Image image = Image.FromStream(bmpStream);
bitmap = new Bitmap(image);
}
return bitmap;
}
【问题讨论】:
-
而不是
List<Bitmap>,我会将它保存为定义为{Image, Hash}的自定义类,这样哈希就只需要计算一次。如果处理图像,该类也可以负责。根据应用的功能,路径名等其他信息也可能很方便。 -
您不应该在列表中导航两次,因为
list[a] = list[b]应该等于list[b] = list[a] -
@RubensFarias 是的,这是我试图避免做的第一件事
标签: c# algorithm performance