我正在尝试找到一种在图像上拟合样条曲线的方法。我知道要处理的图像中有一行:
我找不到在那里适合花键的方法。这可以通过NI LabView来实现,因此很难相信OpenCV也没有办法。
这是我所走的路:
import cv2
frame = cv2.imread("./download.png")
edged = cv2.Canny(frame, 50, 200)
cv2.imshow('frame', frame)
cv2.imshow('edge', edged)
cv2.waitKey()
下面的示意图描述了我已经完成的工作以及预期的输出(手动绘制的红色样条线)。
答案 0 :(得分:1)
您可以尝试形态学操作,例如OpenCV的腐蚀和扩张。通过使用不同的内核形状,修整垂直边缘并加宽水平边缘。重复几次,然后连接水平边缘。最后使那条肥腻的水平边缘变薄。
有关如何使用腐蚀和扩张的一些示例可以在以下位置找到: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html。
答案 1 :(得分:1)
结果证明,OpenCV无法拟合样条曲线,但是我在@triiiiista的帮助下找到了解决方法。供将来参考(为简单起见,在原始图像的上部):
import numpy as np
import cv2
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
frame = cv2.imread("./download.png")
blur = cv2.blur(frame,(5,5))
edged = cv2.Canny(blur, 50, 200)
# remove vertical parts
kernel_remove_vertical = cv2.getStructuringElement(cv2.MORPH_RECT, ksize=(3, 1))# remove vertical parts
edged_hor = cv2.erode(edged, kernel_remove_vertical)
cv2.imshow('edge', edged)
cv2.imshow('edge_hor', edged_hor)
cv2.waitKey(1)
# Find coordinates for white pixels
# Hope that there aren't too many outliers :)
pixels = np.argwhere(edged_hor == 255)
x = (pixels[:, 1])
y = (pixels[:, 0])
# Interpolate with scipy
f = interp1d(x, y, kind='cubic')
xnew = np.linspace(10, 630)
plt.figure()
plt.imshow(cv2.cvtColor(edged_hor, cv2.COLOR_BGR2RGB))
plt.plot(xnew, f(xnew))