File bmp文件的比较?
我想比较两个bmp文件。我想到了两种方法:File bmp文件的比较?,file,comparison,diff,bmp,File,Comparison,Diff,Bmp,我想比较两个bmp文件。我想到了两种方法: 比较两个文件的标题和信息标题 将bmp文件转换为二进制文件,然后进行上述比较 但是,我不知道如何开始,哪种方法更好。如果有人能帮助我,我会很高兴的 我不知道您希望在哪个平台上实现此功能,但以下是一些可能有用的代码片段: 这是一个比较两个图像的片段 看看它们是否相同。这 方法首先将每个位图转换为 字节数组,然后获取每个 数组。然后,我们在中循环通过每个 散列以查看它们是否匹配 // ///用于比较两个图像以查看它们是否相同的方法。弗斯特 ///我们将两
但是,我不知道如何开始,哪种方法更好。如果有人能帮助我,我会很高兴的 我不知道您希望在哪个平台上实现此功能,但以下是一些可能有用的代码片段: 这是一个比较两个图像的片段 看看它们是否相同。这 方法首先将每个位图转换为 字节数组,然后获取每个 数组。然后,我们在中循环通过每个 散列以查看它们是否匹配
//
///用于比较两个图像以查看它们是否相同的方法。弗斯特
///我们将两个图像转换成一个字节数组,然后得到它们的散列(它们的
///如果图像相同,则散列应该匹配),然后循环
///哈希中的每个项目与第二个位图进行比较
///
///
///
///
公共bool doImagesMatch(参考位图bmp1,参考位图bmp2)
{
...
}
好吧,这里至少有两个选项:
- 图像基本相同
在这种情况下,我建议使用splattne的解决方案进行比较 - 图像通常不同,只是有时相同
在这种情况下,您可以通过快速比较信息标题(想想“大小是否相同?”)来消除两个图像之间的任何相似性,并且仅在信息不明确(即大小相同)时进行完整比较
public static bool Compare(this Bitmap bmp1, Bitmap bmp2)
{
//put your comparison logic here
}
你在比较什么?你是想看看两幅图像是否完全相同?还是需要不同程度的差异?为了精确匹配,创建并比较两个文件的哈希值如何?当我有两幅只有颜色深度不同的图像时,上面的解决方案不适用于我——一幅是32bpp,另一幅是8bpp在上个月,我使用了锁位将所有图像转换为32bpp,Marshal.Copy()将数据转换为数组,然后比较数组
/// <summary>
/// Compares two images for pixel equality
/// </summary>
/// <param name="fname1">first image file</param>
/// <param name="fname2">second image file</param>
/// <returns>true if images are identical</returns>
public static string PageCompare(string fname1, string fname2) {
try {
using (Bitmap bmp1 = new Bitmap(fname1))
using (Bitmap bmp2 = new Bitmap(fname2)) {
if (bmp1.Height != bmp2.Height || bmp1.Width != bmp2.Width)
return false;
// Convert image to int32 array with each int being one pixel
int cnt = bmp1.Width * bmp1.Height * 4 / 4;
BitmapData bmData1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Int32[] rgbValues1 = new Int32[cnt];
Int32[] rgbValues2 = new Int32[cnt];
// Copy the ARGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(bmData1.Scan0, rgbValues1, 0, cnt);
System.Runtime.InteropServices.Marshal.Copy(bmData2.Scan0, rgbValues2, 0, cnt);
bmp1.UnlockBits(bmData1);
bmp2.UnlockBits(bmData2);
for (int i = 0; i < cnt; ++i) {
if (rgbValues1[i] != rgbValues2[i])
return false;
}
}
}
catch (Exception ex) {
return false;
}
// We made it this far so the images must match
return true;
}
//
///比较两个图像的像素相等性
///
///第一图像文件
///第二图像文件
///如果图像相同,则为true
公共静态字符串页面比较(字符串fname1、字符串fname2){
试一试{
使用(位图bmp1=新位图(fname1))
使用(位图bmp2=新位图(fname2)){
如果(bmp1.Height!=bmp2.Height | | bmp1.Width!=bmp2.Width)
返回false;
//将图像转换为int32数组,每个int为一个像素
int cnt=bmp1.宽度*bmp1.高度*4/4;
BitmapData bmData1=bmp1.LockBits(新矩形(0,0,bmp1.Width,bmp1.Height),
ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
BitmapData bmData2=bmp2.LockBits(新矩形(0,0,bmp2.Width,bmp2.Height),
ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
Int32[]rgbValues1=新的Int32[cnt];
Int32[]rgbValues2=新的Int32[cnt];
//将ARGB值复制到数组中。
System.Runtime.InteropServices.Marshal.Copy(bmData1.Scan0,rgbValues1,0,cnt);
System.Runtime.InteropServices.Marshal.Copy(bmData2.Scan0,rgbValues2,0,cnt);
bmp1.解锁位(bmData1);
bmp2.解锁位(bmData2);
对于(int i=0;i
你在做什么类型的比较?你想检查它们是否是相同的副本?还是想检查它们是否是相似的图像?第一个比较简单,第二个比较复杂。我在ANSI C中有一个代码,在比较之前将两个文件标准化为32bpp。我不会发布它,因为你似乎是已经对C#代码满意了吗
/// <summary>
/// Compares two images for pixel equality
/// </summary>
/// <param name="fname1">first image file</param>
/// <param name="fname2">second image file</param>
/// <returns>true if images are identical</returns>
public static string PageCompare(string fname1, string fname2) {
try {
using (Bitmap bmp1 = new Bitmap(fname1))
using (Bitmap bmp2 = new Bitmap(fname2)) {
if (bmp1.Height != bmp2.Height || bmp1.Width != bmp2.Width)
return false;
// Convert image to int32 array with each int being one pixel
int cnt = bmp1.Width * bmp1.Height * 4 / 4;
BitmapData bmData1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Int32[] rgbValues1 = new Int32[cnt];
Int32[] rgbValues2 = new Int32[cnt];
// Copy the ARGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(bmData1.Scan0, rgbValues1, 0, cnt);
System.Runtime.InteropServices.Marshal.Copy(bmData2.Scan0, rgbValues2, 0, cnt);
bmp1.UnlockBits(bmData1);
bmp2.UnlockBits(bmData2);
for (int i = 0; i < cnt; ++i) {
if (rgbValues1[i] != rgbValues2[i])
return false;
}
}
}
catch (Exception ex) {
return false;
}
// We made it this far so the images must match
return true;
}