gpt4 book ai didi

C# 检查图像是否与公差相等

转载 作者:行者123 更新时间:2023-11-30 22:55:24 24 4
gpt4 key购买 nike

我有这段代码,可以从热像仪发送图像。 getImage() 返回相机提供的实际图像。无法直接检查相机是否可以提供"new"图像,所以我使用这种方法来比较两张图像:

class ImageCompare
{
public enum CompareResult
{
CompareOK,
SizeMismatch,
PixelMismatch
};

public static CompareResult CompareImages(Image i1, Image i2)
{
CompareResult cr = CompareResult.CompareOK;

if (i1.Size != i2.Size)
{
cr = CompareResult.SizeMismatch;
}
else
{
ImageConverter ic = new ImageConverter();
byte[] btImage1 = new byte[1];
btImage1 = (byte[])ic.ConvertTo(i1, btImage1.GetType());
byte[] btImage2 = new byte[1];
btImage2 = (byte[])ic.ConvertTo(i2, btImage2.GetType());

//compute hashes
SHA256Managed shaM = new SHA256Managed();
byte[] hash1 = shaM.ComputeHash(btImage1);
byte[] hash2 = shaM.ComputeHash(btImage2);

for (int i = 0; i < hash1.Length && i < hash2.Length
&& cr == CompareResult.CompareOK; i++)
{
if (hash1[i] != hash2[i])
cr = CompareResult.PixelMismatch;
}
}
return cr;
}
}

下面是我如何使用这个类:

private static void HandleImageSending(Socket client, Socket s)
{
int sent;
int imageCount = 0;
long totalSize = 0;
try
{
while (true)
{
Console.WriteLine("Starting sending...");
Image old = getImage();
byte[] bmpBytes;
using (Image bmp = getImage())
using (MemoryStream ms = new MemoryStream())
{
if (ImageCompare.CompareImages(bmp, old) == ImageCompare.CompareResult.CompareOK)
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bmpBytes = ms.ToArray();
sent = SendVarData(client, bmpBytes);
imageCount++;
totalSize += sent;
old = bmp;
}
}
}
}
catch (Exception e)
{ ... }

所以我的问题是通过散列比较结果20 个案例中大约有 19 个是“不同”图像。由于相机仅提供 8 fps,因此一定有问题。

是否有可能与某种公差进行比较,所以可以说比较的新图像中有 5% 或 10% 可能与旧图像不同?

因为这是在迷你电脑上使用的,所以我想使用尽可能少的 CPU 负载。
这里有人可以帮助我吗?

最佳答案

索引图像(并减小尺寸)应该对相似图像给出相同的结果

使用

Bitmap imgtarget = imgsource.Clone(
new Rectangle(0, 0, imgsource.Width, imgsource.Height),
PixelFormat.Format8bppIndexed);

来自 another stackoverflow

关于C# 检查图像是否与公差相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com