【问题标题】:Detect mishapen blobs in python using OpenCV使用 OpenCV 检测 python 中的畸形 blob
【发布时间】:2016-06-16 08:36:17
【问题描述】:

我想检测下图中的两个斑点:

原文:

我想像这样检测内部:

我也想检测到外圈:

但我现在正在应用 OpenCV 的简单 blob 检测,但它并没有给我想要的结果。这是我的代码:

# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector()

# Detect blobs.
keypoints = detector.detect(image)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(image, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
final = Image.fromarray(im_with_keypoints)
final.show()

但这是斑点检测器检测到的:

OpenCV 中的 Hugh Circle 检测也不能正确识别这两种形状。

更新: 我也尝试过椭圆拟合,但它没有检测到任何一个斑点,而是检测到图像中的一些随机线。这是我用于椭圆拟合的代码。

ret,thresh = cv2.threshold(image,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt)
print M

cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])

ellipse = cv2.fitEllipse(cnt)
cv2.ellipse(im2,ellipse,(0,255,0),2)

final = Image.fromarray(image)
final.show()

感谢您在检测这些 blob 方面的任何帮助。

【问题讨论】:

  • 嗨,你觉得我下面的回答有用吗?任何 cmets 表示赞赏。谢谢。

标签: python opencv matplotlib computer-vision edge-detection


【解决方案1】:

为了检测内部斑点,您还可以尝试聚类和MSERs,因为该区域看起来很平坦。我对您图片的裁剪版本进行了下采样并应用了这些技术。

下采样图像

在这里,我使用具有 10 个集群的 kmeans。缺点是你必须指定集群的数量。

这里我使用 MSER。它更健壮。

代码是c++。请注意,您必须缩放输出才能查看详细信息。

Mat im = imread("2L6hP.png", 0);
Mat dw;
pyrDown(im, dw);

// kmeans with 10 clusters
int k = 10;
Mat rgb32fc, lbl;
dw.convertTo(rgb32fc, CV_32F);
int imsize[] = {rgb32fc.rows, rgb32fc.cols};
Mat color = rgb32fc.reshape(1, rgb32fc.rows*rgb32fc.cols);
kmeans(color, k, lbl, TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 2, KMEANS_PP_CENTERS);
Mat lbl2d = lbl.reshape(1, 2, imsize);
Mat lbldisp; // clustered result
lbl2d.convertTo(lbldisp, CV_8U, 1);

// MSER
MSER mser;
vector<vector<Point>> regions;

mser(dw, regions);
Mat regionsMat = Mat::zeros(dw.rows, dw.cols, CV_8U); // MSER result

for (size_t i = 0; i < regions.size(); i++)
{
    for (Point pt: regions[i])
    {
        uchar& val = regionsMat.at<uchar>(pt);
        if (val > 0)
        {
            val += 1;
        }
        else
        {
            val = 1;
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2014-03-28
    • 2020-07-24
    • 2018-08-10
    • 2019-11-21
    • 1970-01-01
    相关资源
    最近更新 更多