python + cv2 - 确定图像中亮点的半径
python + cv2 - determine radius of bright spot in image
我已经有了可以检测图像中最亮点的代码(只是高斯模糊 + 找到最亮的像素)。我正在处理日落照片,现在可以很容易地得到这样的结果:
我的问题是圆的半径与我使用了多少高斯模糊有关——我想使它的半径反映照片中太阳的大小(我有一个数据集~我正在尝试处理 500 张日落照片)。
这是一张没有圆圈的图片:
我什至不知道从哪里开始,我缺乏传统的计算机视觉知识。如果我没有得到答案,我可能会尝试做一些事情,比如计算从圆心到最近边缘的距离(使用精明的边缘检测) - 如果有更好的方法请告诉我。感谢阅读
首先使用Canny边缘。然后在边缘图像上尝试霍夫圆或霍夫椭圆。这些是蛮力方法,所以它们会很慢,但它们对非圆形或非椭圆轮廓有抵抗力。您可以轻松过滤结果,使检测结果的中心靠近最亮点。此外,了解太阳的估计大小将有助于提高计算速度。
您还可以考虑使用 cv2.findContours
和 cv2.approxPolyDP
从图像中提取连续轮廓。您可以按周长和形状进行过滤,然后 运行 最小二乘拟合或 Hough 拟合。
编辑
在 Canny 边缘检测之前尝试强度过滤器可能是值得的。我怀疑它会大大清理边缘图像。
这是在 Python/OpenCV 中获得代表性圆圈的一种方法。它找到最小外接圆。
- 读取输入
- 裁掉右边的白色部分
- 转换为灰色
- 应用中值过滤
- 进行Canny边缘检测
- 获取所有白色像素点的坐标(canny edges)
- 计算最小外接圆以获得圆心和半径
- 在输入的副本上用该圆心和半径画一个圆
- 保存结果
输入:
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)
Canny Edge 图片:
在输入上绘制的椭圆和圆:
我已经有了可以检测图像中最亮点的代码(只是高斯模糊 + 找到最亮的像素)。我正在处理日落照片,现在可以很容易地得到这样的结果:
我的问题是圆的半径与我使用了多少高斯模糊有关——我想使它的半径反映照片中太阳的大小(我有一个数据集~我正在尝试处理 500 张日落照片)。
这是一张没有圆圈的图片:
首先使用Canny边缘。然后在边缘图像上尝试霍夫圆或霍夫椭圆。这些是蛮力方法,所以它们会很慢,但它们对非圆形或非椭圆轮廓有抵抗力。您可以轻松过滤结果,使检测结果的中心靠近最亮点。此外,了解太阳的估计大小将有助于提高计算速度。
您还可以考虑使用 cv2.findContours
和 cv2.approxPolyDP
从图像中提取连续轮廓。您可以按周长和形状进行过滤,然后 运行 最小二乘拟合或 Hough 拟合。
编辑
在 Canny 边缘检测之前尝试强度过滤器可能是值得的。我怀疑它会大大清理边缘图像。
这是在 Python/OpenCV 中获得代表性圆圈的一种方法。它找到最小外接圆。
- 读取输入
- 裁掉右边的白色部分
- 转换为灰色
- 应用中值过滤
- 进行Canny边缘检测
- 获取所有白色像素点的坐标(canny edges)
- 计算最小外接圆以获得圆心和半径
- 在输入的副本上用该圆心和半径画一个圆
- 保存结果
输入:
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)
Canny Edge 图片:
在输入上绘制的椭圆和圆: