快速计算两个相似的图像之间的“污染”区域图像、相似、区域、两个

2023-09-03 06:32:36 作者:梨涡浅笑

我有两个非常相似的图像(特别是两个截图),我试图找到发现的最好的(最快)的方式对图像的区域发生了变化(如矩形阵列重新presenting的不同区域)

I have two very similar images (specifically, two screenshots) and I'm trying to find the best (quickest) way of finding which areas of the image have changed (as an array of rectangles representing the differing areas)

有几个标准:

它并不需要是像素精确但必须包括所有的变化但是小(即它是可以接受的一个单像素变化到具有围绕它的误差大幅度) 这需要快速; (理想情况下2×1920×1080的图像应该与其中一个典型的消费机购买今日20毫秒) 在它不需要配置的阈值(但如果有一个解决方案,允许这一点,那将是一个不错的奖金) 可以假定输入图像是总是完美无损耗的图像。 It does not need to be pixel-accurate but must include all changes however small (i.e. it would be acceptable for a single-pixel change to have a large margin of error around it) It needs to be fast (Ideally 2x 1920x1080 images should take < 20ms on a typical consumer machine purchased today) It does not require a configurable threshold (but if there is a solution that allows for this, it would be a nice bonus) It can be assumed that the input images are always perfect loss-less images.

我有两个工作溶液作为上只有一个是一个强力逐个象素演算这当然是很慢的。而对于其他的我试过两个图像分割成不同大小和计算校验和每个块的块,但也相当缓慢。

I have two working solutions as is but one is a brute force pixel-by-pixel calculation which of course is very slow. And for the other I tried splitting up the two images into chunks of varying sizes and calculating checksums for each chunk, but this is also quite slow.

就为了那些想知道我要建什么 - 这是一种笨(慢)的远程桌面,可以用来在浏览器中没有任何插件

Just for those wondering what I'm building - it's a kind of dumber (and slower) remote desktop that can be used in a browser without any plugins.

推荐答案

您需要做的每个像素比较的像素。我不认为它应该是缓慢的。例如,code:

You will need to do a pixel per pixel comparison. I don't think it should be that slow. For example the code:

        int size = 1920 * 1080 * 3;
        byte[] image1 = new byte[size];
        byte[] image2 = new byte[size];
        byte[] diff = new byte[size];

        var sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        for (int i = 0; i < size; i++)
        {
            diff[i] = (byte) (image1[i] - image1[i]);
        }
        sw.Stop();       
        Console.WriteLine(sw.ElapsedMilliseconds);

运行在我的笔记本电脑约40毫秒。如果这只是灰度,它在20毫秒的运行。如果你想使用真实的图像数据的差异[I]!= 0 的将表明在两个图像的变化。

runs in about 40 ms on my laptop. If it's only grayscale, it runs under 20 ms. If you would use real image data the diff[i] != 0 would indicate a change in the two images.

您的解决方案可能是缓慢的,如果您使用的是阅读的像素值的 Bitmap.GetPixel 的或其它的方法缓慢。如果是这样的话,建议找起来就的 Bitmap.LockBits的情况下的或者使用不安全的方法。

Your solution might be slow if you are reading the pixel values using Bitmap.GetPixel or another slow method. If that is the case I suggest looking up on the Bitmap.LockBits or using an unsafe method.