使用Aforge.net绘制图像不起作用

时间:2012-12-25 20:01:56

标签: c# drawing aforge

我正在尝试绘制图像

Graphics g = Graphics.FromImage(BWImage);
        Pen red = new Pen(Color.Red, 2);
        foreach (Blob blob in blobs)
        {
            List<IntPoint> leftPoints, rightPoints, edgePoints = new List<IntPoint>();

            // get blob's edge points
            blobCounter.GetBlobsLeftAndRightEdges(blob,
                 out leftPoints, out rightPoints);

            edgePoints.AddRange(leftPoints);
            edgePoints.AddRange(rightPoints);

            // blob's convex hull
            List<IntPoint> hull = hullFinder.FindHull(edgePoints);

            g.DrawPolygon(red, ToPointsArray(hull));

        }

我从网络摄像头获取帧,并为每个帧我做一些处理然后做那个代码,但它没有用,我试着调试它,我发现了行

Graphics g = Graphics.FromImage(BWImage);

它会一直执行,而不会在它之后执行任何行! 有什么帮助吗?谢谢。

这是我的代码

 private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();

        ProcessImage(bitmap);
    }

和ProcessImage方法:

#region Skin Detection
        YCbCrFiltering YCbCrFilter = new YCbCrFiltering();

        YCbCrFilter.Y = new Range(0, 1);
        YCbCrFilter.Cb = new Range(-0.1862745098039216f, 0.0294117647058824f);
        YCbCrFilter.Cr = new Range(0.0137254901960784f, 0.2254901960784314f);
        // apply the filter
        var YCbCrFilteredImage = YCbCrFilter.Apply(bitmap);

        #endregion

        #region GrayScale

        // create grayscale filter (BT709)
        Grayscale Gray_filter = new Grayscale(0.2125, 0.7154, 0.0721);
        // apply the filter
        Bitmap grayImage = Gray_filter.Apply(YCbCrFilteredImage);

        Threshold threshold = new Threshold(50);
        Bitmap BWImage = threshold.Apply(grayImage);

        #endregion

        #region Remove noise
        // create filter
        BlobsFiltering BlobsFilteringfilter = new BlobsFiltering();
        // configure filter
        BlobsFilteringfilter.CoupledSizeFiltering = true;
        BlobsFilteringfilter.MinWidth = 150;
        BlobsFilteringfilter.MinHeight = 150;
        BlobsFilteringfilter.MaxHeight = 600;
        BlobsFilteringfilter.MaxWidth = 600; 
        // apply the filter
        var BWImageFiltered = BlobsFilteringfilter.Apply(BWImage);
        #endregion


        #region Fill Holes
        FillHoles FillHolesfilter = new FillHoles();
        FillHolesfilter.MaxHoleHeight = 50;
        FillHolesfilter.MaxHoleWidth = 50;
        FillHolesfilter.CoupledSizeFiltering = false;
        var BWImageFilteredHoles = FillHolesfilter.Apply(BWImageFiltered);
        #endregion

        #region Contouring

        // process image with blob counter
        BlobCounter blobCounter = new BlobCounter();
        blobCounter.ProcessImage(BWImageFilteredHoles);
        Blob[] blobs = blobCounter.GetObjectsInformation();

        Graphics g = Graphics.FromImage(BWImageFilteredHoles);
        Pen red = new Pen(Color.Red, 2);
        // create convex hull searching algorithm
        GrahamConvexHull hullFinder = new GrahamConvexHull();
foreach (Blob blob in blobs)
        {
            List<IntPoint> AllEdgesPoints = new List<IntPoint>();
            List<IntPoint> leftPoints, rightPoints, edgePoints = new List<IntPoint>();

            // get blob's edge points
            blobCounter.GetBlobsLeftAndRightEdges(blob,
                 out leftPoints, out rightPoints);

            edgePoints.AddRange(leftPoints);
            edgePoints.AddRange(rightPoints);

            // blob's convex hull
            List<IntPoint> hull = hullFinder.FindHull(edgePoints);

            g.DrawPolygon(red, ToPointsArray(hull));
 g.Dispose();

        #endregion

        pictureBox1.Image = grayImage;
        pictureBox2.Image = BWImage;
    }

1 个答案:

答案 0 :(得分:0)

此代码周围是否有未显示的异常处理程序? Graphics.FromImage()应该抛出异常,因为你是passing an image with an indexed pixel format; Grayscale.Apply()会返回Format8bppIndexed的图片,而BlobsFiltering.Apply()会保留该图片。

从底线开始,您需要创建一个与Bitmap大小相同的新BWImage,从中获取Graphics,将BWImage绘制到新图像上,然后矩形你的斑点。