C#native图像模板匹配

时间:2015-06-04 23:21:16

标签: c# performance image-processing native template-matching

我在这里遇到一点性能问题。 我的图像处理教授让我在没有任何库的情况下进行模板匹配,所以我在c#中完成了它...它完美地完成了工作,但非常非常,非常慢 要在500x500的图像中找到80x80的模板,需要花费几分钟时间,并且使用的CPU数量不超过20%

我做错了吗?

这是我的代码,所有图像都是灰色的。

    public Task<Point[]> MatchImage(Bitmap orig, Bitmap template, int limit = 30)
    {
        Task<Point[]> tsk = 
        new Task<Point[]>(() =>
        {
            Point p;
            List<Point> l = new List<Point>();
            int oh = orig.Height, ow = orig.Width;
            int th = template.Height, tw = template.Width;

            for (int x = 0; x < oh - th; x++)
                for (int y = 0; y < ow - tw; y++)
                {
                    p = new Point(x, y);
                    if (sumBitArea(orig, template, p) <= limit)
                        l.Add(p);
                }
                return l.ToArray();
        });

        tsk.Start();
        return tsk;
    }


    /// <summary>
    /// Soma os pixels de uma determinada imagem
    /// PS PARA USAR MANDE A IMAGEM RECORTADA !
    /// SE MANDAR INTEIRA VAI RETORNAR O VALOR TOTAL.
    /// USEM A GRAPHCS E UM RECTANGLE !
    /// </summary>
    /// <param name="image">treixo da imagem original</param>
    /// <param name="template">imagem template</param>
    /// <param name="p">ponto inicial de referencia</param>
    /// <returns>o valor da soma</returns>
    private int sumBitArea(Bitmap image, Bitmap template, Point p)
    {
        int value = 0;

        int height = p.X + template.Height;
        int width = p.Y + template.Width;

        for (int x = p.X, xt = 0; x < height; x++, xt++)
            for (int y = p.Y, yt = 0; y < width; y++,yt++)
            {
                int tmp = Math.Abs(image.GetPixel(y, x).R - template.GetPixel(yt, xt).R);
                value += tmp;
            }

        return value;
    }

解决方案 - 已解决的锁定位

这是解决方案代码

    public Task<Point[]> MatchImage(Bitmap orig, Bitmap template, int limit = 30)
    {
        Task<Point[]> tsk =
        new Task<Point[]>(() =>
        {
            BitmapData lockedOrig = orig.LockBits(
               new Rectangle(0, 0, orig.Width, orig.Height),
               ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            //bloqueando a leitura da memoria em que a imagem nova sera colocada
            BitmapData lockedTemplate = template.LockBits(
               new Rectangle(0, 0, template.Width, template.Height),
               ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            Point p;
            List<Point> l = new List<Point>();
            int oh = orig.Height, ow = orig.Width;
            int th = template.Height, tw = template.Width;

            for (int y = 0; y < oh - th; y++)
            {
                for (int x = 0; x < ow - tw; x++)
                {
                    p = new Point(x, y);
                    if (sumBitArea(lockedOrig, lockedTemplate, p) <= limit)
                        l.Add(p);
                }
            }

            orig.UnlockBits(lockedOrig);
            template.UnlockBits(lockedTemplate);
            return l.ToArray();
        });

        tsk.Start();
        return tsk;
    }

    unsafe private int sumBitArea(BitmapData image, BitmapData template, Point p)
    {
        int value = 0;
        int pixelSize = 3;

        int height = p.Y + template.Height;
        int width = p.X + template.Width;
        int x = p.X, xt = 0;
        int y = p.Y, yt = 0;
        byte* tRow;
        byte* oRow;

        for (yt = 0 ; y < height; y++, yt++)
        {
            //criando um ponteiro para a imagem original
            oRow = (byte*)image.Scan0 + (y * image.Stride);

            //criando um ponteiro para a nova imagem
            tRow = (byte*)template.Scan0 + (yt * template.Stride);

            for (xt = 0; x < width; x++, xt++)
            {
                int tmp = Math.Abs((byte)oRow[x * pixelSize + 2] - (byte)tRow[xt * pixelSize + 2]);
                value += tmp;
            }
        }
        return value;
    }

1 个答案:

答案 0 :(得分:0)

您尝试锁定位还是使用FastBitmap实现?你应该能够找到像这样的大量实现。它应该加快多次获取像素。你只需要妥善处理它。