目录
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")