快速的方式为位图转换为C#中的布尔数组? [英] Fast way to convert a Bitmap into a Boolean array in C#?

查看:86
本文介绍了快速的方式为位图转换为C#中的布尔数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我作出XNA应用程序,我从一个摄像头捕捉屏幕截图4次秒钟,然后我尝试将其转换成一个布尔数组当像素红色是低于某一阈值。当我把它转换成一个Texture2D并不落后,但是当我设法得到它确实滞后的各个像素,即使在网络摄像头分辨率176×144是

这是code抢位图:

 公共位图getBitmap()
    {
        如果(!panelVideo preview.IsDisposed)
        {
            位图B =新位图(panelVideo preview.Width,panelVideo preview.Height,PixelFormat.Format32bppRgb);
            使用(图形G = Graphics.FromImage(b)条)
            {
                矩形rectanglePanelVideo preVIEW = panelVideo preview.Bounds;
                点sourcePoints = panelVideo preview.PointToScreen(新点(panelVideo preview.ClientRectangle.X,panelVideo preview.ClientRectangle.Y));
                g.CopyFromScreen(sourcePoints,Point.Empty,rectanglePanelVideo preview.Size);
            }            返回b;
        }
        其他
        {
            位图B =新位图(panelVideo preview.Width,panelVideo preview.Height);
            返回b;
        }
    }

这是code将位图转换为布尔数组:

 公共BOOL [,] getBoolBitmap(UINT treshold)
    {
        位图B = getBitmap();        布尔[,] AR =新布尔[b.Width,b.Height]        对于(INT Y = 0; Y< b.Height; Y ++)
        {
            为(中间体X = 0; X&下; b.Width; X ++)
            {
                如果(b.GetPixel(X,Y).R&下;​​ treshold)
                {
                    AR [X,Y] = FALSE;
                }
                其他
                {
                    AR [X,Y] =真;
                }
            }
        }        返回AR;
    }


解决方案

由Hans帕桑特提供的答案是正确的,它是更好地使用LockBits和一次全部处理数据。

您也可以尝试编写阈值的数据的着色器,从而利用GPU的强大并行处理输入的图像流得多,快得多。

I am making an XNA application where I capture the screenshot from a webcam 4 times a second and then I try to convert it into an Boolean array when the pixel color Red is below a certain threshold. When I convert it into a Texture2D it doesn't lag but when I try to get the individual pixels it does lag, even when the webcam resolution is 176x144.

This is the code to grab the Bitmap:

public Bitmap getBitmap()
    {
        if (!panelVideoPreview.IsDisposed)
        {
            Bitmap b = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height, PixelFormat.Format32bppRgb);
            using (Graphics g = Graphics.FromImage(b))
            {
                Rectangle rectanglePanelVideoPreview = panelVideoPreview.Bounds;
                Point sourcePoints = panelVideoPreview.PointToScreen(new Point(panelVideoPreview.ClientRectangle.X, panelVideoPreview.ClientRectangle.Y));
                g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size);
            }

            return b;
        }
        else
        {
            Bitmap b = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height);
            return b;
        }
    }

This is the code to convert the Bitmap to a Boolean array:

public bool[,] getBoolBitmap(uint treshold)
    {
        Bitmap b = getBitmap();

        bool[,] ar = new bool[b.Width, b.Height];

        for (int y = 0; y < b.Height; y++)
        {
            for (int x = 0; x < b.Width; x++)
            {
                if (b.GetPixel(x, y).R < treshold)
                {
                    ar[x, y] = false;
                }
                else
                {
                    ar[x, y] = true;
                }
            }
        }

        return ar;
    }

解决方案

The answer provided by Hans Passant is correct, it is better to use LockBits and process the data all at once.

You might also try writing a shader that thresholds the data and thereby utilize the power of the GPU to parallel-process the input image stream much, much faster.

这篇关于快速的方式为位图转换为C#中的布尔数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆