为什么AForge.Net BlobCounter.GetObjectsInformation()没有检测到对象?

时间:2016-03-14 06:18:30

标签: .net computer-vision aforge

我正在尝试使用AForge.Net来检测图像中的矩形。在测试图像(附后)中,我有以下代码。

        BlobCounter blobCounter = new BlobCounter();

        blobCounter.FilterBlobs = true;

        blobCounter.MinHeight = 5;
        blobCounter.MinWidth = 5;
        blobCounter.MaxHeight = 5000;
        blobCounter.MaxWidth = 5000;


        blobCounter.ProcessImage(currentImage);

        Blob[] blobs = blobCounter.GetObjectsInformation();

        // check for rectangles
        SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

但是,GetObjectsInformation()只返回一个Blob,整个画面。为什么它不能检测内部矩形?我按照例子,但不知道哪里错了。任何帮助表示赞赏。

图像 Simple test image.

编辑:好的。我发现如果我将图像存储为PNG格式,那么它可以检测矩形。但是如果我将图像存储为JPG格式,那么它就会失败。我不确定这是什么原因。我想这是因为Jpeg格式的信息丢失了。我在处理之前将图像加载到Bitmap

1 个答案:

答案 0 :(得分:2)

在搜索blob之前,你必须预处理图像,因为blob计数器在阈值图片中搜索带有黑色背景的白色物体(像素是纯白色或纯黑色),并且由于白色背景,你得到一个blob算作一个blob(整个图片)所以首先你必须做一些步骤

  1. 灰度图像(颜色到灰度)
  2. 阈值图像(灰度为白色和黑色)
  3. 反转图像(将矩形边框转换为白色)
  4. 搜索blob

    public Bitmap PreProcess(Bitmap bmp)
    {
        //Those are AForge filters "using Aforge.Imaging.Filters;"
        Grayscale gfilter = new Grayscale(0.2125, 0.7154, 0.0721);
        Invert ifilter = new Invert();
        BradleyLocalThresholding thfilter = new BradleyLocalThresholding();
        bmp = gfilter.Apply(bmp);
        thfilter.ApplyInPlace(bmp);
        ifilter.ApplyInPlace(bmp);
        return bmp;
    }
    
  5. 调用此方法为blob搜索准备图像

    blobCounter.ProcessImage(PreProcess(currentImage));
    
相关问题