当前位置:网站首页>C # compare the differences between the two images
C # compare the differences between the two images
2022-07-05 09:07:00 【Dakeshan people】
// test :
void Main()
{
var a = (Bitmap)Image.FromFile("image1.png");
var b = (Bitmap)Image.FromFile("image2.png");
var diff = PixelDiff(a, b);
}
// Method
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;
}
// Expand
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;}
}
}
}
边栏推荐
- 驾驶证体检医院(114---2 挂对应的医院司机体检)
- Applet (use of NPM package)
- OpenFeign
- Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
- Alibaba cloud sends SMS verification code
- Array, date, string object method
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
- The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
- Jenkins Pipeline 方法(函数)定义及调用
- asp.net(c#)的货币格式化
猜你喜欢

混淆矩阵(Confusion Matrix)

What is a firewall? Explanation of basic knowledge of firewall

Attention is all you need

nodejs_ fs. writeFile
![[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation](/img/d8/7291a5b14160600ba73810e6dd1eb5.jpg)
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation

RT thread kernel quick start, kernel implementation and application development learning with notes

Count of C # LINQ source code analysis

Nodejs modularization

Programming implementation of ROS learning 2 publisher node

Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
随机推荐
Array, date, string object method
Mengxin summary of LIS (longest ascending subsequence) topics
uni-app 实现全局变量
浅谈Label Smoothing技术
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Huber Loss
Confusion matrix
我的一生.
asp.net(c#)的货币格式化
2011-11-21 training record personal training (III)
Solutions of ordinary differential equations (2) examples
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
OpenFeign
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
Understanding rotation matrix R from the perspective of base transformation
12、动态链接库,dll
Mengxin summary of LCs (longest identical subsequence) topics
Jenkins pipeline method (function) definition and call
kubeadm系列-01-preflight究竟有多少check