【问题标题】:SimpleBlobDetector - Isolate blobs that are distinct from surroundingSimpleBlobDetector - 隔离与周围不同的斑点
【发布时间】:2019-07-04 08:06:53
【问题描述】:

我使用SimpleBlobDetector 来定位小数点和其他类型的标点符号,如下图所示,有时检测器会从文本的实心区域(底部的中间 9),我正在寻找一种通过SimpleBlobDetector 或在后期处理中过滤掉这些检测的方法。

有没有办法指定一个 blob 必须与其背景颜色分开?也许是一种边缘检测方法?

感谢您的帮助。

检测器代码为:

    params = cv2.SimpleBlobDetector_Params()
    params.filterByArea = True
    params.minArea = 30
    params.minThreshold = 50
    params.maxThreshold = 200
    params.filterByConvexity = True
    params.minConvexity = 0.87
    params.filterByColor = True
    detector = cv2.SimpleBlobDetector_create(params)
    detections = detector.detect(img)

带有检测的输出图像

原文:

【问题讨论】:

    标签: python image opencv image-processing computer-vision


    【解决方案1】:

    这里没有使用SimpleBlobDetector,而是使用边缘/轮廓检测的解决方案,它允许更多的过滤控制。主要思想是

    • 将图像转换为灰度
    • 高斯模糊
    • 从背景中分离主要特征的阈值图像
    • 执行精确边缘检测
    • 扩张 canny 图像以增强和闭合轮廓
    • 在图像中查找轮廓并使用最小/最大阈值区域进行过滤

    阈值图像

    Canny 边缘检测

    扩张以增强轮廓

    根据面积检测和过滤轮廓

    输出结果

    检测到的轮廓:1

    import numpy as np
    import cv2
    
    original_image = cv2.imread("1.jpg")
    image = original_image.copy()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (3, 3), 0)
    thresh = cv2.threshold(blurred, 110, 255,cv2.THRESH_BINARY)[1]
    canny = cv2.Canny(thresh, 150, 255, 1)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dilate = cv2.dilate(canny, kernel, iterations=1)
    
    cv2.imshow("dilate", dilate)
    cv2.imshow("thresh", thresh)
    cv2.imshow("canny", canny)
    
    # Find contours in the image
    cnts = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    
    contours = []
    
    threshold_min_area = 1100
    threshold_max_area = 1200
    
    for c in cnts:
        area = cv2.contourArea(c)
        if area > threshold_min_area and area < threshold_max_area:
            cv2.drawContours(original_image,[c], 0, (0,255,0), 3)
            contours.append(c)
    
    cv2.imshow("detected", original_image) 
    print('contours detected: {}'.format(len(contours)))
    cv2.waitKey(0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2019-06-21
      • 2011-08-21
      • 2014-05-20
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      相关资源
      最近更新 更多