当前位置:网站首页>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;}
}
}
}
边栏推荐
- AdaBoost use
- Mengxin summary of LIS (longest ascending subsequence) topics
- Multiple linear regression (sklearn method)
- Beautiful soup parsing and extracting data
- Return of missing persons
- 12. Dynamic link library, DLL
- Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
- 某公司文件服务器迁移方案
- [beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
- TF coordinate transformation of common components of ros-9 ROS
猜你喜欢

AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解

微信H5公众号获取openid爬坑记

Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
![Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]](/img/ed/0483c529db2af5b16b18e43713d1d8.jpg)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]

L'information et l'entropie, tout ce que vous voulez savoir est ici.

我从技术到产品经理的几点体会

ROS learning 4 custom message
![Rebuild my 3D world [open source] [serialization-1]](/img/74/b6253845b43bc18f425d57695fba7c.jpg)
Rebuild my 3D world [open source] [serialization-1]

Applet (use of NPM package)

编辑器-vi、vim的使用
随机推荐
C#图像差异对比:图像相减(指针法、高速)
uni-app 实现全局变量
RT thread kernel quick start, kernel implementation and application development learning with notes
混淆矩阵(Confusion Matrix)
Huber Loss
Kubedm series-00-overview
JS asynchronous error handling
Confusion matrix
Adaboost使用
Alibaba cloud sends SMS verification code
Codeworks round 681 (Div. 2) supplement
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
c#比较两张图像的差异
Transfer learning and domain adaptation
资源变现小程序添加折扣充值和折扣影票插件
location search 属性获取登录用户名
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
How many checks does kubedm series-01-preflight have