gpt4 book ai didi

python - OpenCV:在圆圈内找到小黑(黑)点

转载 作者:行者123 更新时间:2023-12-02 16:11:52 28 4
gpt4 key购买 nike

我正在尝试检测黑点或其中有黑点的圆圈(我在下图中用箭头指向的圆圈)。
enter image description here

我目前的方法是在 OpenCV 中使用 HoughCircles 函数来检测半径大于 2 像素的圆。我对社区的问题是:假设我检测到这些圈子(如下图所示),我怎样才能将我用 指向的圈子分开?箭头 从其余的。用箭头指向的圆圈是我感兴趣的圆圈,其中有黑色/深色的圆圈。粉红色的高光是 HoughCircles 检测到的。另外,我自己添加了箭头以显示我感兴趣的圆圈。

enter image description here

这是我一直在使用的python代码。这里是 link到图像,以防你想尝试它。

import sys
import cv2 as cv
import numpy as np
import math
filename = '72471_125_df.jpg'
src = cv.imread("72471_125_df.jpg", cv.IMREAD_COLOR)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
gray = cv.medianBlur(gray, 3)
# Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height.
accum_size = 1
# Minimum distance between the centers of the detected circles.
minDist = 30
#First method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny() edge detector (the lower one is twice smaller).
param1 = 50
# Second method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.
param2 = 5
#
minRadius = 1
#
maxRadius = 10
circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, accum_size, minDist,
param1=param1, param2=param2,
minRadius=minRadius, maxRadius=maxRadius)
circles = circles.reshape(1,circles.shape[1], circles.shape[2])
if circles is not None:
circles = np.uint16(np.around(circles))
for ind, i in enumerate(circles[0, :]):
center = (i[0], i[1])
radius = 15
cv.circle(src, center, radius, (255, 0, 255), 3)

cv.imwrite("modif_"+filename,src)

请注意,我确实使用了 minRadius = 2 的配置。和 maxRadius = 5但是它并没有给我带有黑色/暗点的圆圈。由于某种原因,它也会返回其他圈子。此外,我确实尝试了阈值方法,但它在所有光照条件下都不是很稳健。请引用此 link为了观察不同光照条件下的图像。

最佳答案

这是我采取的方法,可以用来激发灵感。我不确定它是否能捕捉到所有“黑点”的情况,但您可以对此作出判断。我只是为加载的图像添加了一个阈值,然后重用了您提供的代码。

import cv2
import numpy as np

img = cv2.imread('blackdots.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(gray_img, 170, 255, cv2.THRESH_BINARY) # <--- Try different values here

accum_size = 1
# Minimum distance between the centers of the detected circles.
minDist = 30
#First method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny() edge detector (the lower one is twice smaller).
param1 = 50
# Second method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.
param2 = 5
#
minRadius = 1
#
maxRadius = 10
circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, accum_size, minDist,
param1=param1, param2=param2,
minRadius=minRadius, maxRadius=maxRadius)
circles = circles.reshape(1,circles.shape[1], circles.shape[2])
if circles is not None:
circles = np.uint16(np.around(circles))
for ind, i in enumerate(circles[0, :]):
center = (i[0], i[1])
radius = 15
cv2.circle(img, center, radius, (255, 0, 255), 3)

thresh = cv2.resize(thresh, (1280, 720)) # <---- This is just for easier display
img = cv2.resize(img, (1280, 720)) # <---- This is just for easier display
cv2.imwrite('circles_black_dot.png', img)
cv2.imwrite('threshold_black_dots.png', thresh)

阈值图像:

enter image description here

带圆圈的原始图像:

enter image description here

关于python - OpenCV:在圆圈内找到小黑(黑)点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60646050/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com