python 模拟车道线

目录

python opencv旋转增强模拟车道线:

pil旋转增强模拟车道线:


python opencv旋转增强模拟车道线:

import time

import cv2
import numpy as np
from scipy.interpolate import splprep, splev

def rotate_point(x, y, cx, cy, angle):
    """Rotate a point around a center by a given angle."""
    radians = np.radians(angle)
    cos_angle = np.cos(radians)
    sin_angle = np.sin(radians)
    nx = cos_angle * (x - cx) - sin_angle * (y - cy) + cx
    ny = sin_angle * (x - cx) + cos_angle * (y - cy) + cy
    return int(nx), int(ny)

def interpolate_curve(points, num_points=100):
    """Interpolate a smooth curve through the given points."""
    points = np.array(points)
    tck, u = splprep([points[:, 0], points[:, 1]], s=0)
    unew = np.linspace(0, 1, num_points)
    out = splev(unew, tck)
    return list(zip(out[0], out[1]))

def draw_curve(image, points, color):
    """Draw a curve given a list of points."""
    for i in range(len(points) - 1):
        cv2.line(image, (int(points[i][0]), int(points[i][1])), (int(points[i+1][0]), int(points[i+1][1])), color, 2)

def fill_between_curves(image, curve1, curve2, fill_color):
    """Fill the area between two curves."""
    polygon_points = np.array(curve1 + curve2[::-1], dtype=np.int32)
    cv2.fillPoly(image, [polygon_points], fill_color)

# 创建一个黑色图像
width, height = 800, 600
image = np.zeros((height, width, 3), dtype=np.uint8)

# 定义曲线的起点和其他点
fixed_point = (400, 300)
curve_points = [
    (400, 300), (450, 280), (500, 250), (550, 220), (600, 200)
]

# 插值出平滑的曲线
smooth_curve = interpolate_curve(curve_points)

stat=time.time()
# 绘制原始平滑曲线
draw_curve(image, smooth_curve, (0, 255, 0))

# 计算顺时针旋转后的平滑曲线点
rotated_curve_cw = [fixed_point]
angle_cw = 2 # 顺时针旋转角度
for x, y in smooth_curve[1:]:
    rotated_point = rotate_point(x, y, fixed_point[0], fixed_point[1], -angle_cw)
    rotated_curve_cw.append(rotated_point)

# 计算逆时针旋转后的平滑曲线点
rotated_curve_ccw = [fixed_point]
angle_ccw = 1.5  # 逆时针旋转角度
for x, y in smooth_curve[1:]:
    rotated_point = rotate_point(x, y, fixed_point[0], fixed_point[1], angle_ccw)
    rotated_curve_ccw.append(rotated_point)

# 绘制顺时针旋转后的平滑曲线
draw_curve(image, rotated_curve_cw, (255, 0, 0))

# 绘制逆时针旋转后的平滑曲线
draw_curve(image, rotated_curve_ccw, (0, 0, 255))

# 填充两条旋转曲线之间的区域
fill_between_curves(image, rotated_curve_cw, rotated_curve_ccw, (0, 0, 255))
print('time',time.time()-stat)
# 显示和保存结果
cv2.imshow("Filled Area Between Rotated Curves", image)
cv2.imwrite("filled_between_rotated_curves.png", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

pil旋转增强模拟车道线:

import numpy as np
from PIL import Image, ImageDraw
import math

def rotate_point(x, y, cx, cy, angle):
    """Rotate a point around a center by a given angle."""
    radians = np.radians(angle)
    cos_angle = np.cos(radians)
    sin_angle = np.sin(radians)
    nx = cos_angle * (x - cx) - sin_angle * (y - cy) + cx
    ny = sin_angle * (x - cx) + cos_angle * (y - cy) + cy
    return int(nx), int(ny)

def draw_curve(draw, points, color):
    """Draw a curve given a list of points."""
    for i in range(len(points) - 1):
        draw.line([points[i], points[i + 1]], fill=color, width=2)

def fill_between_curves(draw, curve1, curve2, fill_color):
    """Fill the area between two curves."""
    polygon_points = curve1 + curve2[::-1]
    draw.polygon(polygon_points, fill=fill_color)

# 创建一个黑色图像
width, height = 800, 600
image = Image.new("RGB", (width, height), (0, 0, 0))
draw = ImageDraw.Draw(image)

# 定义曲线的起点和其他点
fixed_point = (400, 300)
curve_points = [
    (400, 300), (450, 280), (500, 250), (550, 220), (600, 200)
]

# 绘制原始曲线
draw_curve(draw, curve_points, (0, 255, 0))

# 计算旋转后的曲线点
rotated_curve_points = [fixed_point]
angle = 5  # 旋转角度
for x, y in curve_points[1:]:
    rotated_point = rotate_point(x, y, fixed_point[0], fixed_point[1], angle)
    rotated_curve_points.append(rotated_point)

# 绘制旋转后的曲线
draw_curve(draw, rotated_curve_points, (255, 0, 0))

# 填充两条曲线之间的区域
fill_between_curves(draw, curve_points, rotated_curve_points, (0, 255, 255))

# 显示和保存结果
image.show()
image.save("filled_between_curves.png")

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值