首页 > 解决方案 > python + cv2 - 确定图像中亮点的半径

问题描述

我已经有了可以检测图像中最亮点的代码(只是高斯模糊+找到最亮的像素)。我正在处理日落照片,现在可以很容易地得到这样的结果:

在此处输入图像描述

我的问题是圆的半径与我使用的高斯模糊量有关 - 我想让它的半径反映照片中太阳的大小(我有一个大约 500 张日落照片的数据集试图处理)。

这是没有圆圈的图像:

在此处输入图像描述 我什至不知道从哪里开始,我缺乏传统的计算机视觉知识。如果我没有得到答案,我可能会尝试做一些事情,比如计算从圆心到最近边缘的距离(使用精明的边缘检测) - 如果有更好的方法,请告诉我。感谢您阅读

标签: pythonopencvcomputer-visionobject-detectionbrightness

解决方案


这是在 Python/OpenCV 中获得代表性圆圈的一种方法。它找到最小的封闭圆。

  • 读取输入
  • 剪掉右边的白色
  • 转换为灰色
  • 应用中值过滤
  • 做 Canny 边缘检测
  • 获取所有白色像素的坐标(canny edge)
  • 计算最小封闭圆以获得中心和半径
  • 在输入的副本上绘制一个具有该中心和半径的圆
  • 保存结果

输入:

在此处输入图像描述

import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('sunset.jpg')
hh, ww = img.shape[:2]

# shave off white region on right side
img = img[0:hh, 0:ww-2]

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# median filter
median = cv2.medianBlur(gray, 3)

# do canny edge detection
canny = cv2.Canny(median, 100, 200)

# get canny points
# numpy points are (y,x)
points = np.argwhere(canny>0)

# get min enclosing circle
center, radius = cv2.minEnclosingCircle(points)
print('center:', center, 'radius:', radius)

# draw circle on copy of input
result = img.copy()
x = int(center[1])
y = int(center[0])
rad = int(radius)
cv2.circle(result, (x,y), rad, (255,255,255), 1)

# write results
cv2.imwrite("sunset_canny.jpg", canny)
cv2.imwrite("sunset_circle.jpg", result)

# show results
cv2.imshow("median", median)
cv2.imshow("canny", canny)
cv2.imshow("result", result)
cv2.waitKey(0)

精明的边缘:

在此处输入图像描述

结果圈:

在此处输入图像描述

center: (265.5, 504.5) radius: 137.57373046875

备用

将椭圆拟合到 Canny 点,然后得到两个椭圆半径的平均值作为圆的半径。请注意 Canny 参数的细微变化,以仅获取日落的顶部。

import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('sunset.jpg')
hh, ww = img.shape[:2]

# shave off white region on right side
img = img[0:hh, 0:ww-2]

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# median filter
median = cv2.medianBlur(gray, 3)

# do canny edge detection
canny = cv2.Canny(median, 100, 250)

# transpose canny image to compensate for following numpy points as y,x
canny_t = cv2.transpose(canny)

# get canny points
# numpy points are (y,x)
points = np.argwhere(canny_t>0)

# fit ellipse and get ellipse center, minor and major diameters and angle in degree
ellipse = cv2.fitEllipse(points)
(x,y), (d1,d2), angle = ellipse
print('center: (', x,y, ')', 'diameters: (', d1, d2, ')')

# draw ellipse
result = img.copy()
cv2.ellipse(result, (int(x),int(y)), (int(d1/2),int(d2/2)), angle, 0, 360, (0,0,0), 1)

# draw circle on copy of input of radius = half average of diameters = (d1+d2)/4
rad = int((d1+d2)/4)
xc = int(x)
yc = int(y)
print('center: (', xc,yc, ')', 'radius:', rad)
cv2.circle(result, (xc,yc), rad, (0,255,0), 1)

# write results
cv2.imwrite("sunset_canny_ellipse.jpg", canny)
cv2.imwrite("sunset_ellipse_circle.jpg", result)

# show results
cv2.imshow("median", median)
cv2.imshow("canny", canny)
cv2.imshow("result", result)
cv2.waitKey(0)

精明边缘图像:

在此处输入图像描述

在输入上绘制的椭圆和圆:

在此处输入图像描述


推荐阅读