使用OpenCV检测python中的mishapen blob

使用OpenCV检测python中的mishapen blob,python,opencv,matplotlib,computer-vision,edge-detection,Python,Opencv,Matplotlib,Computer Vision,Edge Detection,我想在下图中检测两个斑点: 原件: 我想像这样检测内部: 我还希望检测到外圆: 但是我现在正在应用OpenCV的简单斑点检测,它并没有给我想要的结果。这是我的代码: # Set up the detector with default parameters. detector = cv2.SimpleBlobDetector() # Detect blobs. keypoints = detector.detect(image) # Draw detected blobs as red

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

原件:

我想像这样检测内部:

我还希望检测到外圆:

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

# 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圆检测也无法正确识别这两种形状

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

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()

非常感谢您在检测这些斑点时提供的任何帮助。

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

下采样图像

这里我使用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;
        }
    }

}
Mat im=imread(“2L6hP.png”,0);
Mat-dw;
吡咯烷酮(im,dw);
//具有10个集群的kmeans
int k=10;
Mat rgb32fc,lbl;
dw.convertTo(rgb32fc,CV_32F);
int imsize[]={rgb32fc.rows,rgb32fc.cols};
Mat color=rgb32fc.重塑(1,rgb32fc.行*rgb32fc.列);
kmeans(颜色、k、lbl、术语标准(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,10,1.0),2,kmeans_PP_中心);
Mat lbl2d=lbl.重塑(1,2,imsize);
Mat lbldisp;//聚类结果
lbl2d.convertTo(lbldisp,CV_8U,1);
//MSER
MSER-MSER;
矢量区域;
mser(dw,区域);
Mat regionsMat=Mat::零(dw.rows,dw.cols,CV_8U);//MSER结果
对于(size_t i=0;i0)
{
val+=1;
}
其他的
{
val=1;
}
}
}

嗨,你觉得我下面的答案有用吗?如有任何意见,我们将不胜感激。谢谢