Python opencv读取深度图,网格化显示深度

效果图:

代码:

python 复制代码
import cv2
import os

img_path = "./outdir/180m_norm_depth.png"
depth_img = cv2.imread(img_path, cv2.IMREAD_ANYDEPTH)
filename = os.path.basename(img_path)
img_hig, img_wid = depth_img.shape  # (1080, 1920)
print(depth_img.shape)

point_color = (0, 0, 255) # BGR
thickness = 5 
lineType = 4

grid_size = 80

for i in range(img_wid // grid_size):  # 40 为正方向网格的宽
    for j in range(img_hig // grid_size):
        p1 = ((i + 1) * grid_size, (j + 1) * grid_size)
        p2 = (i * grid_size, (j + 1) * grid_size)
        p3 = ((i + 1) * grid_size, j * grid_size)
        cv2.line(depth_img, p1, p2, point_color, thickness, lineType)
        cv2.line(depth_img, p1, p3, point_color, thickness, lineType)
        depth_pos = ((i + 1) * grid_size - grid_size // 2, (j + 1) * grid_size - grid_size // 2)
        depth_value = depth_img[depth_pos[1], depth_pos[0]]
        cv2.putText(depth_img, str(depth_value), (depth_pos[0] - 20, depth_pos[1]), cv2.FONT_HERSHEY_COMPLEX, 0.8, (100, 200, 200), 1)
        # depth_img[i][j] = depth_img[i][j] / 1000
cv2.imwrite(os.path.join("./outdir/", filename[:filename.rfind('.')] + '_grid.png'), depth_img)
相关推荐
明月_清风27 分钟前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风29 分钟前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly15 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling19 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook1 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风1 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风1 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei2 天前
python 抽象基类
python
用户8356290780512 天前
Python 实现 PPT 转 HTML
后端·python
zone77392 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试