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

图像曲线拟合与交点检测方法问询(已完成边缘提取)

曲线拟合与交点检测实现方案

基于你已完成的边缘提取,后续可通过轮廓分离、多项式拟合、联立方程求解三步获取两条曲线的交点,具体实现如下:

1. 分离两条曲线的边缘点

Canny检测得到的边缘包含所有轮廓点,需先通过轮廓提取将两条曲线的点分离:

import cv2
import numpy as np

# 你的边缘提取代码
img = cv2.imread('image6.jpg')
grad_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, 3)
grad_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, 3)
grad = np.sqrt(grad_x**2 + grad_y**2)
grad_norm = (grad * 255 / grad.max()).astype(np.uint8)
grad_norm = cv2.blur(grad_norm,(3,3))
edges = cv2.Canny(image=grad_norm, threshold1=100, threshold2=200)

# 提取轮廓并筛选目标曲线
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 按轮廓面积排序,取前2个最大的轮廓(对应两条目标曲线)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:2]
# 将轮廓点转换为二维数组
points1 = contours[0].reshape(-1, 2)
points2 = contours[1].reshape(-1, 2)

2. 多项式曲线拟合

观察图像中曲线形态,采用二次多项式(抛物线)拟合即可满足需求:

# 拟合第一条曲线:y = a1x² + b1x + c1
x1, y1 = points1[:, 0], points1[:, 1]
coeffs1 = np.polyfit(x1, y1, 2)

# 拟合第二条曲线:y = a2x² + b2x + c2
x2, y2 = points2[:, 0], points2[:, 1]
coeffs2 = np.polyfit(x2, y2, 2)

3. 联立方程求解交点

将两条曲线的方程联立,转化为一元二次方程求解:

# 构造差值方程:(a1-a2)x² + (b1-b2)x + (c1-c2) = 0
a = coeffs1[0] - coeffs2[0]
b = coeffs1[1] - coeffs2[1]
c = coeffs1[2] - coeffs2[2]

# 求解一元二次方程的根
delta = b**2 - 4*a*c
x_intersect = [(-b + np.sqrt(delta))/(2*a), (-b - np.sqrt(delta))/(2*a)]
# 计算对应交点的y坐标
y_intersect = [np.polyval(coeffs1, x) for x in x_intersect]

# 最终交点坐标(已取整适配图像像素)
intersections = [(int(round(x)), int(round(y))) for x, y in zip(x_intersect, y_intersect)]

4. 可视化验证

将交点标记在原图上,确认结果:

# 绘制红色交点
img_result = img.copy()
for (x, y) in intersections:
    cv2.circle(img_result, (x, y), 6, (0, 0, 255), -1)
cv2.imshow('Result', img_result)
cv2.waitKey(0)
cv2.destroyAllWindows()

注意事项

  • 若轮廓提取出现干扰小轮廓,可通过设置面积阈值过滤;
  • 若曲线形态为高次曲线,可调整np.polyfit的第三个参数(拟合次数);
  • 题目明确存在两个交点,因此无需处理判别式delta≤0的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 20:02:16