diff --git a/test_density.py b/test_density.py new file mode 100644 index 0000000..2d7f24e --- /dev/null +++ b/test_density.py @@ -0,0 +1,207 @@ +""" +示例: 密度图可视化 + +本示例演示如何: +1. 获取拼接图像 +2. 计算光流 +3. 基于光流生成 density map +4. 使用 colormap 实时显示 +""" +import os +import argparse + +import cv2 +import numpy as np +import orisys + +from getDensity import getFilteredDensity + +os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0" + + +def density_to_colormap(density): + """Convert density map to a displayable heatmap.""" + if density is None or density.size == 0: + return np.zeros((1, 1, 3), dtype=np.uint8) + + dmin = float(np.min(density)) + dmax = float(np.max(density)) + if dmax > dmin: + density_u8 = ((density - dmin) * (255.0 / (dmax - dmin))).astype(np.uint8) + else: + density_u8 = np.zeros_like(density, dtype=np.uint8) + return cv2.applyColorMap(density_u8, cv2.COLORMAP_TURBO) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--video", "-v", type=str, default="0", help="Camera ID / Video file path") + parser.add_argument("--config", "-c", type=str, default="./config/ddjx01.json", help="Configuration file path") + parser.add_argument("--cal", "-cal", type=str, default="./config/ddjx01.npy", help="Calibration file path") + parser.add_argument("--verbose", "-verbose", type=bool, default=True, help="Print detailed information") + args = parser.parse_args() + + # 步骤1:创建传感器对象 (必要步骤) + try: + vid_src = int(args.video) + except ValueError: + vid_src = args.video + sensor = orisys.Sensor(vid_src, config_name=args.config, cal_path=args.cal, verbose=args.verbose) + + print("\n按 'q' 退出程序,按 'r' 重置传感器追踪器") + + while True: + # 步骤2:获取图像并计算形变 + img = sensor.get_img() + if img is None: + print("Failed to get image (end of stream or capture error). Exiting.") + break + + sensor.compute_deformation(check_motion=False) + sensor.compute_contact() + (fps, flow, contour, centroid) = sensor.read_info( + sensor.info.FPS, + sensor.info.VRAW, + sensor.info.CONTOUR, + sensor.info.CENTROID, + ) + + # 步骤3:根据光流计算 density map 并显示 + density = getFilteredDensity(flow, use_cuda=sensor.cuda) + density_view = density_to_colormap(density) + cv2.imshow("Density Map", density_view) + + image_with_foe = orisys.util.draw_contact(img, contour, centroid) + cv2.imshow("Contact Centroid", image_with_foe) + + print(f"fps: {fps:.2f}, density_shape: {density.shape}, flow_shape: {flow.shape}") + + # 步骤4:键盘控制 + key = cv2.waitKey(1) & 0xFF + if key == ord("q"): + print("\n退出程序...") + break + if key == ord("r"): + sensor.reset() + print("传感器已重置") + + sensor.disconnect() + + +if __name__ == "__main__": + main() +""" +示例 1: 基础用法 - 单传感器同步处理 + +本示例演示 orisys 的基本使用流程: +1. 初始化传感器(视频文件或摄像头) +2. 循环获取图像并计算形变和触碰点 +3. 读取各类传感器数据 +4. 可视化显示 + + +""" +import os +os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0" +import cv2 +import orisys +import numpy as np +import argparse + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--video","-v", type=str, default="0", help="Camera ID / Video file path") + parser.add_argument("--config","-c", type=str, default="./config/ddjx01.json", help="Configuration file path") + parser.add_argument("--cal","-cal", type=str, default="./config/ddjx01.npy", help="Calibration file path") + parser.add_argument("--verbose","-verbose", type=bool, default=True, help="Print detailed information") + args = parser.parse_args() + + # 步骤1:创建传感器对象 (必要步骤), + # config_name为配置文件名(内置配置文件名)或路径(自定义配置文件路径) + # cal_path为校准文件路径, 每一个设备需要创建/使用一个校准文件 + + # 方式1: 从视频文件读取 + try: + input = int(args.video) + is_camera = True + sensor = orisys.Sensor(input, config_name=args.config, cal_path= args.cal, verbose=args.verbose) + except: + is_camera = False + sensor = orisys.Sensor(args.video, config_name=args.config, cal_path= args.cal, verbose=args.verbose) + + print("\n按 'q' 退出程序,按 'r' 重置传感器追踪器") + + while True: + # 步骤2:信息提取 + # 步骤 2.1: 获取并拼接图像 (必要步骤) + sensor.get_img() + # 步骤 2.2: 计算形变场 (必要步骤) + is_touched = sensor.compute_deformation(check_motion=True, threshold=6) + # 步骤 2.3: 计算触碰点 + if is_touched: + sensor.compute_contact() + + + #步骤 2.4: 读取信息 + fps, flow, vnormal, img, contour, centroid, depth_map, fnormal, fshearx, fsheary = sensor.read_info( + sensor.info.FPS, + sensor.info.VRAW, + sensor.info.VNORMAL, + sensor.info.IMG, + sensor.info.CONTOUR, + sensor.info.CENTROID, + sensor.info.DEPTH, + sensor.info.FNORMAL, + sensor.info.FSHEARX, + sensor.info.FSHEARY + ) + + #步骤 3: 可视化显示 + # 步骤 3.1: 绘制形变场 + arrows = orisys.util.draw_arrows( + img, + flow, + threshold=2, + grid_spacing=20, + arrow_scale=1.0 + ) + cv2.imshow("Deformation", arrows) + + # 步骤 3.2: 在图像上触碰点和区域 + image_with_foe = orisys.util.draw_contact(img, contour, centroid) + cv2.imshow("Contact Centroid", image_with_foe) + + # 步骤 3.3: 绘制深度图 + # 归一化散度值到0-255范围(使用绝对值,然后归一化) + div_abs = depth_map + if div_abs.max() > div_abs.min(): + div_normalized = ((div_abs - div_abs.min()) / (div_abs.max() - div_abs.min()) * 255).astype(np.uint8) + else: + div_normalized = np.zeros_like(div_abs, dtype=np.uint8) + + # 应用颜色映射(使用JET:蓝色=低散度,红色=高散度) + cv2.imshow("Depth Map", cv2.applyColorMap(div_normalized, cv2.COLORMAP_JET)) + + + + #步骤 3.4: 打印资料 + print(f"fps: {fps}, fnormal: {fnormal}, fshearx: {fshearx}, fsheary: {fsheary}, depth_shape: {depth_map.shape}, sensor.resolution_raw: {sensor.img_size_raw}") + + #步骤 3.5: 键盘控制 + key = cv2.waitKey(1) & 0xFF + if key == ord("q"): + print("\n退出程序...") + break + if key == ord("r"): + # 重置光流追踪器(当追踪漂移时使用) + sensor.reset() + print("传感器已重置") + + # ========================================================================= + # 4. 清理资源 + # ========================================================================= + sensor.disconnect() # 释放视频源和相关资源 + + +if __name__ == '__main__': + main() diff --git a/test_of_vis.py b/test_of_vis.py new file mode 100644 index 0000000..31f6167 --- /dev/null +++ b/test_of_vis.py @@ -0,0 +1,121 @@ +""" +示例 1: 基础用法 - 单传感器同步处理 + +本示例演示 orisys 的基本使用流程: +1. 初始化传感器(视频文件或摄像头) +2. 循环获取图像并计算形变和触碰点 +3. 读取各类传感器数据 +4. 可视化显示 + + +""" +import os +os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0" +import cv2 +import orisys +import numpy as np +import argparse + +def flow_to_colormap(flow): + """Visualize optical flow with hue for direction and value for magnitude.""" + if flow is None or flow.size == 0: + return np.zeros((1, 1, 3), dtype=np.uint8) + + fx = flow[:, :, 0].astype(np.float32, copy=False) + fy = flow[:, :, 1].astype(np.float32, copy=False) + mag, ang = cv2.cartToPolar(fx, fy, angleInDegrees=True) + + hsv = np.zeros(flow.shape[:2] + (3,), dtype=np.uint8) + hsv[:, :, 0] = (ang / 2).astype(np.uint8) + hsv[:, :, 1] = 255 + if mag.max() > 0: + hsv[:, :, 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8) + return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--video","-v", type=str, default="0", help="Camera ID / Video file path") + parser.add_argument("--config","-c", type=str, default="./config/ddjx01.json", help="Configuration file path") + parser.add_argument("--cal","-cal", type=str, default="./config/ddjx01.npy", help="Calibration file path") + parser.add_argument("--verbose","-verbose", type=bool, default=True, help="Print detailed information") + args = parser.parse_args() + + # 步骤1:创建传感器对象 (必要步骤), + # config_name为配置文件名(内置配置文件名)或路径(自定义配置文件路径) + # cal_path为校准文件路径, 每一个设备需要创建/使用一个校准文件 + + # 方式1: 从视频文件读取 + try: + input = int(args.video) + is_camera = True + sensor = orisys.Sensor(input, config_name=args.config, cal_path= args.cal, verbose=args.verbose) + except: + is_camera = False + sensor = orisys.Sensor(args.video, config_name=args.config, cal_path= args.cal, verbose=args.verbose) + + print("\n按 'q' 退出程序,按 'r' 重置传感器追踪器") + + while True: + # 步骤2:信息提取 + # 步骤 2.1: 获取并拼接图像 (必要步骤) + sensor.get_img() + # 步骤 2.2: 计算形变场 (必要步骤) + is_touched = sensor.compute_deformation(check_motion=True, threshold=6) + # 步骤 2.3: 计算触碰点 + if is_touched: + sensor.compute_contact() + + + #步骤 2.4: 读取信息 + fps, flow, vnormal, img, contour, centroid, depth_map, fnormal, fshearx, fsheary = sensor.read_info( + sensor.info.FPS, + sensor.info.VRAW, + sensor.info.VNORMAL, + sensor.info.IMG, + sensor.info.CONTOUR, + sensor.info.CENTROID, + sensor.info.DEPTH, + sensor.info.FNORMAL, + sensor.info.FSHEARX, + sensor.info.FSHEARY + ) + + flow_colormap = flow_to_colormap(vnormal) + cv2.imshow("Optical Flow Colormap", flow_colormap) + + # 步骤 3.2: 在图像上触碰点和区域 + image_with_foe = orisys.util.draw_contact(img, contour, centroid) + cv2.imshow("Contact Centroid", image_with_foe) + + # 步骤 3.3: 绘制深度图 + # 归一化散度值到0-255范围(使用绝对值,然后归一化) + div_abs = depth_map + if div_abs.max() > div_abs.min(): + div_normalized = ((div_abs - div_abs.min()) / (div_abs.max() - div_abs.min()) * 255).astype(np.uint8) + else: + div_normalized = np.zeros_like(div_abs, dtype=np.uint8) + + # 应用颜色映射(使用JET:蓝色=低散度,红色=高散度) + cv2.imshow("Depth Map", cv2.applyColorMap(div_normalized, cv2.COLORMAP_JET)) + + #步骤 3.4: 打印资料 + print(f"fps: {fps}, fnormal: {fnormal}, fshearx: {fshearx}, fsheary: {fsheary}, depth_shape: {depth_map.shape}, sensor.resolution_raw: {sensor.img_size_raw}") + + #步骤 3.5: 键盘控制 + key = cv2.waitKey(1) & 0xFF + if key == ord("q"): + print("\n退出程序...") + break + if key == ord("r"): + # 重置光流追踪器(当追踪漂移时使用) + sensor.reset() + print("传感器已重置") + + # ========================================================================= + # 4. 清理资源 + # ========================================================================= + sensor.disconnect() # 释放视频源和相关资源 + + +if __name__ == '__main__': + main()