当前位置:网站首页>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;}
}
}
}
边栏推荐
- 多元线性回归(梯度下降法)
- [daily training -- Tencent selected 50] 557 Reverse word III in string
- ROS learning 4 custom message
- Ros-11 common visualization tools
- Halcon color recognition_ fuses. hdev:classify fuses by color
- Digital analog 1: linear programming
- 【日常训练--腾讯精选50】557. 反转字符串中的单词 III
- Yolov4 target detection backbone
- Ecmascript6 introduction and environment construction
- EA introduction notes
猜你喜欢

Guess riddles (2)

Programming implementation of ROS learning 2 publisher node

Lori remote control LEGO motor

Halcon Chinese character recognition

Guess riddles (8)

猜谜语啦(5)

Halcon affine transformations to regions

Halcon clolor_ pieces. Hedv: classifier_ Color recognition

Business modeling of software model | overview

Guess riddles (5)
随机推荐
多元线性回归(sklearn法)
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
319. 灯泡开关
12、动态链接库,dll
Illustrated network: what is gateway load balancing protocol GLBP?
Wheel 1:qcustomplot initialization template
Kubedm series-00-overview
[matlab] matlab reads and writes Excel
Confusing basic concepts member variables local variables global variables
Oracle advanced (III) detailed explanation of data dictionary
Array, date, string object method
Typescript hands-on tutorial, easy to understand
Lori remote control LEGO motor
Halcon Chinese character recognition
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
Ecmascript6 introduction and environment construction
猜谜语啦(8)
Guess riddles (5)
猜谜语啦(7)
Task failed task_ 1641530057069_ 0002_ m_ 000000