文章目录

  • API文档
  • 代码示例:Intel Realsense图像的两种对齐方式
  • 验证猜想
  • 完整示例代码

API文档

def convertScaleAbs(src, dst=None, alpha=None, beta=None): # real signature unknown; restored from __doc__"""convertScaleAbs(src[, dst[, alpha[, beta]]]) -> dst.   @brief Scales, calculates absolute values, and converts the result to 8-bit.缩放,计算绝对值,然后将结果转换为8位。.   .   On each element of the input array, the function convertScaleAbs.   performs three operations sequentially: scaling, taking an absolute.   value, conversion to an unsigned 8-bit type:在输入数组的每个元素上,函数convertScaleAbs依次执行三个操作:缩放,获取绝对值,转换为无符号的8位类型:.   \f[\texttt{dst} (I)= \texttt{saturate\_cast<uchar>} (| \texttt{src} (I)* \texttt{alpha} +  \texttt{beta} |)\f].   In case of multi-channel arrays, the function processes each channel.   independently. When the output is not 8-bit, the operation can be.   emulated by calling the Mat::convertTo method (or by using matrix.   expressions) and then by calculating an absolute value of the result.如果是多通道阵列,该函数将独立处理每个通道。 当输出不是8位时,可以通过调用Mat :: convertTo方法(或使用矩阵表达式),然后通过计算结果的绝对值来模拟该操作。.   For example:.   @code{.cpp}.   Mat_<float> A(30,30);.   randu(A, Scalar(-100), Scalar(100));.   Mat_<float> B = A*5 + 3;.   B = abs(B);.   // Mat_<float> B = abs(A*5+3) will also do the job,.   // but it will allocate a temporary matrix.   @endcode.   @param src input array. 输入数组。.   @param dst output array. 输出数组。.   @param alpha optional scale factor. 可选比例因子。.   @param beta optional delta added to the scaled values. 可选增量添加到缩放值。.   @sa  Mat::convertTo, cv::abs(const Mat&)"""pass

代码示例:Intel Realsense图像的两种对齐方式

cv.convertScaleAbs(depth_image, alpha=0.03)

假设我们需要让我们的深度摄像头感兴趣的距离范围有差别地显示,那么我们就需要确定一个合适的alpha值,公式为:有效距离*alpha=255,假设我们想让深度摄像头8m距离内的深度被显示,>8m的与8m的颜色显示相同,那么alpha=255/(8*10^3)≈0.03,假设我们想让深度摄像头6m距离内的深度被显示,>6m的与6m的颜色显示相同,那么alpha=255/(6*10^3)≈0.0425cv.convertScaleAbs()函数对输入数组进行如下运算:

对于输入深度图数组中的每个值srci(它是16位的,数据类型是uint16),先乘以系数α,再加上偏置β,最后将结果取绝对值,并截取为8位(uint8),然后返回与原数组相同维度的数组dsti。

验证猜想

猜测如果计算后的值大于255,则将其取为255(8位最大值)。

import numpy as np
import cv2 as cv
depth_image=np.array([[1000,2,3],[8000,-8000,6],[-1000,8,9]])
print('depth_image:\n',depth_image)
depth_image_map=cv.convertScaleAbs(depth_image,alpha=0.03,beta=100)
print('depth_image_map:\n',depth_image_map)

结果:

depth_image:[[ 1000     2     3][ 8000 -8000     6][-1000     8     9]]
depth_image_map:[[130 100 100][255 140 100][ 70 100 100]]

验证结果正确!

完整示例代码

import pyrealsense2 as rs
import cv2 as cv
import numpy as nppipeline = rs.pipeline()cfg = rs.config()
cfg.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
cfg.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)# 设定需要对齐的方式(这里是深度对齐彩色,彩色图不变,深度图变换)
align_to = rs.stream.color
# 设定需要对齐的方式(这里是彩色对齐深度,深度图不变,彩色图变换)
# align_to = rs.stream.depthalignedFs = rs.align(align_to)profile = pipeline.start(cfg)try:while True:fs = pipeline.wait_for_frames()aligned_frames = alignedFs.process(fs)color_frame = aligned_frames.get_color_frame()depth_frame = aligned_frames.get_depth_frame()if not depth_frame or not color_frame:continuecolor_image = np.asanyarray(color_frame.get_data())depth_image = np.asanyarray(depth_frame.get_data())# D·C 191122:打印depth_image的最大值看看# print(depth_image.max())# 可以看到,最大的数值从一万到六万不等(表示最远距离十多米到六十米这样)# D·C 191122:打印数据类型看看# print(depth_image.dtype)# uint16# print(color_image.dtype)# uint8# D·C 191122:打印color_image的维度看看# print(color_image.shape)# (480, 640, 3)# print(depth_image.shape)# (480, 640)# D·C 191122:打印cv.convertScaleAbs(depth_image, alpha=0.03)的数据类型和维度看看:# print(cv.convertScaleAbs(depth_image, alpha=0.03).dtype)# uint8# print(cv.convertScaleAbs(depth_image, alpha=0.03).shape)# (480, 640)# D·C 191122:打印cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)的数据类型和维度看看# print(cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET).dtype)# uint8# print(cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET).shape)# (480, 640, 3)# D·C 191122:打印cv.convertScaleAbs(depth_image, alpha=0.03)的最大值看看# print(cv.convertScaleAbs(depth_image, alpha=0.03))# 可看到最大值为255# 估计若原值*alpha大于255,则将其取值为255,而当alpha为0.03时,能够映射的最大可变距离为255/0.03=8500mm=8.5m# D·C 191122:修改alpha参数后,发现图像对比度发生变化(比如alpha=1,图像基本呈红没啥对比度、alpha=0.001,图像呈蓝也没啥对比度、alpha=1点几,效果也不行)# origin:depth_image = cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)depth_image = cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)images = np.hstack((color_image, depth_image))# window = cv.namedWindow('window', cv.WINDOW_AUTOSIZE)cv.imshow('window', images)cv.waitKey(1)
finally:pipeline.stop()

参考文章1:【OpenCV3】cv::convertScaleAbs()使用详解

参考文章2:opencv中图像基础(大小,深度,通道)

参考文章3:图像位深度 8位 16位 24位 32位区别对比 RGB 真彩色 基本概念:(大小,深度,通道)位深度数据类型转换原理 Mat数据读取(opencv里的imread)

python opencv 4.1.0 cv2.convertScaleAbs()函数 (通过线性变换将数据转换成8位[uint8])(用于Intel Realsense D435显示depth图像)相关推荐

  1. python绘制柱状图,如何改变柱状柱间距,如何设置横纵轴标签(绘制Intel Realsense D435深度误差柱状图)

    文章目录 [参考文章1:Python 绘制 柱状图](https://www.cnblogs.com/shenxiaolin/p/11100094.html) 我的代码 参考文章1:Python 绘制 ...

  2. python如何拟合三维平面(拟合Intel Realsense D435深度数据点)

    文章目录 拟合Intel Realsense D435深度数据点 参考文章:[MQ笔记]超简单的最小二乘法拟合平面(Python) import numpy as np import matplotl ...

  3. python opencv cv.applyColorMap()函数(颜色映射)ColormapTypes【将Intel Realsense D435深度图的黑白图映射为彩色图】

    文章目录 API ColormapTypes 完整应用代码[将深度图的黑白图映射为彩色图] map原理 能否map CV_24UC3的? API def applyColorMap(src, colo ...

  4. python Intel Realsense D435 多线程资源分配问题(卡住、卡死)

    在使用python多线程调用Intel Realsense D435多个摄像头时,发现pyrealsense的例如pipeline.start().context.query_devices()函数会 ...

  5. Intel Realsense D435 是否启动pipeline.start()就开始传输帧,还是只有我们调用wait_for_frames()函数时它才会传输帧?(任务管理器USB带宽内存测试)

    我猜想Intel Realsense D435摄像头只要启动pipeline.start()函数,就会开始传输帧,无论我们是否调用wait_for_frames()或者poll_for_frames( ...

  6. Intel Realsense D435运行报错 RuntimeError: Camera not connected! dev.hardware_reset()函数需加睡眠sleep()

    解决方案: 参考:Intel Realsense D435报错 RuntimeError: MFCreateDeviceSource(_device_attrs, &_source) retu ...

  7. Intel Realsense d435 使用python对深度图进行预处理

    Intel Realsense d435 使用python对深度图进行预处理 本文中主要翻译一下一篇关于深度图预处理过滤器的内容,后面还会有关于距离测量的. 原文中的图像显示,是使用matplotli ...

  8. ubutnu16.04下Intel Realsense D435驱动的安装和python环境的配置

    ubutnu16.04下Intel Realsense D435驱动的安装和python环境的配置 一. Intel Realsense D435驱动的安装 普遍操作:这里就复制别人的: 1.Regi ...

  9. Intel Realsense D435 python (Python Wrapper)examples 官方案例汇总

    From pypi/pyrealsense 1.Intel Realsense D435 python (Python Wrapper)example -1: quick start (快速开始) F ...

最新文章

  1. 命令行运行Python脚本时传入参数的三种方式
  2. 想入门图深度学习?这篇55页的教程帮你理清楚了脉络
  3. 进击的Android Hook 注入术《五》
  4. List, Set, Map是否继承自Collection接口?
  5. 台达plc自由口通讯_【台达PLC详解】附PLC功能总简介~
  6. Cisco路由器上传和下载IOS
  7. 比较(0,π/2]上对sinx/x的定积分、对x/sinx的定积分与1的大小关系
  8. 开源究竟差哪了--- 关于开源软件和自由软件的区别
  9. 服务器性能测试 iometer 测试io
  10. java实现webservice调用
  11. 解析:Python就业方向有哪些?
  12. ctf逆向解题——re1
  13. 运维监控之——云原生运维监控报警架构(prometheus+grafana+netdata+Thanos+Alertmanager+Consul)
  14. 我不喜欢代码,却为何坚持做程序员?
  15. 【CV/Matlab系列】基于图像处理的苹果质量检测和分级系统【含Matlab源码】
  16. matlab butter用法,matlab butter c实现
  17. java模拟登录百度_模拟登陆百度的Java实现
  18. 汉字八大基本笔画-东方星书法艺术
  19. icon图标和文字对不齐(已解决)
  20. nubia基于android深度定制的ui,1499双卡八核可定制 nubia Z9 mini上手

热门文章

  1. 分摊、分配、定期重过账
  2. SAP FI常用事务码
  3. 在TABLE CONTROL 输入完一行记录,按回车的时候光标自动移动到下一行
  4. 中报亮眼,阅文的增长“飞轮”是如何练成的?
  5. cf手游服务器维护19年9月19,神雕侠侣2手游9月19日停服维护公告_神雕侠侣2手游9月19日更新了什么_玩游戏网...
  6. vuex 存储刷新_vuex 存储数据 页面刷新不缓存
  7. 饿了么外卖商家版电脑版_为什么有些美团、饿了么外卖商家生意那么好,单子也不少,但是却倒闭了?...
  8. mysql优化说出九条_技术分享 | MySQL 优化:为什么 SQL 走索引还那么慢?
  9. mysql从库同步delete不动了_MySQL主从同步报错故障处理集锦
  10. mysql+提升更新语句效率_MySQL加快批量更新 UPDATE优化