使用OpenCV和Python检测接触/重叠的圆/椭圆

27
我想测量圆的圆度(“圆”的高度和宽度或椭圆参数之间的差异)。这些圆在图片中给出如下所示: 经过像素灰度化、二值化和边框检测等常规操作,我得到了以下图片: 通过这些,我已经尝试了很多不同的方法:使用findContour进行Watershed分割(类似于这个问题),但OpenCV检测到圆之间的空间作为封闭轮廓而不是圆形,因为它们粘在一起并没有形成封闭轮廓。fitEllipse也有相同的问题,我得到了在黑色背景轮廓上拟合的椭圆而不是在两圆之间拟合的椭圆。即使像代码和第三张图片所示地尝试应用霍夫变换,也会产生奇怪的结果: 代码请见此处:
import sys
import cv2
import numpy
from scipy.ndimage import label

# Application entry point
#img = cv2.imread("02_adj_grey.jpg")
img = cv2.imread("fuss02.jpg")

# Pre-processing.
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    
cv2.imwrite("SO_0_gray.png", img_gray)

#_, img_bin = cv2.threshold(img_gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
_, img_bin = cv2.threshold(img_gray, 170, 255, cv2.THRESH_BINARY)
cv2.imwrite("SO_1_threshold.png", img_bin)

#blur = cv2.GaussianBlur(img,(5,5),0)
img_bin = cv2.morphologyEx(img_bin, cv2.MORPH_CLOSE, numpy.ones((3, 3), dtype=int))
cv2.imwrite("SO_2_img_bin_morphoEx.png", img_bin)

border = img_bin - cv2.erode(img_bin, None)
cv2.imwrite("SO_3_border.png", border)


circles = cv2.HoughCircles(border,cv2.cv.CV_HOUGH_GRADIENT,50,80, param1=80,param2=40,minRadius=10,maxRadius=150)
print circles

cimg = img
for i in circles[0,:]:
# draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
    cv2.putText(cimg,str(i[0])+str(',')+str(i[1]), (i[0],i[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, 255)

cv2.imwrite("SO_8_cimg.png", cimg)

有人有改进我的算法或完全不同的方法的想法吗?我尝试了很多不同的方法,但到目前为止都没有成功。感谢大家的帮助。


你在从图像中提取圆形方面遇到了问题吗?我不太明白你想要什么。 - rayryeng
是的,我无法像您在上面的边缘检测图像中看到的那样分离圆。许多边界在过滤等过程中会丢失。 - Merlin
我有一些想法,请给我一点时间。 - rayryeng
2个回答

60

以下是我的检测圆形的尝试。总结如下:

  • 执行BGR->HSV转换,使用V通道进行处理

V通道:

enter image description here

  • 二值化,应用形态学闭运算,然后进行距离变换(我将其称为dist

dist图像:

enter image description here

  • 创建一个模板。根据图像中圆的大小,大约75像素半径的圆盘看起来比较合适。取其距离变换并将其用作模板(我将其称为temp

temp图像:

enter image description here

  • 执行模板匹配:dist * temp

dist * temp图像:

enter image description here

  • 查找结果图像的局部最大值。最大值的位置对应于圆心,最大值对应于它们的半径

模板匹配图像的阈值处理:

enter image description here

检测圆形作为局部最大值:

enter image description here

我使用C++完成了这个项目,因为我对它最熟悉。如果您觉得这有用,我认为您可以很容易地将其转换为Python。请注意,上述图像不是按比例缩放的。希望这可以帮到您。

编辑:添加Python版本

C++:

    double min, max;
    Point maxLoc;

    Mat im = imread("04Bxy.jpg");
    Mat hsv;
    Mat channels[3];
    // bgr -> hsv
    cvtColor(im, hsv, CV_BGR2HSV);
    split(hsv, channels);
    // use v channel for processing
    Mat& ch = channels[2];
    // apply Otsu thresholding
    Mat bw;
    threshold(ch, bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
    // close small gaps
    Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
    Mat morph;
    morphologyEx(bw, morph, CV_MOP_CLOSE, kernel);
    // take distance transform
    Mat dist;
    distanceTransform(morph, dist, CV_DIST_L2, CV_DIST_MASK_PRECISE);
    // add a black border to distance transformed image. we are going to do
    // template matching. to get a good match for circles in the margin, we are adding a border
    int borderSize = 75;
    Mat distborder(dist.rows + 2*borderSize, dist.cols + 2*borderSize, dist.depth());
    copyMakeBorder(dist, distborder, 
            borderSize, borderSize, borderSize, borderSize, 
            BORDER_CONSTANT | BORDER_ISOLATED, Scalar(0, 0, 0));
    // create a template. from the sizes of the circles in the image, 
    // a ~75 radius disk looks reasonable, so the borderSize was selected as 75
    Mat distTempl;
    Mat kernel2 = getStructuringElement(MORPH_ELLIPSE, Size(2*borderSize+1, 2*borderSize+1));
    // erode the ~75 radius disk a bit
    erode(kernel2, kernel2, kernel, Point(-1, -1), 10);
    // take its distance transform. this is the template
    distanceTransform(kernel2, distTempl, CV_DIST_L2, CV_DIST_MASK_PRECISE);
    // match template
    Mat nxcor;
    matchTemplate(distborder, distTempl, nxcor, CV_TM_CCOEFF_NORMED);
    minMaxLoc(nxcor, &min, &max);
    // threshold the resulting image. we should be able to get peak regions.
    // we'll locate the peak of each of these peak regions
    Mat peaks, peaks8u;
    threshold(nxcor, peaks, max*.5, 255, CV_THRESH_BINARY);
    convertScaleAbs(peaks, peaks8u);
    // find connected components. we'll use each component as a mask for distance transformed image,
    // then extract the peak location and its strength. strength corresponds to the radius of the circle
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(peaks8u, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
    {
        // prepare the mask
        peaks8u.setTo(Scalar(0, 0, 0));
        drawContours(peaks8u, contours, idx, Scalar(255, 255, 255), -1);
        // find the max value and its location in distance transformed image using mask
        minMaxLoc(dist, NULL, &max, NULL, &maxLoc, peaks8u);
        // draw the circles
        circle(im, maxLoc, (int)max, Scalar(0, 0, 255), 2);
    }

Python:

import cv2

im = cv2.imread('04Bxy.jpg')
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
th, bw = cv2.threshold(hsv[:, :, 2], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
morph = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
dist = cv2.distanceTransform(morph, cv2.cv.CV_DIST_L2, cv2.cv.CV_DIST_MASK_PRECISE)
borderSize = 75
distborder = cv2.copyMakeBorder(dist, borderSize, borderSize, borderSize, borderSize, 
                                cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0)
gap = 10                                
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*(borderSize-gap)+1, 2*(borderSize-gap)+1))
kernel2 = cv2.copyMakeBorder(kernel2, gap, gap, gap, gap, 
                                cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0)
distTempl = cv2.distanceTransform(kernel2, cv2.cv.CV_DIST_L2, cv2.cv.CV_DIST_MASK_PRECISE)
nxcor = cv2.matchTemplate(distborder, distTempl, cv2.TM_CCOEFF_NORMED)
mn, mx, _, _ = cv2.minMaxLoc(nxcor)
th, peaks = cv2.threshold(nxcor, mx*0.5, 255, cv2.THRESH_BINARY)
peaks8u = cv2.convertScaleAbs(peaks)
contours, hierarchy = cv2.findContours(peaks8u, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
peaks8u = cv2.convertScaleAbs(peaks)    # to use as mask
for i in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[i])
    _, mx, _, mxloc = cv2.minMaxLoc(dist[y:y+h, x:x+w], peaks8u[y:y+h, x:x+w])
    cv2.circle(im, (int(mxloc[0]+x), int(mxloc[1]+y)), int(mx), (255, 0, 0), 2)
    cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 255), 2)
    cv2.drawContours(im, contours, i, (0, 0, 255), 2)

cv2.imshow('circles', im)

非常好的解释和说明(+1) - George Profenza
这是我找到的寻找圆的最佳解决方案(以及此链接:http://ceng.anadolu.edu.tr/CV/EDCircles)。 - chase
可能与此处所示相同:https://docs.opencv.org/3.0-rc1/d2/dbd/tutorial_distance_transform.html - Joe

1
我在您的代码@ dhanuskha中遇到了一些错误。 我猜这是因为我正在使用不同版本的CV。 如果需要,此代码适用于CV 3.0。
import cv2

im = cv2.imread('input.png')
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
th, bw = cv2.threshold(hsv[:, :, 2], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
morph = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
dist = cv2.distanceTransform(morph, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
borderSize = 75
distborder = cv2.copyMakeBorder(dist, borderSize, borderSize, borderSize, borderSize, 
                                cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0)
gap = 10                                
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*(borderSize-gap)+1, 2*(borderSize-gap)+1))
kernel2 = cv2.copyMakeBorder(kernel2, gap, gap, gap, gap, 
                                cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0)
distTempl = cv2.distanceTransform(kernel2, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
nxcor = cv2.matchTemplate(distborder, distTempl, cv2.TM_CCOEFF_NORMED)
mn, mx, _, _ = cv2.minMaxLoc(nxcor)
th, peaks = cv2.threshold(nxcor, mx*0.5, 255, cv2.THRESH_BINARY)
peaks8u = cv2.convertScaleAbs(peaks)
_, contours, hierarchy = cv2.findContours(peaks8u, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
peaks8u = cv2.convertScaleAbs(peaks)    # to use as mask
for i in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[i])
    _, mx, _, mxloc = cv2.minMaxLoc(dist[y:y+h, x:x+w], peaks8u[y:y+h, x:x+w])
    cv2.circle(im, (int(mxloc[0]+x), int(mxloc[1]+y)), int(mx), (255, 0, 0), 2)
    cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 255), 2)
    cv2.drawContours(im, contours, i, (0, 0, 255), 2)

cv2.imshow('circles', im)
cv2.waitKey(0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接