python - 查找图像中的所有圆圈

标签 python opencv

我是 python 和图像处理的新手。我正在从事一个业余爱好项目,我想在其中找到图像中的所有圆圈,然后找出哪个圆圈内有十字('X')标记。到目前为止,我已经将一些代码放在一起来查找圆圈(如下)。它适用于一张图片,但无法识别另一张图片上的所有圆圈。请指导我如何提高 find_circles 算法的性能。

测试图片:

test image

结果图片:

result image

import cv2
import cv
import numpy as np
import operator
from PIL import Image

def find_circles(img):
    im_gray = cv2.imread(img, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    img_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
    cv2.imwrite('img_bw.png',img_bw)
    rows, cols =img_bw.shape
    circles = cv2.HoughCircles(img_bw,cv.CV_HOUGH_GRADIENT,1,rows/32, param1=100,param2=40,minRadius=0,maxRadius=100)
    circles = np.uint16(np.around(circles))
    return circles

def draw_circles(img, circles):
    img = cv2.imread(img,0)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    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)
    return cimg 

def main():
    img = "query_circle9.png"
    circles = find_circles(img)
    img_circle = draw_circles(img,circles)
    cv2.imwrite('cricle.png',img_circle) 

if __name__=='__main__':
    main()

最佳答案

#!/usr/bin/env python

import cv2

def draw_circles(img, circles):
    # img = cv2.imread(img,0)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    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)
    return cimg

def detect_circles(image_path):
    gray = cv2.imread(image_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    gray_blur = cv2.medianBlur(gray, 13)  # Remove noise before laplacian
    gray_lap = cv2.Laplacian(gray_blur, cv2.CV_8UC1, ksize=5)
    dilate_lap = cv2.dilate(gray_lap, (3, 3))  # Fill in gaps from blurring. This helps to detect circles with broken edges.
    # Furture remove noise introduced by laplacian. This removes false pos in space between the two groups of circles.
    lap_blur = cv2.bilateralFilter(dilate_lap, 5, 9, 9)
    # Fix the resolution to 16. This helps it find more circles. Also, set distance between circles to 55 by measuring dist in image.
    # Minimum radius and max radius are also set by examining the image.
    circles = cv2.HoughCircles(lap_blur, cv2.cv.CV_HOUGH_GRADIENT, 16, 55, param2=450, minRadius=20, maxRadius=40)
    cimg = draw_circles(gray, circles)
    print("{} circles detected.".format(circles[0].shape[0]))
    # There are some false positives left in the regions containing the numbers.
    # They can be filtered out based on their y-coordinates if your images are aligned to a canonical axis.
    # I'll leave that to you.
    return cimg

结果:

cimg = detect_circles("circles.png")

Detected circles

有一些遗留的错误检测。如果您的图像是对齐的,那么您可以根据它们的 y 坐标过滤这些误报。我会把它留给你。

关于python - 查找图像中的所有圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317011/

相关文章:

python - Jinja 宏看不到传递给 render_template 的值

python - 使用 scikit learn 检索错误分类的文档

python - 从同一模块动态实例化类会导致 main 运行两次

python - python 中的 SFTP 方法获取文件的基本名称和完整路径

python - 使用 findContour 和生成的轮廓近似线段以找到线交点

c++ - 如何在 OpenCV 中将图像调整为特定大小?

ios - 如何消除偏色或调整白平衡?

c++ - 有限制的随机矩阵

python - Python中有计算百分比的运算符吗?

python - 如何使用 Opencv 和 python 从图像中识别最大的边界矩形并将它们分成单独的图像