用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/MIUAV/vibe-coding-ros2 --skill depth-estimation命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
基于 SOC 职业分类
| name | depth-estimation |
| description | OpenCV4 深度估计技能 - 单目深度估计、立体深度估计、ToF、LiDAR 融合、结构光 |
| user-invocable | true |
| argument-hint | 深度估计 OR 深度学习 OR 单目 OR 立体匹配 OR ToF OR LiDAR OR 3D点云 |
深度估计完整指南
当需要以下帮助时使用此技能:
import cv2
import numpy as np
# 加载 MiDaS 模型
model = cv2.dnn.readNet('MiDaS/model.onnx')
# 预处理
def preprocess_midas(img):
original = img.copy()
input_size = (384, 384)
img = cv2.resize(img, input_size)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0
img = (img - np.array([0.485, 0.456, 0.406])) / np.array([0.229, 0.224, 0.225])
img = img.transpose(2, 0, 1)
return img[np.newaxis, :, :, :].astype(np.float32), original
# 推理
def estimate_depth(model, img):
input_tensor, original = preprocess_midas(img)
model.setInput(input_tensor)
depth = model.forward()
depth = cv2.resize(depth[0, 0], (original.shape[1], original.shape[0]))
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
return depth.astype(np.uint8)
# 伪彩色显示
depth_colored = cv2.applyColorMap(cv2.convertScaleAbs(depth, alpha=0.03), cv2.COLORMAP_JET)
import cv2
import numpy as np
# 加载标定参数
data = np.load('stereo_calibration.npz')
mtx_l = data['mtx_l']
mtx_r = data['mtx_r']
R = data['R']
T = data['T']
dist_l = data['dist_l']
dist_r = data['dist_r']
# 立体校正
R1, R2, P1, P2, Q, _, _ = cv2.stereoRectify(
mtx_l, dist_l, mtx_r, dist_r,
img_size, R, T)
# 计算映射
map1_l, map2_l = cv2.initUndistortRectifyMap(mtx_l, dist_l, R1, P1, img_size, cv2.CV_32FC2)
map1_r, map2_r = cv2.initUndistortRectifyMap(mtx_r, dist_r, R2, P2, img_size, cv2.CV_32FC2)
# 校正图像
rect_l = cv2.remap(img_l, map1_l, map2_l, cv2.INTER_LINEAR)
rect_r = cv2.remap(img_r, map1_r, map2_r, cv2.INTER_LINEAR)
# SGBM 立体匹配
stereo = cv2.StereoSGBM_create(
minDisparity=0,
numDisparities=64,
blockSize=9,
uniquenessRatio=10,
speckleWindowSize=100,
speckleRange=32,
disp12MaxDiff=1
)
disparity = stereo.compute(rect_l, rect_r)
# 视差转深度
focal_length = mtx_l[0, 0]
baseline = abs(T[0, 0])
depth = (focal_length * baseline) / (disparity + 1e-6)
import cv2
import numpy as np
from open3d import *
# 从深度图和内参生成点云
def depth_to_pointcloud(depth, intrinsic):
h, w = depth.shape
fx, fy = intrinsic[0, 0], intrinsic[1, 1]
cx, cy = intrinsic[0, 2], intrinsic[1, 2]
points = []
colors = []
for y in range(h):
for x in range(w):
z = depth[y, x] / 1000.0 # mm to m
if z <= 0:
continue
X = (x - cx) * z / fx
Y = (y - cy) * z / fy
points.append([X, y, z])
colors.append([1, 1, 1])
pcd = PointCloud()
pcd.points = Vector3dVector(np.array(points))
pcd.colors = Vector3dVector(np.array(colors) / 255.0)
return pcd
# 使用 OpenCV 的 reprojectImageTo3D
points_3d = cv2.reprojectImageTo3D(disparity, Q)
mask = disparity > disparity.min()
points = points_3d[mask]
import pyrealsense2 as rs
# 初始化 pipeline
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
profile = pipeline.start(config)
# 获取内参
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
intrinsic = depth_frame.profile.as_video_stream_profile().intrinsics
# 深度处理
depth = np.asanyarray(depth_frame.get_data())
color = np.asanyarray(color_frame.get_data())
# 深度滤波
depth_filtered = cv2.bilateralFilter(depth, 5, 30, 30)
# 深度转伪彩色
depth_colored = cv2.applyColorMap(cv2.convertScaleAbs(depth, alpha=0.03), cv2.COLORMAP_JET)
# 稀疏深度补全(使用 RGB 引导)
def fill_depth_holes(depth, rgb):
# 安装 opencv-contrib
# depth = cv2.xphoto.inpaint(depth, mask, cv2.xphoto.INPAINT_SHADOWS)
pass
# 导向滤波深度优化
def guided_depth_filter(depth, rgb, epsilon=0.01):
# 引导滤波
guided_filter = cv2.ximgproc.createGuidedFilter(rgb, 9, epsilon)
depth_filtered = guided_filter.filter(depth.astype(np.float32))
return depth_filtered
#include <opencv2/opencv.hpp>
#include <opencv2/ximgproc.hpp>
// MiDaS 单目深度估计
cv::Mat estimateDepthMidas(cv::dnn::Net& net, const cv::Mat& img) {
cv::Mat input;
cv::dnn::blobFromImage(img, input, 1/255.0, cv::Size(384, 384));
net.setInput(input);
cv::Mat depth = net.forward();
cv::resize(depth, depth, img.size());
cv::normalize(depth, depth, 0, 255, cv::NORM_MINMAX);
return depth;
}
// 双目深度估计
cv::Mat computeDepthStereo(cv::Mat& rect_l, cv::Mat& rect_r,
cv::Mat& mtx_l, cv::Mat& T) {
cv::Ptr<cv::StereoSGBM> stereo = cv::StereoSGBM::create(
0, 64, 9, 8*9*9, 32*9*9, 1, 63, 10, 100, 32);
cv::Mat disparity;
stereo->compute(rect_l, rect_r, disparity);
// 视差转深度
cv::Mat depth;
float f = mtx_l.at<float>(0, 0);
b = (T.<>(, ));
cv::(disparity, depth, f * b / (disparity + ));
depth;
}
单目 vs 双目:
立体匹配优化:
深度滤波:
点云处理:
VoxelGrid 滤波StatisticalOutlierRemovalprojectPoints