对于循环,应用程序挂起

时间:2011-01-22 20:14:42

标签: c# loops for-loop nested-loops

我正在创建一个应用程序,我正在使用for循环基本上读取图像的每个像素寻找像素颜色的模式(简单的东西)无论如何由于某种原因我的应用程序只是锁定并且永远不会恢复正常。我一次又一次地遍历代码而没有看到任何实际问题。

我唯一注意到的是,ScanPixelsLater 中的for循环可能提前退出。我尽可能地评论了代码,

private Point topLeftc, bottomLeftc, topRightc, bottomRightc;

/// <summary>
/// Starts the initial looping process, designed only to loop through ONCE, ScanPixelsLater takes over
/// </summary>
/// <param name="img">Image to scan</param>
public void ScanPixels(Bitmap img)
{
    int whitePixel = 0;

    for (int y = 100; y < img.Height; y++)
    {

        for (int x = 100; x < img.Width; x++)
        {

            if (img.GetPixel(x, y) == Color.FromArgb(255, 255, 255, 255))
            {
                // img.SetPixel(x, y, Color.Green);
                whitePixel++;

            }
            else { whitePixel = 0; }


            if (whitePixel == 18)
            {
                whitePixel = 0;
                topLeftc = new Point(x - 18, y);
                DetectNextWhiteLine(topLeftc, img);

            }
        }
    }


}


/// <summary>
/// First creates the TopRight value via using the last pixel in x range, and using the current Y value
/// Then a Y loop is started 10 pixels down, within this loop is another X loop which scans the X range
/// If 10 consecutive white pixels are found, the TopLeft X value and the current Y value are used to map the 
/// BottomLeft and BottomRight coordinates. Finally a new Point is created which starts (x = 1) and (y = currentYValue + 2)
/// The ScanPixelsLater method is then called, passing the new Point (newLocation).
/// 
/// </summary>
/// <param name="p">The x and y value of where the pixels were found</param>
/// <param name="img">Image being used</param>
private void DetectNextWhiteLine(Point p, Bitmap img)
{

    int whitePixel = 0;
    topRightc = new Point(img.Width, topLeftc.Y);

    for (int y = p.Y + 10; y < img.Height; y++)
    {

        for (int x = p.X - 5; x < img.Width; x++)
        {
            if (img.GetPixel(x, y) == Color.FromArgb(255, 255, 255, 255))
            {
                whitePixel++;
            }
            else
            {
                whitePixel = 0;
            }

            if (whitePixel == 10)
            {
                bottomLeftc = new Point(topLeftc.X, y);
                bottomRightc = new Point(img.Width, y);
                Cords.Add(new Coordinates(topLeftc, topRightc, bottomLeftc, bottomRightc));

                Point newLocation = new Point(1, y + 2);
                calls++;
                ScanPixelsLater(newLocation, img); //rescan the image from new y axis 
            }
        }
    }

}



/// <summary>
/// Loops through the pixels based on the p parameter, if 15 white pixels are found, the location (x & y) is
/// passed to the DetectNextWhiteLine method which fixes the next line with a similar number of white pixels.
/// </summary>
/// <param name="p">The Point(x,y) at which to start scanning</param>
/// <param name="img"></param>
private void ScanPixelsLater(Point p, Bitmap img)
{

    int whitePixel = 0;

    for (int y = p.Y; y < img.Height; y++)
    {

        for (int x = p.X; x < img.Width; x++)
        {
            if (img.GetPixel(x, y) == Color.FromArgb(255, 255, 255, 255))
            {
                whitePixel++;
            }
            else
            {
                whitePixel = 0;
            }

            if (whitePixel == 15)
            {
                bottomLeftc = new Point(topLeftc.X, y);
                topLeftc = new Point(x - 15, y);
                calls++;
                DetectNextWhiteLine(topLeftc, img);
            }

        }
    }

    // only want this to execute after all the pixels within the entire img have been read
    // possibly executing early.

    DrawWhiteLines(img);
    AppArgs aa = new AppArgs(true);
    Change(this, aa); // custom event handler, fired behind form to update GUI

}

2 个答案:

答案 0 :(得分:2)

因此,要了解您的应用程序暂停的原因,您需要了解WinForm应用程序的工作原理。

运行UI的线程也具有所谓的消息泵。此消息泵包含从操作系统(和其他源)传递到所有UI元素的消息。他们告诉他们何时改变状态,何时重绘自己等等。当你有一个像你这样的长时间循环时,消息泵无法处理消息。 get排队但从未处理过,这就是应用程序“挂起”的意思。

您的应用程序不可能永远不会恢复。你的循环最终将结束,你的UI将再次响应(假设我没有错过任何地方的无限循环,但我不认为我做了)。但是,GDI + GetPixel方法实际上非常慢,如果你的图像很大,那么这组循环将需要很长时间才能完成。您可能需要深入研究不安全的上下文并使用LockBits获取指向图像内存的指针。这里有很多如何做到这一点的例子。

编辑:在仔细查看您的代码之后,显然它的效率相对较低。你有至少6级嵌套for循环,所以当你只需要一次扫描时,你实际上是多次扫描图像。

图像处理是一个资源密集型过程。您需要小心尽可能高效地完成所有性能密集型工作。

答案 1 :(得分:2)

从我的评论中迁移(现已删除)

这不是导致你麻烦的原因,因为它永远不会正常但你永远不应该在循环中调用GetPixel。使用它的速度令人难以置信。相反,您可以使用指针或像素数组使用google或stackoverflow搜索“getpixel slow”,并且会出现大量解决方案。

更新:我现在看了一下代码...在主代码(ScanPixels)中,这是一个嵌套的for循环,你调用{{1}这也是一个嵌套的for循环,最后调用DetectNextWhiteLine,它也是一个嵌套的for循环。现在你可能有一个6级深度的嵌套for循环O(n ^ 6),它调用了一个相对昂贵的方法(ScanPixelsLater)。您应该只迭代几次像素。这可能就是它永远不会停止的原因,因为这可能是1000 ^ 6 * ~100条指令:)

相关问题