精易论坛

标题: 【2023开源大赛】C#隔行扫描代码 [打印本页]

作者: 陽陽陽    时间: 2023-11-16 13:43
标题: 【2023开源大赛】C#隔行扫描代码
本帖最后由 陽陽陽 于 2023-11-22 09:26 编辑
效果图:
两张大图:



隔行扫描找不同后的图:



里面带着不同的开始坐标。

延迟大的图片在100ms左右,越小越快
目前还没找到改进的方法,欢迎大佬改进。。。

[hide=d100]
[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;
}
}
[/hide]
编辑补充:
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);
                        //}



作者: Suky    时间: 2023-11-16 14:13
我是来看你隔行扫描代码结构的
但是我看不懂C

作者: idbucunzai    时间: 2023-11-16 14:32
感谢分享
作者: pipicool    时间: 2023-11-16 15:45
学习一下
作者: gdhong    时间: 2023-11-16 18:08
感谢分享
作者: bianyuan456    时间: 2023-11-16 20:40
已经顶贴,感谢您对论坛的支持!
作者: 一指温柔    时间: 2023-11-17 08:56
感谢分享,很给力!~
作者: 396384183    时间: 2023-11-17 13:24
谢谢分享
作者: pipicool    时间: 2023-11-17 14:36
学习一下
作者: 9527287    时间: 2023-11-18 14:23
代码写的很不错。
作者: 陽陽陽    时间: 2023-11-22 09:26
@LWB666
作者: 陽陽陽    时间: 2023-11-22 09:27
Suky 发表于 2023-11-16 14:13
我是来看你隔行扫描代码结构的
但是我看不懂C

获取图片WH,每隔X列扫描一行,循环获取像素
作者: 七颗心    时间: 2023-11-22 10:30

作者: wuxinglong71    时间: 2023-11-22 12:32
支持开源~!感谢分享
作者: yokie    时间: 2023-11-22 14:53
支持开源~!感谢分享
作者: cheng732119477    时间: 2023-11-22 17:59
顶顶顶顶顶顶顶
作者: sunsail2018    时间: 2023-11-22 18:57
支持开源~!感谢分享
作者: LWB666    时间: 2023-11-22 19:44
支持
作者: 大金刚弟弟    时间: 2023-11-22 19:47
坛友自建2023开源大赛
作者: huowang8888    时间: 2023-11-22 22:27
支持自建开源大赛
作者: 小飞爱精易    时间: 2023-11-22 23:13
支持
作者: 陈枫    时间: 2023-11-23 00:26
看见2023开源大赛,反手就是一个赞
作者: hhf4977    时间: 2023-11-23 08:36
感谢分享,很给力!~
作者: gxxiaoyuan    时间: 2023-11-23 12:11
感谢这位网友组织的开源大赛
作者: 7024178    时间: 2023-11-23 12:57
感谢这位网友组织的开源大赛

作者: 学易未精    时间: 2023-11-23 13:33
支持开源,感谢      
作者: Mr.Zhang    时间: 2023-11-23 13:36

作者: linqing4    时间: 2023-11-23 13:37
感谢分享,很给力!~
作者: tan666    时间: 2023-11-23 13:44
感谢这位网友组织的开源大赛
作者: reveriexue    时间: 2023-11-23 13:47
感谢分享,很给力!~

作者: 又见花儿开    时间: 2023-11-23 14:40
66666666666666666666666
作者: wohenhao123    时间: 2023-11-23 14:51
感谢分享
作者: ctry78985    时间: 2023-11-23 15:35
感谢分享 学习一下
作者: zhongzutao    时间: 2023-11-23 16:32
里面带着不同的开始坐标。
作者: wuyanqing187    时间: 2023-11-23 16:37
膜拜大佬作品
作者: 曲终人散啊    时间: 2023-11-23 16:42
感谢分享,
作者: 不会要饭的乞丐    时间: 2023-11-23 18:03
感谢分享,很给力!~
作者: 13434230728    时间: 2023-11-23 18:09
谢谢分享。。。
作者: ZHuanR    时间: 2023-11-23 19:34
新技能已get√
作者: zhaofu123    时间: 2023-11-24 09:48
【2023开源大赛】C#隔行扫描代码
作者: 韦贝贝    时间: 2023-11-24 13:14
        感谢分享,很给力!~
作者: gogjjogdgv    时间: 2023-11-25 15:29

作者: zhangzhenqing    时间: 2023-11-27 21:24
感谢大佬分享~~
作者: yuxuanju    时间: 2024-6-3 10:23
学习中,感谢分享。




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4