当前位置:网站首页>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;}
}
}
}
边栏推荐
- Meta标签详解
- Codeworks round 638 (Div. 2) cute new problem solution
- Programming implementation of ROS learning 2 publisher node
- C#图像差异对比:图像相减(指针法、高速)
- MPSoC QSPI flash upgrade method
- 交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
- 使用arm Neon操作,提高内存拷贝速度
- 多元线性回归(梯度下降法)
- The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
- Transfer learning and domain adaptation
猜你喜欢
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Huber Loss
[daiy4] copy of JZ35 complex linked list
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
容易混淆的基本概念 成员变量 局部变量 全局变量
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
L'information et l'entropie, tout ce que vous voulez savoir est ici.
Understanding rotation matrix R from the perspective of base transformation
随机推荐
什么是防火墙?防火墙基础知识讲解
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
Nodemon installation and use
Array,Date,String 对象方法
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
驾驶证体检医院(114---2 挂对应的医院司机体检)
[Niuke brush questions day4] jz55 depth of binary tree
Alibaba cloud sends SMS verification code
nodejs_ fs. writeFile
ROS learning 1- create workspaces and function packs
It's too difficult to use. Long articles plus pictures and texts will only be written in short articles in the future
迁移学习和域自适应
容易混淆的基本概念 成员变量 局部变量 全局变量
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
Ros-10 roslaunch summary
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Adaboost使用
2011-11-21 training record personal training (III)
多元线性回归(sklearn法)