Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;_C++_Opencv_Bitwise Operators_Bit Shift - Fatal编程技术网

C++ C++;

C++ C++;,c++,opencv,bitwise-operators,bit-shift,C++,Opencv,Bitwise Operators,Bit Shift,我试图理解一种读取和转换存储为16位png文件的深度数据的方法 首先,他们将文件加载到类型为CV_16UC1 cv::Mat depth_image = cv::imread(filename.c_str(), CV_LOAD_IMAGE_ANYDEPTH); 然后他们分配 unsigned short * depth_raw = new unsigned short[frame_height * frame_width]; for (int i = 0; i < frame_heigh

我试图理解一种读取和转换存储为16位png文件的深度数据的方法

首先,他们将文件加载到类型为
CV_16UC1

cv::Mat depth_image = cv::imread(filename.c_str(), CV_LOAD_IMAGE_ANYDEPTH);
然后他们分配

unsigned short * depth_raw = new unsigned short[frame_height * frame_width];
for (int i = 0; i < frame_height * frame_width; ++i) {
  depth_raw[i] = ((((unsigned short)depth_image.data[i * 2 + 1]) << 8) + ((unsigned short)depth_image.data[i * 2 + 0]));
  depth_raw[i] = (depth_raw[i] << 13 | depth_raw[i] >> 3);
  depth_data[i] = float((float)depth_raw[i] / 1000.0f);
}
真正奇怪的是最后一行:从
无符号短
浮点
的转换似乎是将数字191转换为1528

任何帮助或暗示都将不胜感激

编辑:
我发现了一些Matlab代码,显示了作者以前是如何保存深度图像的:

% resave depth map with bit shifting
depthRaw = double(imread(filename))/1000;
saveDepth (depthRaw,newFilename);

function saveDepth (depth,filename)
    depth(isnan(depth)) =0;
    depth =single(depth)*1000;
    depthVis = uint16(depth);
    depthVis = bitor(bitshift(depthVis,3), bitshift(depthVis,3-16));
    imwrite(depthVis,filename);
end
所以这看起来像是一个奇怪的保存

编辑2:
作者回复:

“深度图以移位3位的方式保存,以使PNG格式的深度更符合人眼。因此,我们需要在读取文件时将其移回原处。”

数据的存储方式没有通用规范。因此,可能有必要从小端到大端或相反的方式进行转换。要了解endianess,请查看以下内容:

depth_raw[i]=(((无符号短)depth_image.data[i*2+1])3;
深度_数据[i]=浮点((浮点)深度_原始[i]/1000.0f);

在endianess转换后,数据必须被解释。确保作者在这里所做的唯一方法是查看深度图的文档。第一行将3个最低有效位移到前面,将其他位移到下面。我不知道为什么要这样做。我认为之后除以1000就是o仅用于纠正单位(可能是mm中的m或m中的km),或某种固定点语义(整数数据类型中的有理数表示)。

感谢您的有力、充分的回答。我已经联系了作者,但至今没有回复。我将在找到答案后发布解决方案。。。
% resave depth map with bit shifting
depthRaw = double(imread(filename))/1000;
saveDepth (depthRaw,newFilename);

function saveDepth (depth,filename)
    depth(isnan(depth)) =0;
    depth =single(depth)*1000;
    depthVis = uint16(depth);
    depthVis = bitor(bitshift(depthVis,3), bitshift(depthVis,3-16));
    imwrite(depthVis,filename);
end
depth_raw[i] = ((((unsigned short)depth_image.data[i * 2 + 1]) << 8) + ((unsigned short)depth_image.data[i * 2 + 0]));
depth_raw[i] = (depth_raw[i] << 13 | depth_raw[i] >> 3);
depth_data[i] = float((float)depth_raw[i] / 1000.0f);