当前位置:网站首页>c#比较两张图像的差异
c#比较两张图像的差异
2022-07-05 08:45:00 【大可山人】
//测试:
void Main()
{
var a = (Bitmap)Image.FromFile("image1.png");
var b = (Bitmap)Image.FromFile("image2.png");
var diff = PixelDiff(a, b);
}
//方法
unsafe Bitmap PixelDiff(Bitmap a, Bitmap b)
{
Bitmap output = new Bitmap(a.Width, a.Height, PixelFormat.Format32bppArgb);
Rectangle rect = new Rectangle(Point.Empty, a.Size);
using (var aData = a.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
using (var bData = b.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
using (var outputData = output.LockBitsDisposable(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb))
{
byte* aPtr = (byte*)aData.Scan0;
byte* bPtr = (byte*)bData.Scan0;
byte* outputPtr = (byte*)outputData.Scan0;
int len = aData.Stride * aData.Height;
for (int i = 0; i < len; i++)
{
// For alpha use the average of both images (otherwise pixels with the same alpha won't be visible)
if ((i + 1) % 4 == 0)
*outputPtr = (byte)((*aPtr + *bPtr) / 2);
else
*outputPtr = (byte)~(*aPtr ^ *bPtr);
outputPtr++;
aPtr++;
bPtr++;
}
}
return output;
}
//扩展
static class Extensions
{
public static DisposableImageData LockBitsDisposable(this Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
{
return new DisposableImageData(bitmap, rect, flags, format);
}
public class DisposableImageData : IDisposable
{
private readonly Bitmap _bitmap;
private readonly BitmapData _data;
internal DisposableImageData(Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
{
bitmap.CheckArgumentNull("bitmap");
_bitmap = bitmap;
_data = bitmap.LockBits(rect, flags, format);
}
public void Dispose()
{
_bitmap.UnlockBits(_data);
}
public IntPtr Scan0
{
get { return _data.Scan0; }
}
public int Stride
{
get { return _data.Stride;}
}
public int Width
{
get { return _data.Width;}
}
public int Height
{
get { return _data.Height;}
}
public PixelFormat PixelFormat
{
get { return _data.PixelFormat;}
}
public int Reserved
{
get { return _data.Reserved;}
}
}
}
边栏推荐
- Illustrated network: what is gateway load balancing protocol GLBP?
- Ros-11 common visualization tools
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- kubeadm系列-01-preflight究竟有多少check
- Speech recognition learning summary
- Halcon wood texture recognition
- Guess riddles (8)
- Business modeling of software model | object modeling
- An enterprise information integration system
- 猜谜语啦(7)
猜你喜欢
猜谜语啦(3)
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Illustration of eight classic pointer written test questions
Typescript hands-on tutorial, easy to understand
Lori remote control LEGO motor
It cold knowledge (updating ing~)
Wechat H5 official account to get openid climbing account
IT冷知识(更新ing~)
TF coordinate transformation of common components of ros-9 ROS
Programming implementation of ROS learning 2 publisher node
随机推荐
12、动态链接库,dll
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
Use arm neon operation to improve memory copy speed
Business modeling of software model | stakeholders
Warning: retrying occurs during PIP installation
ABC#237 C
Guess riddles (4)
猜谜语啦(7)
[牛客网刷题 Day4] JZ55 二叉树的深度
Some pitfalls of win10 network sharing
猜谜语啦(6)
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
MPSoC QSPI Flash 升级办法
kubeadm系列-01-preflight究竟有多少check
Low code platform | apaas platform construction analysis
Run menu analysis
整形的分类:short in long longlong
Halcon affine transformations to regions
Explore the authentication mechanism of StarUML
Confusing basic concepts member variables local variables global variables