当前位置: 首页 > news >正文

驾驶模拟左拐右拐

目录

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

驾驶模拟左拐右拐 平移

四个点选


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

import cv2
import numpy as npdef 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 randomimport cv2
import numpy as npdef toushi(turn_right,step):rows, cols = image.shape[:2]# 定义原图像中的四个角点pts1 = np.float32([[0, 0], [cols, 0], [0, rows], [cols, rows]])angle_x=4np.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_imageif __name__ == '__main__':# 读取图像aimage = 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 pointsif 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) & 0xFFif key == 27:  # 按ESC键退出breakelif 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()


http://www.mrgr.cn/news/17115.html

相关文章:

  • Pandas 5-单元格填充日期
  • 巧妙的数(逐倍数判断)
  • Pinterest账号被封?试试这几种解封方法
  • java 反射
  • Docker 容器编排之 Docker Compose
  • 数据结构--带头双向循环链表
  • asp.net实验:数据库写入不成功
  • Java、python、php版 保险业务管理与数据分析系统 社会保险档案管理系统(源码、调试、LW、开题、PPT)
  • ubuntu20.04 colmap安装
  • JavaWeb JavaScript ⑨ 正则表达式
  • spring -- AOP详解
  • 如何实现一个通用的接口限流、防重、防抖机制
  • Nginx 维护与应用:最佳实践
  • 前端宝典二十一:前端异步编程规范手写Promise、async、await
  • Python Chardet介绍
  • 重塑未来:碳捕集与存储(CCS)的革命性突破与可持续发展路径
  • 大模型目录
  • 用技术手段冲击市场,上海破获特大操纵期货市场案
  • 菜鸟教程002 目标对象的中心点与源对象的中心点对齐,获取对象中心坐标
  • 电池点焊机设计要点记录及个人分析