- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我希望能得到一些帮助来改进我的椭圆拟合方法。我正在考虑尝试使用 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)
最佳答案
这是我尝试过的,我使用 dilate
和 scipy.ndimage
来做一些处理:
import cv2
import numpy as np
image = cv2.imread("ellipse.jpg", 0)
bimage = cv2.GaussianBlur(image, (3, 3), 0)
edge_image = cv2.Canny(bimage, 107, 208,
apertureSize=3, L2gradient=False)
img2 = cv2.dilate(edge_image, np.ones((3, 3)), iterations=3)
dis_image = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
import scipy.ndimage as ndimage
labels, count = ndimage.label(img2)
for lab, idx in enumerate(ndimage.find_objects(labels.astype(int)), 1):
sy = idx[0].start
sx = idx[1].start
y, x = np.where(labels[idx] == lab)
ellp = cv2.fitEllipse(np.column_stack((x+sx, y+sy)))
cv2.ellipse(dis_image, ellp, (0, 0, 255))
这是输出:
关于python - 改进我的椭圆拟合算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21175078/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我有实体: @Entity @Table(name = "CARDS") public class Card { @ManyToOne @JoinColumn(name = "PERSON_I
我正在尝试计算二维多边形的表面法线。我正在使用 OpenGL wiki 中的 Newell 方法来计算表面法线。 https://www.opengl.org/wiki/Calculating_a_S
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 7 年前。 Improve
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我这里有以下 XML: Visa, Mastercard, , , , 0, Discover, American Express siteonly, Buyer Pay
即将发生的 Google 政策变更迫使我们实现一个对话框,以通知欧盟用户有关 Cookie/设备标识符用于广告和分析的情况。我只想向欧盟用户显示此对话框。我不想使用额外的权限(例如 android.p
本文分享自华为云社区《华为大咖说 | 企业应用AI大模型的“道、法、术” ——道:认知篇》,作者:华为云PaaS服务小智。 本期核心观点 上车:AGI是未来5~10年内,每个人都无法回避的技
我有一个与酒精相关的网站,需要先验证年龄,然后才能让他们进入该网站。我使用 HttpModule 来执行此操作,该模块检查 cookie,如果未设置,我会将它们重定向到验证页面。我验证他们的年龄并存储
在欧盟,我们有一项法律,要求网页请求存储 cookie 的许可。我们大多数人都了解 cookie 并同意它们,但仍然被迫在任何地方明确接受它们。所以我计划编写这个附加组件(ff & chrome),它
以下在 C 和/或 C++ 中是否合法? void fn(); inline void fn() { /*Do something here*/ } 让我担心的是,第一个声明看起来暗示函数将被定义
我是一名优秀的程序员,十分优秀!