当前位置:网站首页>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;}
}
}
}
边栏推荐
- Halcon blob analysis (ball.hdev)
- How to manage the performance of R & D team?
- TypeScript手把手教程,简单易懂
- golang 基础 —— golang 向 mysql 插入的时间数据和本地时间不一致
- 【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
- 【日常训练--腾讯精选50】557. 反转字符串中的单词 III
- [牛客网刷题 Day4] JZ32 从上往下打印二叉树
- Confusing basic concepts member variables local variables global variables
- ROS learning 4 custom message
- [daiy4] copy of JZ35 complex linked list
猜你喜欢
RT-Thread内核快速入门,内核实现与应用开发学习随笔记
319. Bulb switch
IT冷知识(更新ing~)
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
STM32 lights up the 1.8-inch screen under Arduino IDE
微信H5公众号获取openid爬坑记
Halcon snap, get the area and position of coins
Halcon affine transformations to regions
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
Programming implementation of subscriber node of ROS learning 3 subscriber
随机推荐
Bit operation related operations
Apaas platform of TOP10 abroad
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
Basic number theory -- Euler function
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
Low code platform | apaas platform construction analysis
Hello everyone, welcome to my CSDN blog!
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
EA introduction notes
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
ROS learning 4 custom message
How can fresh students write resumes to attract HR and interviewers
Digital analog 2: integer programming
Classification of plastic surgery: short in long long long
How many checks does kubedm series-01-preflight have
Wechat H5 official account to get openid climbing account
Mengxin summary of LIS (longest ascending subsequence) topics
Typescript hands-on tutorial, easy to understand
[牛客网刷题 Day4] JZ55 二叉树的深度
Old Wang's esp8266 and old Wu's ws2818 light strip