【问题标题】:Converting a .binary file into an image将 .binary 文件转换为图像
【发布时间】:2015-03-09 15:48:03
【问题描述】:

我有一个 .binary 文件,其中包含来自 kinect 传感器的深度数据。

我正在尝试浏览 .binary 文件并在 MATLAB 中取回实际图像。所以这是我想出的 MATLAB 程序:

fid = fopen('E:\KinectData\March7Pics\Depth\Depth_Raw_0.binary');
col = 512; %// Change if the dimensions are not proper
row = 424;
frames = {}; %// Empty cell array - Put frames in here
numFrames = 0; %// Let's record the number of frames too
while (true) %// Until we reach the end of the file:
    B = fread(fid, [col row],'ushort=>ushort'); %// Read in one frame at a   time

if (isempty(B)) %// If there are no more frames, get out
    break;
end


 frames{end+1} = B.'; %// Transpose to make row major and place in cell array
numFrames = numFrames + 1; %// Count frame
  imwrite(frames{numFrames},sprintf('Depth_%03d.png',numFrames));
end

%// Close the file    
fclose(fid);
frm = frames{1};
imagesc(frm)
colormap(gray)

上面的程序运行良好,但它不会给我任何高于 99 的图像。 也就是说,我将处理 .binary 文件,而我获得的最后一张图像是 Depth_099.png,即使完整视频的内容不止于此。

有人认识你吗? 谢谢

【问题讨论】:

  • 如果您需要更多数字,为什么不直接使用 %04d 呢? %03d 将文件名限制为 3 位数字并填充 0。
  • 这段代码是否包含在更大的while 循环中,循环遍历帧号?如果可以,您能否发布整个代码?
  • 是的,我已经尝试过了,但不幸的是,提高精度不起作用。不,它不在 while 循环内。

标签: matlab image-processing computer-vision kinect


【解决方案1】:

您没有获得高于 99 的图像的原因是您在读取文件时创建文件名字符串时指定整数的格式。具体来说,这里:

imwrite(frames{numFrames},sprintf('Depth_%03d.png',numFrames));

%03d.png 表示您最多只能指定 3 位精度,因此 999 是您将获得的最大值。如果超过999,那么文件名的字符也会扩大,例如Depth_1000.pngDepth_124141.png。格式化字符串中的%03d 确保您的数字具有三位数的精度,数字左侧的零填充以确保您拥有那么多位数。如果您想为文件名保持相同数量的字符,一种解决方法可能是增加精度位数,例如:

imwrite(frames{numFrames},sprintf('Depth_%05d.png',numFrames));

这样,字符串的长度会更长,按照你的约定,你会得到'Depth_99999.png'。如果超出此范围,则文件名的字符数将相应增加。如果指定%05d,则保证有 5 位精度,相应地对小于 5 位的数字进行零填充。

根据您的视频包含的帧数,相应地调整数量。


但是,鉴于您在下面的 cmets.... 可能只是您只有 99 帧数据 :)... 但我上面提到的精度提示绝对应该有用。

【讨论】:

  • 我承认在这里也很困惑。为什么 3 位整数会在 099 处停止? %03d 不应该给你三个 3 位数字,只在必要时填充零吗? sprintf('Depth_%03d.png',134) 给了我Depth_134.png 的输出。
  • @eigenchris - 是的,我认为 OP 的意思是 999,而不是 099。无论哪种方式,我认为增加精度位数应该会有所帮助!
  • 啊。那会更有意义。 :)
  • 嗨! @rayryeng 感谢您就此事回复我。实际上 Depth_099.png 并没有错。我尝试改变精度,但它仍然不会给我任何高于 99 的结果。
  • 这违背了所有精确的格式化逻辑。例如,执行sprintf('Depth_%03d.png', 999); 会给出Depth_999.png。在每次迭代时显示numFrames。是否超过 99?
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 2014-10-08
  • 2013-08-13
  • 2010-11-22
  • 2011-11-04
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多