本帖最后由 陽陽陽 于 2023-11-22 09:26 编辑
效果图:
两张大图:
隔行扫描找不同后的图:
里面带着不同的开始坐标。
延迟大的图片在100ms左右,越小越快
目前还没找到改进的方法,欢迎大佬改进。。。
[C#] 纯文本查看 复制代码 namespace Diff
{
public class InterlacedScanningDiff
{
private int separation = 10;
public static bool HasDiff(ref Bitmap b1, ref Bitmap b2, Rectangle area, int separation_ = 10)
{
int w = area.Width;
int h = area.Height;
for (int i = 1; i < (int)(h / separation_); i++)
{
for (int n = 0; n < w; n++)
{
if (b1.GetPixel(n + area.X, i * separation_ + area.Y) != b2.GetPixel(n + area.X, i * separation_ + area.Y))
{
return true;
}
}
}
return false;
}
public void GetDiff(Bitmap b1, Bitmap b2, out List<InterlacedScanningImageInfo> infor, out List<Bitmap> imgs)
{
List<InterlacedScanningImageInfo> ls = new List<InterlacedScanningImageInfo>();
int w = (b1.Width < b2.Width) ? (b1.Width) : (b2.Width); //最小的
int h = (b1.Height < b2.Height) ? (b1.Height) : (b2.Height);
for (int i = 1; i < (int)(h / separation); i++)
{
for (int n = 0; n < w; n++)
{
if (b1.GetPixel(n, i * separation) != b2.GetPixel(n, i * separation))
{
//Rectangle rectangle = new Rectangle(0, (i - 1) * separation, w, separation * 2);
//, image = Drawing.PartImage.ExtractRegionFromImage(b2, rectangle)
ls.Add(new InterlacedScanningImageInfo() { StartPos = (i - 1) * separation, Height = separation * 2 }); // i -1, i , i + 1
i += 1; // i+1+1
Console.WriteLine(i);
break;
/*
-----------------------
----------------------=i-1
-----------------------找到的线 - i height = 2 * space
----------------------=i+1---------------------------------------不同i-1
-----------------------i+=2 - 再从这开始 -假如有不同 不同i height = 2 * space
----------------------- 不同i+1
-----------------------
-----------------------
*/
}
}
}
foreach (var a in ls)
{
Console.WriteLine("stat pos: " + a.StartPos + " end pos: " + (a.Height + a.StartPos));
}
for (int i = 0; i < ls.Count; i++)
{
for (int n = 0; n < ls.Count; n++)
{
if (ls[n].StartPos + ls[n].Height == ls.StartPos) //i在底下
{
//var temp = ls[n];
//temp.height = ls[n].height;
//ls[n] = temp;
//ls.RemoveAt(i);
//i--;
var temp = ls;
temp.StartPos = ls[n].StartPos;
temp.Height = ls.Height + ls[n].Height;
ls = temp;
ls.RemoveAt(n);
i--;
}
}
}
foreach (var a in ls)
{
Console.WriteLine(a.StartPos);
}
List<Bitmap> bitmaps = new List<Bitmap>();
foreach (var a in ls)
{
Rectangle rectangle = new Rectangle(0, a.StartPos, w, a.Height);
bitmaps.Add(Drawing.PartImage.ExtractRegionFromImage(b2, rectangle));
}
infor = ls;
imgs = bitmaps;
}
}
public struct InterlacedScanningImageInfo
{
public int StartPos;
public int Height;
//public Bitmap image;
}
}
编辑补充:
1:既然大家都参与了开源大赛,我这前几天帖子发急了,现在编辑一下
2:修复了几个小问题
3:添加了隔行扫描判断图片是否变化,非常快,比直接对比点阵信息有效率,而且还能一定程度地去除微小变化(如果需要精确判断是否变化,可能不适用)
附赠一个精确找不同源码(如果短时间内频繁调用可会失效):
[C#] 纯文本查看 复制代码 //var bmp1 = b1.Clone(a, PixelFormat.Format8bppIndexed);
//Monitor.Enter(imageMonitor);
//var data = b1.LockBits(a, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
//IntPtr ptr = data.Scan0;
//int NumBytes = data.Stride * data.Height;
//byte[] rgbValues1 = new byte[NumBytes];
//Marshal.Copy(ptr, rgbValues1, 0, NumBytes);
//b1.UnlockBits(data);
//var data2 = b2.LockBits(a, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
//IntPtr ptr2 = data2.Scan0;
//int NumBytes2 = data2.Stride * data2.Height;
//byte[] rgbValues2 = new byte[NumBytes2];
//Marshal.Copy(ptr2, rgbValues2, 0, NumBytes2);
//b2.UnlockBits(data2);
////for(int i = 0; i < 20; i++) Console.WriteLine("1 ---- " + rgbValues1);
////for(int i = 0; i < 20; i++) Console.WriteLine("2 ---- " + rgbValues2);
//Monitor.Exit(imageMonitor);
//if (!md5.ComputeHash(rgbValues1).SequenceEqualEX(md5.ComputeHash(rgbValues2)))
//{
// //for (int i = 0; i < rgbValues1.Length; i++)
// //{
// // if (rgbValues1 != rgbValues2) Console.WriteLine("diff at pos" + i + ": " + rgbValues1 + " and " + rgbValues2);
// //}
// Monitor.Enter(listMonitor);
// // Console.WriteLine(diffList.FirstOrDefault(x => x.X == a.X && x.Y == a.Y && x.Width == a.Width && x.Height == a.Height) == default);
// if (diffList.FirstOrDefault(x => x.X == a.X && x.Y == a.Y && x.Width == a.Width && x.Height == a.Height) == default)
// {
// diffList.Add(a);
// changeAction?.Invoke();
// Console.WriteLine("Add one");
// }
// Monitor.Exit(listMonitor);
//}
|