如何优化图像比较例程执行时间

时间:2012-12-12 19:05:57

标签: c# image comparison

我只需对我的桌面进行两次微小拍摄,几乎没有变化,并使用win32 api以编程方式比较两个图像。我的图像扩展名为jpg,图像高度和图像高度均为宽度是: - 身高:-768宽度: - 1366.我使用秒表课程只是为了看看我花了多少时间来获取差异图像并将其保存到桌面。我发现它需要47毫秒。是否有任何方法可以最大限度地减少例行程序所需的时间。这是我的例行代码。请查看我的例行程序并检查如何减少此例程所需的时间。感谢

    private void button1_Click(object sender, EventArgs e)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();

        Bitmap firstImg = new Bitmap(@"C:\Users\TRIDIP\Desktop\pic1.jpg");
        Bitmap secondImg = new Bitmap(@"C:\Users\TRIDIP\Desktop\pic2.jpg");

        Rectangle bounds = GetBoundingBoxForChanges(firstImg, secondImg);

        if (bounds == Rectangle.Empty)
        {
    return;
        }

        Bitmap diff = new Bitmap(bounds.Width, bounds.Height);
        Graphics g = Graphics.FromImage(diff);
        g.DrawImage(secondImg, 0, 0, bounds, GraphicsUnit.Pixel);
        g.Dispose();

        stopwatch.Stop();

        string strmm = string.Format("Time elapsed {0} {1} {2} {3}",  stopwatch.Elapsed.Hours.ToString(), stopwatch.Elapsed.Minutes.ToString(), stopwatch.Elapsed.Seconds.ToString(), stopwatch.Elapsed.Milliseconds.ToString());
        MessageBox.Show(strmm);
    }

    private Rectangle GetBoundingBoxForChanges(Bitmap _prevBitmap, Bitmap _newBitmap)
    {
        // The search algorithm starts by looking
        //    for the top and left bounds. The search
        //    starts in the upper-left corner and scans
        //    left to right and then top to bottom. It uses
        //    an adaptive approach on the pixels it
        //    searches. Another pass is looks for the
        //    lower and right bounds. The search starts
        //    in the lower-right corner and scans right
        //    to left and then bottom to top. Again, an
        //    adaptive approach on the search area is used.
        //

        // Note: The GetPixel member of the Bitmap class
        //    is too slow for this purpose. This is a good
        //    case of using unsafe code to access pointers
        //    to increase the speed.
        //

        // Validate the images are the same shape and type.
        //
        if (_prevBitmap.Width != _newBitmap.Width ||
            _prevBitmap.Height != _newBitmap.Height ||
            _prevBitmap.PixelFormat != _newBitmap.PixelFormat)
        {
            // Not the same shape...can't do the search.
            //
            return Rectangle.Empty;
        }

        // Init the search parameters.
        //
        int width = _newBitmap.Width;
        int height = _newBitmap.Height;
        int left = width;
        int right = 0;
        int top = height;
        int bottom = 0;

        BitmapData bmNewData = null;
        BitmapData bmPrevData = null;
        try
        {
            // Lock the bits into memory.
            //
            bmNewData = _newBitmap.LockBits(new Rectangle(0, 0, _newBitmap.Width, _newBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            bmPrevData = _prevBitmap.LockBits(new Rectangle(0, 0, _prevBitmap.Width, _prevBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            // The images are ARGB (4 bytes)
            //
            int numBytesPerPixel = 4;

            // Get the number of integers (4 bytes) in each row
            //    of the image.
            //
            int strideNew = bmNewData.Stride / numBytesPerPixel;
            int stridePrev = bmPrevData.Stride / numBytesPerPixel;

            // Get a pointer to the first pixel.
            //
            // Note: Another speed up implemented is that I don't
            //    need the ARGB elements. I am only trying to detect
            //    change. So this algorithm reads the 4 bytes as an
            //    integer and compares the two numbers.
            //
            System.IntPtr scanNew0 = bmNewData.Scan0;
            System.IntPtr scanPrev0 = bmPrevData.Scan0;

            // Enter the unsafe code.
            //
            unsafe
            {
                // Cast the safe pointers into unsafe pointers.
                //
                int* pNew = (int*)(void*)scanNew0;
                int* pPrev = (int*)(void*)scanPrev0;

                // First Pass - Find the left and top bounds
                //    of the minimum bounding rectangle. Adapt the
                //    number of pixels scanned from left to right so
                //    we only scan up to the current bound. We also
                //    initialize the bottom & right. This helps optimize
                //    the second pass.
                //
                // For all rows of pixels (top to bottom)
                //
                for (int y = 0; y < _newBitmap.Height; ++y)
                {
                    // For pixels up to the current bound (left to right)
                    //
                    for (int x = 0; x < left; ++x)
                    {
                        // Use pointer arithmetic to index the
                        //    next pixel in this row.
                        //
                        if ((pNew + x)[0] != (pPrev + x)[0])
                        {
                            // Found a change.
                            //
                            if (x < left)
                            {
                                left = x;
                            }
                            if (x > right)
                            {
                                right = x;
                            }
                            if (y < top)
                            {
                                top = y;
                            }
                            if (y > bottom)
                            {
                                bottom = y;
                            }
                        }
                    }

                    // Move the pointers to the next row.
                    //
                    pNew += strideNew;
                    pPrev += stridePrev;
                }

                // If we did not find any changed pixels
                //    then no need to do a second pass.
                //
                if (left != width)
                {
                    // Second Pass - The first pass found at
                    //    least one different pixel and has set
                    //    the left & top bounds. In addition, the
                    //    right & bottom bounds have been initialized.
                    //    Adapt the number of pixels scanned from right
                    //    to left so we only scan up to the current bound.
                    //    In addition, there is no need to scan past
                    //    the top bound.
                    //

                    // Set the pointers to the first element of the
                    //    bottom row.
                    //
                    pNew = (int*)(void*)scanNew0;
                    pPrev = (int*)(void*)scanPrev0;
                    pNew += (_newBitmap.Height - 1) * strideNew;
                    pPrev += (_prevBitmap.Height - 1) * stridePrev;

                    // For each row (bottom to top)
                    //
                    for (int y = _newBitmap.Height - 1; y > top; y--)
                    {
                        // For each column (right to left)
                        //
                        for (int x = _newBitmap.Width - 1; x > right; x--)
                        {
                            // Use pointer arithmetic to index the
                            //    next pixel in this row.
                            //
                            if ((pNew + x)[0] != (pPrev + x)[0])
                            {
                                // Found a change.
                                //
                                if (x > right)
                                {
                                    right = x;
                                }
                                if (y > bottom)
                                {
                                    bottom = y;
                                }
                            }
                        }

                        // Move up one row.
                        //
                        pNew -= strideNew;
                        pPrev -= stridePrev;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            int xxx = 0;
        }
        finally
        {
            // Unlock the bits of the image.
            //
            if (bmNewData != null)
            {
                _newBitmap.UnlockBits(bmNewData);
            }
            if (bmPrevData != null)
            {
                _prevBitmap.UnlockBits(bmPrevData);
            }
        }

        // Validate we found a bounding box. If not
        //    return an empty rectangle.
        //
        int diffImgWidth = right - left + 1;
        int diffImgHeight = bottom - top + 1;
        if (diffImgHeight < 0 || diffImgWidth < 0)
        {
            // Nothing changed
            return Rectangle.Empty;
        }

        // Return the bounding box.
        //
        return new Rectangle(left, top, diffImgWidth, diffImgHeight);
    }

3 个答案:

答案 0 :(得分:1)

首先,我建议通过代码计时数百/数千个通道以确定平均速度 - 单次传递永远不是计时代码的好方法。此外,您正在从磁盘加载两个大图像,这可能比整个diff操作需要更长的时间,因此您可能希望从定时测试中消除加载过程(因为无论如何都无法优化该代码)。 / p>

通过寻找上/下,然后下/右,你已经开始了正确的轨道。但也许你可以做得更好一点。你需要寻找早期和不必要的工作。例如,为什么第一个循环跟踪顶部/左侧/底部/右侧?在点击第一个差异之前,检查是否需要更新底部是没有意义的。一旦找到top,就没有必要检查是否需要更新它。因此,请考虑将循环拆分,以便在循环中更新较少的状态(但不增加您考虑的像素数)

然后你必须开始减少每像素的开销并利用你对处理器/总线/存储器架构的了解 - 例如如果你有一个64位处理器,一次读取32位很可能比一次读取64位慢 - 所以使用long(Int64)来比较每次迭代的像素而不只是一个。这样可以更好地利用处理器的寄存器宽度,并将循环的迭代次数减半(将x值的加法次数减半,再循环一次的分支数)。 (注意:您必须小心具有奇数宽度的图像,如果您发现一对不同的像素,则需要检查对中的每个像素以确定差异所在的确切位置)。根据您的处理器/总线架构,您可以使用更广泛的数据类型获得进一步的收益。

接下来,您可以看到缓存一致性是否有帮助。在第二个(底部/右侧)循环中,您将反向迭代X值。根据您的处理器架构,这可能比向前迭代它们要慢得多 - 因此您可以通过在每条扫描线中向前搜索来获得更好的平均时间。

(注意:以上可能不是“最佳方式”,并且可能会有硬件加速技巧使处理器更快,但希望它会给你一些关于如何懒惰的想法帮助你走得更快)

答案 1 :(得分:0)

这不是问题的答案,但也许这会有所帮助。

首先,我认为你大部分时间花在屏幕上的图像绘制上,而不是比较图像。尝试测量没有平局的时间,这通常是一个繁重的操作。

其次,在时间测量之前运行方法一次(或两次)是个好主意,因为第一次运行代码可能需要更长的时间来运行,更好的是运行1000次并将总时间除以1000以得到平均估计值。

最后但并非最不重要的一点,也许你可以跳过几个像素并检查每一个像素,它仍会给你带来好结果?

答案 2 :(得分:0)

在这样的代码x&gt;右内循环可以被删除(我们在循环“x”中有相同的检查)

for (int x = _newBitmap.Width - 1; x > right; x--)
// Found a change.
//
if (x > right)
{
    right = x;
}

但是在优化之前尝试重构代码。

相关问题