0

我正在尝试在图像上绘图

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;
    }
4

1 回答 1

0

此代码周围是否有未显示的异常处理程序?Graphics.FromImage()应该抛出异常,因为您正在传递具有索引像素格式的图像Grayscale.Apply()返回带有Format8bppIndexed,的图像BlobsFiltering.Apply()并将保留它。

最重要的是,您需要创建一个Bitmap与 相同大小的新图像,BWImage从中获取您的图像,在新图像上Graphics绘画,然后矩形您的斑点。BWImage

于 2013-01-04T20:02:35.433 回答