当前位置:网站首页>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;}
}
}
}
边栏推荐
- 特征工程
- Codeworks round 639 (Div. 2) cute new problem solution
- 生成对抗网络
- Pearson correlation coefficient
- Kubedm series-00-overview
- Halcon Chinese character recognition
- Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
- kubeadm系列-02-kubelet的配置和启动
- 2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
- Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
猜你喜欢
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
Blogger article navigation (classified, real-time update, permanent top)
Wechat H5 official account to get openid climbing account
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
nodejs_ fs. writeFile
TF coordinate transformation of common components of ros-9 ROS
Programming implementation of ROS learning 5-client node
Halcon Chinese character recognition
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
L'information et l'entropie, tout ce que vous voulez savoir est ici.
随机推荐
C#图像差异对比:图像相减(指针法、高速)
我从技术到产品经理的几点体会
Codeworks round 681 (Div. 2) supplement
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
生成对抗网络
nodejs_ 01_ fs. readFile
[daiy4] copy of JZ35 complex linked list
Luo Gu p3177 tree coloring [deeply understand the cycle sequence of knapsack on tree]
Wechat H5 official account to get openid climbing account
Ros-10 roslaunch summary
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Programming implementation of ROS learning 5-client node
多元线性回归(sklearn法)
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
Use and programming method of ros-8 parameters
2310. 个位数字为 K 的整数之和
Illustrated network: what is gateway load balancing protocol GLBP?
浅谈Label Smoothing技术