【发布时间】:2014-02-06 03:28:26
【问题描述】:
我希望能在改进我的椭圆拟合方法方面得到一些帮助。我正在考虑尝试使用 RANSAC 风格的方法,但我不确定这是否是正确的方向。对于我应该开始的方向的任何帮助将不胜感激,即使这只是一种改进在我的边缘寻找。
我一直在研究这个问题,但进展不大。我认为主要问题是图像的质量,但我只能使用我现在拥有的东西。
我目前正在测试的方法是在图像上使用边缘检测,然后尝试在我找到的边缘周围拟合椭圆。下面的图片将突出我的主要问题,即我的方法对噪声的处理效果很差。
原图: http://i.imgur.com/usygfXw.jpg
Canny 边缘检测后: http://i.imgur.com/K7XDcVL.png
椭圆拟合后: http://i.imgur.com/bN0lNIq.jpg
以下是我使用的代码。对于 Canny 边缘检测,我找到了一些值,目前正在静态使用它们。它是从网上获取的代码,然后我进行了修改,现在有点hacky。
#!/usr/bin/python
import cv2
import numpy as np
import sys
from numpy.linalg import eig, inv
# param is the result of canny edge detection
def process_image(img):
# for every pixel in the image:
for (x,y), intensity in np.ndenumerate(img):
# if the pixel is part of an edge:
if intensity == 255:
# determine if the edge is similar to an ellipse
ellipse_test(img, x, y)
def ellipse_test(img, i, j):
#poor coding practice but what I'm doing for now
global output, image
i_array = []
j_array = []
# flood fill i,j while storing all unique i,j values in arrays
flood_fill(img, i, j, i_array, j_array)
i_array = np.array(i_array)
j_array = np.array(j_array)
if i_array.size >= 10:
#put those values in a numpy array
#which can have an ellipse fit around it
array = []
for i, elm in enumerate(i_array):
array.append([int(j_array[i]), int(i_array[i])])
array = np.array([array])
ellp = cv2.fitEllipse(array)
cv2.ellipse(image, ellp, (0,0,0))
cv2.ellipse(output, ellp, (0,0,0))
def flood_fill(img, i, j, i_array, j_array):
if img[i][j] != 255:
return
# store i,j values
i_array.append(float(i))
j_array.append(float(j))
# mark i,j as 'visited'
img[i][j] = 250
# flood_fill adjacent and diagonal pixels
(i_max, j_max) = img.shape
if i - 1 > 0 and j - 1 > 0:
flood_fill(img, i - 1, j - 1, i_array, j_array)
if j - 1 > 0:
flood_fill(img, i, j - 1, i_array, j_array)
if i - 1 > 0:
flood_fill(img, i - 1, j, i_array, j_array)
if i + 1 < i_max and j + 1 < j_max:
flood_fill(img, i + 1, j + 1, i_array, j_array)
if j + 1 < j_max:
flood_fill(img, i, j + 1, i_array, j_array)
if i + 1 < i_max:
flood_fill(img, i + 1, j, i_array, j_array)
if i + 1 < i_max and j - 1 > 0:
flood_fill(img, i + 1, j - 1, i_array, j_array)
if i - 1 > 0 and j + 1 < j_max:
flood_fill(img, i - 1, j + 1, i_array, j_array)
image = cv2.imread(sys.argv[1], 0)
canny_result = cv2.GaussianBlur(image, (3,3), 0)
canny_result = cv2.Canny(canny_result, 107, 208,
apertureSize=3, L2gradient=False)
#output is a blank images which the ellipses are drawn on
output = np.zeros(image.shape, np.uint8)
output[:] = [255]
cv2.waitKey(0)
cv2.namedWindow("Canny result:", cv2.WINDOW_NORMAL)
cv2.imshow('Canny result:', canny_result)
print "Press any key to find the edges"
cv2.waitKey(0)
print "Now finding ellipses"
process_image(canny_result)
print "Ellipses found!"
cv2.namedWindow("Original image:", cv2.WINDOW_NORMAL)
cv2.imshow('Original image:', image)
cv2.namedWindow("Output image:", cv2.WINDOW_NORMAL)
cv2.imshow("Output image:", output)
cv2.waitKey(0)
【问题讨论】: