python opencv - blob检测或圆检测

cmp*_*x96 18 python geometry opencv blobs

我在检测圆形区域时遇到问题.我尝试使用opencv中的HoughCircles函数.然而,即使图像非常相似,功能的参数也必须不同以便检测圆圈.

我尝试的另一种方法是迭代每个像素并检查当前像素是否为白色.如果是这种情况,则检查区域中是否存在blob对象(距blob中心的距离小于阈值).如果有,请将像素附加到blob,如果没有,则创建一个新blob.这也无法正常工作.

有谁知道如何使这项工作(90%检测)?我附上了一个示例图像和另一个我标记了圆圈的图像.谢谢!

例

箭头的例子

更新: 感谢您的帮助到目前为止!这是我获取轮廓并按区域过滤它们的代码:

im = cv2.imread('extract_blue.jpg')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gauss = cv2.GaussianBlur(imgray, (5, 5), 0)
ret, thresh = cv2.threshold(im_gauss, 127, 255, 0)
# get contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

contours_area = []
# calculate area and filter into new array
for con in contours:
    area = cv2.contourArea(con)
    if 1000 < area < 10000:
        contours_area.append(con)
Run Code Online (Sandbox Code Playgroud)

这非常整洁.我把它们画在图像上: contours_filtered_area

这是我通过圆度过滤的部分,它直接在我按区域过滤的代码下面:

contours_cirles = []

# check if contour is of circular shape
for con in contours_area:
    perimeter = cv2.arcLength(con, True)
    area = cv2.contourArea(con)
    if perimeter == 0:
        break
    circularity = 4*math.pi*(area/perimeter*perimeter)
    print circularity
    if 0.8 < circularity < 1.2:
        contours_cirles.append(con)
Run Code Online (Sandbox Code Playgroud)

但是,新列表'contours_cirles'为空.我在循环中打印了'圆形',值都在10 000到10万之间.

更新#2: 纠正丢失的括号后,它现在正在工作!

contours_cirles = []

# check if contour is of circular shape
for con in contours_area:
    perimeter = cv2.arcLength(con, True)
    area = cv2.contourArea(con)
    if perimeter == 0:
        break
    circularity = 4*math.pi*(area/(perimeter*perimeter))
    print circularity
    if 0.7 < circularity < 1.2:
        contours_cirles.append(con)
Run Code Online (Sandbox Code Playgroud)

非常感谢!:)

example_done

Zda*_*daR 7

作为起点,您可以从以下开始:

  • 使用查找给定图像中的所有轮廓 cv2.findContours()
  • 迭代每个轮廓:
    • 如果轮廓区域在给定范围内,则计算面积70 < area < 150.这将过滤掉一些极小和大的轮廓.
    • 使用面积阈值过滤轮廓后,需要检查轮廓边缘的数量,可以使用以下方法完成: cv2.approxPolyDP()对于圆len(大约)必须> 8但<23.或者您可以应用一些更复杂的操作在这里检测圆圈.

您应该尝试实现此方法并使用您将在此后编写的代码更新问题.

编辑: 正如@Miki所建议的那样,使用圆度= 4pi(面积/周长^ 2)检测几何形状是否为圆形形状并确定一个阈值(如0.9)以检查形状是否更好更清晰 是循环的.为了完美的圆圈circularity == 1.您可以根据需要微调此阈值.

您可以查阅arcLength以查找轮廓和contourArea的周长,以获得计算圆度所需的轮廓区域.

  • 我用_circularity_来测量一个圆形的形状:"圆度定义为:A测量形状的圆形有多接近.例如,正六边形的圆形度高于方形.定义为(\ frac) {4*\pi*Area} {周长*周长}).这意味着圆的圆度为1,正方形的圆度为0.785,依此类推. (4认同)

San*_*Dey 5

我们Hough Transformation也可以尝试检测图像中的圆圈并使用阈值来获得所需的结果(检测到以红点为中心的绿色边界线中的圆圈):

import cv2
import numpy as np

img = cv2.imread('rbv2g.jpg',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,10,
                            param1=50,param2=12,minRadius=0,maxRadius=20)

circles = np.uint16(np.around(circles))
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.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述