python opencv-斑点检测或圆形检测 [英] python opencv - blob detection or circle detection

查看:470
本文介绍了python opencv-斑点检测或圆形检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检测圆形区域时遇到问题. 我使用来自opencv的HoughCircles函数进行了尝试.但是,即使图像非常相似,该功能的参数也必须不同才能检测到卷.

I am having problems detecting circle areas. I tried it with the HoughCircles function from opencv. However even though the images are pretty similar the parameters for the funtion have to be different in order to detect the cirles.

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

Another approach I tried was to iterate over every pixel and check if the current pixel is white. If this is the case then check if there is a blob object in the area (distance to blob center smaller than a threshold). If there is, append the pixel to the blob, if not then create a new blob. This also didn't work properly.

有人知道我该如何进行这项工作(检测到90%)吗? 我附加了一个示例图像和另一个标记了cirles的图像. 谢谢!

Has anyone a idea how I can make this work (90% detection) ? I attached a example image and another image where I marked the cirles. Thanks!

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

UPDATE: Thank you for the help so far! This is the code where I acquire the contours and filter them by area:

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)

这很整洁.我在图片上画了它们:

This works pretty neat. I drew them on the image:

这是我用圆度过滤的部分,它直接在我按面积过滤的代码下方:

This is the part where I filtered by circularity, it goes straight below the code where I filter by 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)

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

However, the new list 'contours_cirles' is empty. I printed 'circularity' in the loop and the values are all between 10 000 and 100 000.

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

UPDATE #2: After correcting the missing brackets it is working now!

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)

非常感谢你们! :)

推荐答案

作为起点,您可以开始:

As a starting point you may start with:

  • 使用cv2.findContours()
  • 查找给定图像中的所有轮廓
  • 遍历每个轮廓:
      如果轮廓的面积在给定范围内,请说计算面积.这将滤除一些非常小的和 大轮廓.
    • 用面积阈值过滤轮廓后,需要检查轮廓的边数,可使用以下方法完成: cv2.approxPolyDP(),对于一个圆,len(approx)必须大于8,但< 23.或者你可能 在此处应用一些更复杂的操作来检测圈子.
  • Find all the contours in the given image using cv2.findContours()
  • Iterate over each contour:
    • calculate the area, if the area of contour is in a given range say 70 < area < 150. This will filter out some extremely smaller and large contours.
    • After filtering the contours with the area threshold, you need to check the number of edges of contour, which can be done using: cv2.approxPolyDP(), for a circle len(approx) must be > 8 but < 23. Or you may apply some more sophisticated operations to detect circles here.

您应该尝试实现这种方法,并使用以后要编写的代码更新问题.

You should try to implement this approach and update the question with the code that you will write henceforth.

正如@Miki所建议的那样,有一种更好,更清洁的方法,即使用 circularity = 4pi(area/perimeter ^ 2)来检测几何形状是否为圆形,并确定一个阈值,例如0.9,检查形状是否为圆形.对于完美的圆circularity == 1.您可以根据需要微调此阈值.

As suggested by @Miki, there is a better and cleaner way of detecting if a geometrical shape is of circular shape using circularity = 4pi(area/perimeter^2), and decide a threshold such as 0.9, to check if the shape is circular. For perfect circle circularity == 1. You may fine tune this threshold as per your needs.

您可以咨询 arcLength 来找到周长轮廓的轮廓和 contourArea 来获取计算圆度所需的轮廓.

You may consult arcLength to find the perimeter of the contour and contourArea to get the area of the contour which are required to calculate the circularity.

这篇关于python opencv-斑点检测或圆形检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆