【问题标题】:Blob Detection with light-colored blobs用浅色斑点检测斑点
【发布时间】:2016-02-24 02:39:10
【问题描述】:

我在检测一组图像中的特定“斑点”时遇到了一些问题。并非所有图像都是相同的,但我想无论如何都会使用相同的参数来检测。

如果放大,您会在叶子上看到小的黄色蚜虫。我的目标是挑出这些并计算它们。我真的不需要对图像做太多事情,只需获取它们的数量即可。

现在,我有这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.CV.Util;

namespace AphidCounter
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read image
            Mat im_in = CvInvoke.Imread("myimage1.jpg", Emgu.CV.CvEnum.LoadImageType.Grayscale);
            //Mat im_in = CvInvoke.Imread("myimage2.png", Emgu.CV.CvEnum.LoadImageType.Color);
            Mat im = im_in;
            CvInvoke.Threshold(im_in, im, 40, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv);  // 60, 255, 1

            //CvInvoke.NamedWindow("Blob Detector", Emgu.CV.CvEnum.NamedWindowType.AutoSize);

            DetectBlobs(im, 0);

            CvInvoke.WaitKey(0);
        }

        static void DetectBlobs(Mat im, int c)
        {

            int maxT = 50;
            int minA = 125; // Minimum area in pixels
            int maxA = 550; // Maximum area in pixels

            SimpleBlobDetectorParams EMparams = new SimpleBlobDetectorParams();
            SimpleBlobDetector detector;

            EMparams.MinThreshold = 0;
            EMparams.MaxThreshold = 100;

            if (minA < 1) minA = 1;
            EMparams.FilterByArea = true;
            EMparams.MinArea = minA;
            EMparams.MaxArea = maxA;

            if (maxT < 1) maxT = 1;
            EMparams.MinConvexity = (float)maxT / 1000.0F; // 0.67

            EMparams.FilterByInertia = true;
            EMparams.MinInertiaRatio = 0.01F;

            EMparams.FilterByColor = true;
            EMparams.blobColor = 0;

            VectorOfKeyPoint keyPoints = new VectorOfKeyPoint();

            detector = new SimpleBlobDetector(EMparams);
            detector.DetectRaw(im, keyPoints);

            Mat im_with_keypoints = new Mat();
            Bgr color = new Bgr(0, 0, 255);
            Features2DToolbox.DrawKeypoints(im, keyPoints, im_with_keypoints, color, Features2DToolbox.KeypointDrawType.DrawRichKeypoints);

            // Show blobs
            CvInvoke.Imwrite("keypoints1.jpg", im_with_keypoints);
            CvInvoke.Imshow("Blob Detector " + keyPoints.Size, im_with_keypoints);

            System.Console.WriteLine("Number of keypoints: " + keyPoints.Size);

        }
    }
}

但是,结果如下:

我没有得到正确的参数吗?还是我还缺少其他东西?

【问题讨论】:

  • 请提供像您的二值图像一样的中间图像。

标签: opencv image-processing emgucv opencv3.0


【解决方案1】:

这不是因为一些错误的参数。图像分割部分本身就有其局限性。

当斑点和背景之间的对比度非常低时,基于灰度的阈值处理可能不起作用。然而,在这个例子中,160 左右的阈值是可以容忍的,但并不准确。

我建议使用基于颜色的阈值,因为存在不错的色差。

这是基于颜色的阈值处理的 C++ 实现。使用相同的 SimpleBlobDetector 过滤 Blob。

我已将图像从RGB to ‘Lab’ 转换为更好的分割。

由于提供的图片太大,处理时间较长。所以我裁剪了图像的一个关键部分并调整了 blob 参数。所以我也提供了裁剪后的图像(755 x 494px)。

基于颜色的阈值和斑点过滤:

#include "opencv2\imgproc\imgproc.hpp";
#include "opencv2\highgui\highgui.hpp";
#include "opencv2\features2d\features2d.hpp";

using namespace cv;
using namespace std;

void main()
{
    char image_path[] = "E:/Coding/media/images/leaf_small.jpg";
    Mat img_color, img_lab, img_thresh, img_open, img_close, img_keypoints;

    img_color = imread(image_path, IMREAD_ANYCOLOR);

    //Convert image to CIE Lab colorspace for better colour based segmentation
    cvtColor(img_color, img_lab, CV_BGR2Lab);

    //create window before creating trackbar
    namedWindow("win_thresh", WINDOW_NORMAL);
    namedWindow("win_blob", WINDOW_NORMAL);

    //Using trackbar calculate the range of L,a,b values to seperate blobs
    int low_L = 150, low_A = 0, low_B = 155,
        high_L = 255, high_A = 255, high_B = 255;

    //*Use trackbars to caliberate colour thresholding
    createTrackbar("low_L", "win_thresh", &low_L, 255);
    createTrackbar("low_A", "win_thresh", &low_A, 255);
    createTrackbar("low_B", "win_thresh", &low_B, 255);
    createTrackbar("high_L", "win_thresh", &high_L, 255);
    createTrackbar("high_A", "win_thresh", &high_A, 255);
    createTrackbar("high_B", "win_thresh", &high_B, 255);

    int minArea = 35, maxArea = 172, minCircularity = 58, minConvexity = 87, minInertiaRatio = 21;

    //Use trackbar and set Blob detector parameters
    createTrackbar("minArea", "win_blob", &minArea, 200);
    createTrackbar("maxArea", "win_blob", &maxArea, 200);
    createTrackbar("minCircular", "win_blob", &minCircularity, 99);
    createTrackbar("minConvex", "win_blob", &minConvexity, 99);
    createTrackbar("minInertia", "win_blob", &minInertiaRatio, 99);

    SimpleBlobDetector::Params params;
    vector<KeyPoint> keypoints;

    while (waitKey(1) != 27) //press 'esc' to quit
    {
        //inRange thresholds basedon the Scalar boundaries provided
        inRange(img_lab, Scalar(low_L, low_A, low_B), Scalar(high_L, high_A, high_B), img_thresh);

        //Morphological filling
        Mat strucElement = getStructuringElement(CV_SHAPE_ELLIPSE, Size(5, 5), Point(2, 2));
        morphologyEx(img_thresh, img_close, MORPH_CLOSE, strucElement);

        imshow("win_thresh", img_close);

        //**SimpleBlobDetector works only in inverted binary images
        //i.e.blobs should be in black and background in white.
        bitwise_not(img_close, img_close); // inverts matrix

        //Code crashes if minArea or any miin value is set to zero
        //since trackbar starts from 0, it is adjusted here by adding 1
        params.filterByArea = true;
        params.minArea = minArea + 1;
        params.maxArea = maxArea + 1;

        params.filterByCircularity = true;
        params.filterByConvexity = true;
        params.filterByInertia = true;

        params.minCircularity = (minCircularity + 1) / 100.0;
        params.minConvexity = (minConvexity + 1) / 100.0;
        params.minInertiaRatio = (minInertiaRatio + 1) / 100.0;

        SimpleBlobDetector detector(params);
        detector.detect(img_close, keypoints);
        drawKeypoints(img_color, keypoints, img_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DEFAULT);

        stringstream displayText;
        displayText = stringstream();
        displayText << "Blob_count: " << keypoints.size();
        putText(img_keypoints, displayText.str(), Point(0, 50), CV_FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 255), 2);

        imshow("win_blob", img_keypoints);
    }
    return;
}

输出截图

根据实际高清图像调整blob参数。

由于叶脉与蚜虫的颜色和强度几乎相同,因此当蚜虫靠近或正好位于叶脉顶部时,这种方法也可能完全失败。

这可以是一个临时修复,但不够强大。 必须有一种简单而稳健的方法来实现结果,使用一些过滤器、变换或边缘检测。请分享任何其他最佳解决方案(如果有)。

编辑:选择灰度阈值作为以前的方法失败

this_image 的颜色阈值方法失败

基于颜色的阈值处理具有非常窄的带宽,如果图像落在带宽内,则准确度会非常好,另一方面颜色偏移会完全破坏准确度。 由于您将处理 100 张图像,因此颜色阈值可能不适合。

我尝试了带有一些形态侵蚀和填充的正常灰度阈值,并获得了不错的准确性。此外,灰度阈值对颜色偏移具有更好的免疫力。

此外,我们还有使用 OTSU Thresholding 的自动阈值选项,它根据图像选择阈值。

代码 sn-p:

threshold(img_gray, img_thresh, 0, 255, THRESH_OTSU);

Mat strucElement = getStructuringElement(CV_SHAPE_ELLIPSE, Size(3, 3), Point(1, 1));
morphologyEx(img_thresh, img_open, MORPH_OPEN, strucElement);

其余代码保持不变。

参数值:

minArea = 75, maxArea = 1000, minCircularity = 50, minConvexity = 20, minInertiaRatio = 15

由于我们不使用颜色信息,因此很难将白蚁与蚜虫区分开来。所以min_area 必须仔细调整才能排除它们。

处理后的图像可以在这里找到img_1、img_2。

调整形态学方法和 blob 参数以获得最佳平均计数。

【讨论】:

  • 使用和不使用裁剪图像的处理时间是多少?我的目标是即时执行此类操作(每张图像 20-45 秒)。我认为会发生可接受的错误级别。我只希望平均水平在一组 100 张图像中不超过 5%-10%。
  • 这是我从中得到的几张照片。如您所见,蚜虫越多,它错过的越多。这可能是颜色差异问题吗? i.imgur.com/zPizCnL.jpgi.imgur.com/uD8XK29.jpg
  • 我已经编辑了答案以修改阈值方法。每张高清图像大约需要 15-30 秒。我的图像处理知识有限,否则我会尝试模板匹配或前景蒙版等高级方法。
  • 还要在阈值化前寻找预处理函数来增强图像,以获得更好的结果。
  • 太棒了。很抱歉耽搁了这么久 - 出现了一些问题。我注意到的一件事是它会重复计算其中一些。我已经调整了参数,但还没有找到一个好的“中间立场”来减少它们而不损失准确性。根据大小排除已标记的位置是否可行?因此,如果它检测到某种大小的蚜虫,我们会保存圆圈的内部区域并检查以确保后续的命中不在这些区域内?我想知道这会减慢多少速度......
猜你喜欢
  • 2012-04-05
  • 2012-05-21
  • 2020-09-17
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
相关资源
最近更新 更多