驾驶模拟左拐右拐

目录

根据4个点确定投影变换关系:

驾驶模拟左拐右拐 平移

四个点选


根据4个点确定投影变换关系:

import cv2
import numpy as np

def apply_perspective_transform(image, src_points, dst_points):
    # 将选择的点转换为浮点数数组
    pts1 = np.float32(src_points)
    pts2 = np.float32(dst_points)

    # 计算透视变换矩阵
    perspective_matrix = cv2.getPerspectiveTransform(pts1, pts2)

    # 应用透视变换
    result = cv2.warpPerspective(image, perspective_matrix, (image.shape[1], image.shape[0]))

    return result

# 加载图像
image = cv2.imread(r'0831_1825_12.jpg')

# 定义源点和目标点(可以自行修改这些点的坐标)
src_points = [(614,251), (615,385), (821,388), (821,249)]  # 示例:原始图像的四个顶点
dst_points =[(614+100,251), (615+100,385-20), (821+110,388-20), (821+110,249)]  # 示例:原始图像的四个顶点

# 应用透视变换
result=apply_perspective_transform(image, src_points, dst_points)

cv2.imshow("image", image)
cv2.imshow("Transformed Image", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

驾驶模拟左拐右拐 平移

import random

import cv2
import numpy as np




def toushi(turn_right,step):
    rows, cols = image.shape[:2]

    # 定义原图像中的四个角点
    pts1 = np.float32([[0, 0], [cols, 0], [0, rows], [cols, rows]])
    angle_x=4

    np.random.uniform(-angle_x, angle_x)
    scale= step*10+angle_x

        # 定义变换后的四个点,模拟左转并保持车辆近似垂直向上
        # 将左侧点向右偏移,右侧点向左偏移,模拟车辆左转
        # 同时保持车辆近似垂直向上

    if turn_right:
        offset_x_left = -scale  # 左侧点向右偏移量
        offset_x_right = scale  # 右侧点向左偏移量
    else:
        offset_x_left = scale  # 左侧点向右偏移量
        offset_x_right = -scale  # 右侧点向左偏移量
    offset_y = 0  # 垂直偏移量

    pts2 = np.float32([
        [offset_x_left, offset_y],                     # 左上角
        [cols - offset_x_right, 0],                    # 右上角
        [offset_x_left, rows - offset_y],              # 左下角
        [cols - offset_x_right, rows]                  # 右下角
    ])

    # 计算透视变换矩阵
    M = cv2.getPerspectiveTransform(pts1, pts2)

    # 应用透视变换
    transformed_image = cv2.warpPerspective(image, M, (cols, rows))
    return transformed_image

if __name__ == '__main__':
    # 读取图像a
    image = cv2.imread(r'B:\project\jijia\lixiang\data_util\imgs\000002\0831_1824_1.jpg')
    image = cv2.imread(r'B:\project\jijia\lixiang\data_util\imgs\000002\0831_1825_12.jpg')
    # 图像的尺寸
    # 图像的尺寸

    while True:

        turn_right = random.choice([0, 1])


        for step in range(1,12):
            print(turn_right,step)
            transformed_image=toushi(turn_right, step)
            # 显示原图和变换后的图像
            cv2.imshow('Original Image', image)
            cv2.imshow('Transformed Image', transformed_image)

            cv2.waitKey(0)
            cv2.destroyAllWindows()

四个点选

import cv2
import numpy as np

# 初始化全局变量
points = []


# 定义鼠标回调函数,用于记录用户点击的点
def select_points(event, x, y, flags, param):
    global points
    if event == cv2.EVENT_LBUTTONDOWN:
        points.append((x, y))
        cv2.circle(image, (x, y), 5, (0, 255, 0), -1)
        cv2.imshow("Original Image", image)
        if len(points) == 4:
            sorted_points = sort_points_clockwise(points)
            apply_perspective_transform(sorted_points)


# 将点按顺时针顺序排序的函数
def sort_points_clockwise(pts):
    # 将点转换为NumPy数组
    pts = np.array(pts)

    # 计算中心点
    center = np.mean(pts, axis=0)

    # 计算点相对于中心点的角度
    angles = np.arctan2(pts[:, 1] - center[1], pts[:, 0] - center[0])

    # 按照角度排序,逆时针顺序,所以使用负号将其变为顺时针排序
    sorted_pts = pts[np.argsort(angles)]

    return sorted_pts


# 应用透视变换的函数
def apply_perspective_transform(sorted_points):
    # 定义目标图像的四个顶点
    width, height = image.shape[1], image.shape[0]
    pts1 = np.float32(sorted_points)
    print(pts1)
    # 目标点矩阵, 我们选择目标图像的四个角落作为目标点
    pts2 = np.float32([[0, 0], [width, 0], [width, height], [0, height]])

    # 计算透视变换矩阵
    perspective_matrix = cv2.getPerspectiveTransform(pts1, pts2)

    # 应用透视变换
    result = cv2.warpPerspective(image_copy, perspective_matrix, (width, height))

    # 显示结果
    cv2.imshow("Transformed Image", result)


# 加载图像
image = cv2.imread(r'B:\project\jijia\lixiang\data_util\imgs\000002\0831_1825_12.jpg')
height, width = image.shape[:2]
center_x = width // 2
cv2.line(image, (center_x, 0), (center_x, height), (255, 0, 0), 2)  # 蓝色垂直线
image_copy = image.copy()

# 在图像中绘制一条垂直线


# 创建窗口并设置鼠标回调函数
cv2.imshow("Original Image", image)
cv2.setMouseCallback("Original Image", select_points)

print("请点击图像中的四个点来选择透视变换的区域,按ESC键退出。")

# 主循环
while True:
    key = cv2.waitKey(1) & 0xFF
    if key == 27:  # 按ESC键退出
        break
    elif key == ord('r'):  # 按'r'键重置选择
        points = []
        image = image_copy.copy()
        cv2.line(image, (center_x, 0), (center_x, height), (255, 0, 0), 2)  # 重绘垂直线
        cv2.imshow("Original Image", image)

cv2.destroyAllWindows()

你可能感兴趣的:(opencv基础,opencv,计算机视觉,人工智能)