You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用OpenCV提取图像轮廓为连续路径用于DFT傅里叶变换可视化

背景

我目前正在开发一个延伸自Coding Train傅里叶变换系列的项目,非常感谢该创作者给我带来的项目灵感!
在观看了他制作的Coding Train logo傅里叶变换相关视频后,我认为尝试将该效果适配到任意图像上会很有意义。我的目标是可以将任意图像中的主体轮廓作为连续路径输入DFT算法,从而实现该路径的傅里叶变换可视化。
Coding Train相关视频链接:https://www.youtube.com/watch?v=MY4luNgGfms

已完成进度

目前我已经使用Python的OpenCV库实现了从任意图像中检测主体轮廓并展示的算法,附带样例输出如下:
处理前图像地址:https://i.sstatic.net/FOrXl.jpg
处理后图像地址:https://i.sstatic.net/R6gNF.png

遇到的问题与预期结果

我已经完成了边缘检测步骤,现在需要将检测得到的边缘提取为由(x,y)坐标表示的连续路径。需要连续路径的原因是后续要对该路径执行DFT运算,如果路径不连续,DFT可视化结果就会出错。
我不知道该如何对边缘点进行排序,才能避免点的位置出现左右、上下跳跃的情况,而是按照平滑连续的路径走向排列。
我是计算机视觉领域的初学者,非常希望能得到大家的帮助!如果我现有实现存在问题或者有可优化的地方,也欢迎大家指出,我十分想要学习相关知识。

现有实现代码

以下是我目前编写的代码:

# read in the image
img = cv2.imread("Images/selfietest.jpeg")

# convert image to gray scale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
height, width = gray_img.shape

# white_padding = np.zeros((50, width, 3))
# white_padding[:, :] = [255, 255, 255]
# rgb_img = np.row_stack((white_padding, imgOut))

gray_img = 255 - gray_img
gray_img[gray_img > 100] = 255
gray_img[gray_img <= 100] = 0
# black_padding = np.zeros((50, width))
# gray_img = np.row_stack((black_padding, gray_img))

kernel = np.ones((30, 30), np.uint8)

# fill inside of image
filledImage = cv2.morphologyEx(gray_img, cv2.MORPH_CLOSE, kernel)
filledImageNormalized = np.uint8(filledImage)

# Canny edge detection
edges = cv2.Canny(filledImageNormalized, 100, 200)

title = ['edges']
images = [edges]

for i in range(1):
    plt.subplot(1, 1, i + 1), plt.imshow(images[i], 'gray')
    plt.title(title[i])
    plt.xticks([])
    plt.yticks([])
plt.show()

内容的提问来源于stack exchange,提问作者Krishna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 20:24:04