使用Aforge.net检测有限区域的形状

时间:2015-01-26 07:48:14

标签: image limit shape detection aforge

我的形状检测应用有问题。我的应用程序应检测单色图片上的小方块。我写了一个检测到形状的部分,但我不知道如何将找到的对象限制为小的,它会检测到一切。我使用emgu.cv编写了simmilar应用程序,在那里我使用了类Contour并使用了

if(currentContour.Area> 250&& currentContour.Area< 800)

我可以用aforge写一些类似的东西吗?或者你可以帮我理解如何限制这个吗?

代码:

        ColorFiltering colorFilter = new ColorFiltering();

        colorFilter.Red = new IntRange(0, 64);
        colorFilter.Green = new IntRange(0, 64);
        colorFilter.Blue = new IntRange(0, 64);
        colorFilter.FillOutsideRange = false;

        colorFilter.ApplyInPlace(bitmapData);

        BlobCounter blobCounter = new BlobCounter();

        blobCounter.FilterBlobs = true;
        blobCounter.MinHeight = 5;
        blobCounter.MinWidth = 5;

        blobCounter.ProcessImage(bitmapData);
        Blob[] blobs = blobCounter.GetObjectsInformation();
        bitmap.UnlockBits(bitmapData);

        SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

        Graphics g = Graphics.FromImage(bitmap);
        Pen redPen = new Pen(Color.Red, 2);
        Pen brownPen = new Pen(Color.Brown, 2);

        for (int i = 0, n = blobs.Length; i < n; i++)
        {
            List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

            {
                List<IntPoint> corners;

                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
                    Pen pen;
                    pen = (corners.Count == 4) ? redPen : brownPen;
                    g.DrawPolygon(pen, ToPointsArray(corners));
                }
            }
        }

        redPen.Dispose();
        brownPen.Dispose();
        g.Dispose();

        Clipboard.SetDataObject(bitmap);
        pictureBox1.Image = bitmap;

1 个答案:

答案 0 :(得分:0)

我使用了blobCounter.Min *和blobCounter.Max *值来按大小限制找到的对象。

以下方法适合我。希望它有用:

    public static Blob[] findBlobs(Bitmap bitmap, bool invertBitmap, bool blur, int divisor, bool color, int rangeMin, int rangeMax, bool aussen, int colorId, int blobMin, int blobMax, bool koppeln) {
        // lock image
        BitmapData bitmapData = bitmap.LockBits(
            new Rectangle(0, 0, bitmap.Width, bitmap.Height),
            ImageLockMode.ReadWrite, bitmap.PixelFormat);


        if(invertBitmap) {
            new Invert().ApplyInPlace(bitmapData);
        }


        if(blur) {
            Blur blur_filter = new Blur();
            blur_filter.Divisor = divisor;//100;
            blur_filter.ApplyInPlace(bitmapData);
        }

        if(color) {
            ColorFiltering colorFilter = new ColorFiltering();
            colorFilter.Red = new AForge.IntRange(rangeMin, rangeMax);
            colorFilter.Green = new AForge.IntRange(rangeMin, rangeMax);
            colorFilter.Blue = new AForge.IntRange(rangeMin, rangeMax);
            colorFilter.FillOutsideRange = aussen;
            colorFilter.ApplyInPlace(bitmapData);
        }

        BlobCounter blobCounter = new BlobCounter();
        blobCounter.FilterBlobs = true;
        blobCounter.CoupledSizeFiltering = koppeln;
        switch(colorId) {
            case 0: blobCounter.BackgroundThreshold = Color.Black; break;
            case 1: blobCounter.BackgroundThreshold = Color.DarkGray; break;
            case 2: blobCounter.BackgroundThreshold = Color.Gray; break;
            case 3: blobCounter.BackgroundThreshold = Color.LightGray; break;
            case 4: blobCounter.BackgroundThreshold = Color.WhiteSmoke; break;
            case 5: blobCounter.BackgroundThreshold = Color.White; break;
            default: blobCounter.BackgroundThreshold = Color.Black; break;
        }

        blobCounter.MinHeight = blobMin;//12;
        blobCounter.MinWidth = blobMin;//12;
        blobCounter.MaxHeight = blobMax;//30;
        blobCounter.MaxWidth = blobMax;//30;
        blobCounter.ProcessImage(bitmapData);

        bitmap.UnlockBits(bitmapData);

        return blobCounter.GetObjectsInformation();
    }