【问题标题】:Incomplete Circle detection with opencv python使用opencv python检测不完整的圆
【发布时间】:2021-03-08 07:34:11
【问题描述】:

是否可以得到不完整圆的坐标?我正在使用opencv和python。所以我可以找到大部分的圈子。 但我不知道如何检测图片中不完整的圆圈。 我正在寻找一种简单的方法来解决它。

import sys
import cv2 as cv
import numpy as np

## [load]
default_file = 'captcha2.png'
# Loads an image
src = cv.imread(cv.samples.findFile(default_file), cv.IMREAD_COLOR)
## [convert_to_gray]
# Convert it to gray
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
## [convert_to_gray]

## [reduce_noise]
# Reduce the noise to avoid false circle detection
gray = cv.medianBlur(gray, 3)
## [reduce_noise]

## [houghcircles]
#rows = gray.shape[0]
circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, 5,
                          param1=1, param2=35,
                          minRadius=1, maxRadius=30)
## [houghcircles]

## [draw]
if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        center = (i[0], i[1])
        # circle center
        cv.circle(src, center, 1, (0, 100, 100), 3)
        # circle outline
        radius = i[2]
        cv.circle(src, center, radius, (255, 0, 255), 3)
## [draw]

## [display]
cv.imshow("detected circles", src)
cv.waitKey(0)
## [display]

嗨 - 还有另一张图片。我想要不完整圆圈的 x 和 y 线,左下方为浅蓝色。

这里是原图:

【问题讨论】:

  • 能否也附上当前的输出?

标签: python opencv geometry detection


【解决方案1】:

您需要移除图像的彩色背景并仅显示圆圈。

一种方法是:


二进制掩码:

使用二进制掩码,我们将检测圆圈:

代码:


# Load the libraries
import cv2
import numpy as np

# Load the image
img = cv2.imread("r5lcN.png")

# Copy the input image
out = img.copy()

# Convert to the HSV color space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Get binary mask
msk = cv2.inRange(hsv, np.array([0, 0, 130]), np.array([179, 255, 255]))

# Detect circles in the image
crc = cv2.HoughCircles(msk, cv2.HOUGH_GRADIENT, 1, 10, param1=50, param2=25, minRadius=0, maxRadius=0)

# Ensure circles were found
if crc is not None:

    # Convert the coordinates and radius of the circles to integers
    crc = np.round(crc[0, :]).astype("int")

    # For each (x, y) coordinates and radius of the circles
    for (x, y, r) in crc:

        # Draw the circle
        cv2.circle(out, (x, y), r, (0, 255, 0), 4)

        # Print coordinates
        print("x:{}, y:{}".format(x, y))

    # Display
    cv2.imshow("out", np.hstack([img, out]))
    cv2.waitKey(0)

输出:

x:178, y:60
x:128, y:22
x:248, y:20
x:378, y:52
x:280, y:60
x:294, y:46
x:250, y:44
x:150, y:62

说明

我们有三个机会找到阈值:

    1. 简单阈值结果:
    1. 自适应阈值
    1. 二进制掩码

我们可以看到第三个选项给了我们一个合适的结果。当然,您可以通过其他选项获得所需的结果,但可能需要很长时间才能找到合适的参数。然后我们应用霍夫圆,玩弄参数值,得到了想要的结果。

更新

对于第二张上传的图片,可以通过减小霍夫圆的第一个和第二个参数来检测半圆。

crc = cv2.HoughCircles(msk, cv2.HOUGH_GRADIENT, 1, 10, param1=10, param2=15, minRadius=0, maxRadius=0)

在主代码中替换上述行将导致:

控制台结果

x:238, y:38
x:56, y:30
x:44, y:62
x:208, y:26

【讨论】:

  • 嘿,谢谢,这可以帮助我找到所有圈子。我正在寻找类似你解释的东西。我喜欢二进制掩码的方式。但是也可以找到左下角的空圆圈?
  • ...找到空心圆.... 你是说不完整的圆吗?我想是的,但如果不尝试就不确定
  • 哦,是的,我的意思是不完整的圆圈。它只是一个开放的圆圈。
  • 哪一个?我以为我已经检测到输入图像中的所有圆圈
  • 我添加了另一张照片。还有一个不完整的圆圈。我正在寻找 x 和 y 线。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 2020-06-16
  • 2014-07-27
  • 2017-07-01
  • 2018-03-20
  • 2014-12-02
相关资源
最近更新 更多