Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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+中读取kinect深度文件+;_C++_File_Computer Vision_Kinect - Fatal编程技术网

C++ 在C+中读取kinect深度文件+;

C++ 在C+中读取kinect深度文件+;,c++,file,computer-vision,kinect,C++,File,Computer Vision,Kinect,要读取帧的深度数据,必须跳过深度文件的前28个字节,其余的是320*240无符号短字符数组,即320*240*2字节(因为每个深度帧有320 x 240像素)。我已经包含了读取深度文件的代码,但是当我试图运行它时,它总是挂起。请让我知道如何更正代码 int main() { int array_size = 76800*2; char *data[array_size]; unsigned short depth[320][240]; int i,j; /

要读取帧的深度数据,必须跳过深度文件的前28个字节,其余的是320*240无符号短字符数组,即320*240*2字节(因为每个深度帧有320 x 240像素)。我已经包含了读取深度文件的代码,但是当我试图运行它时,它总是挂起。请让我知道如何更正代码

int main()
{
    int array_size = 76800*2;
    char *data[array_size];
    unsigned short depth[320][240];
    int i,j;
    // open the depth file in read mode.
    ifstream infile("000000.depth");
    // check for error in opening file
    if(!infile.is_open())
    {
        std::cout<< "File could not be opened";
        return 1;
    }

    std::cout << "Reading from the file" << endl;
    infile.seekg(29,ios::beg); // discarding first 28 bytes

    while(!infile.eof())
    {
        infile >> data[array_size];
    }
    // storing data in required array
    for (i=0; i = 320; i++)
    {
        for (j=0; j=240; j++)
        {
            depth[i][j] = (unsigned short)atof(data[i*j]);
            std::cout << depth[i][j] << endl;
        }
    }

    infile.close();
    getch();
    return(0);
}
intmain()
{
int数组_size=76800*2;
字符*数据[数组大小];
无符号短深度[320][240];
int i,j;
//以读取模式打开深度文件。
如果河流填充(“000000.深度”);
//检查打开文件时是否有错误
如果(!infle.is_open())
{

std::cout我想您的文件可能不是文本文件,所以请尝试以二进制模式打开文件,即更改

 ifstream infile("000000.depth");

并使用
infle.read()
而不是
infle>
读取数据

编辑:

我还有一个问题:将数据读取到数组
data
,看起来像

while(!infile.eof())
{
    infile >> data[array_size];
}
应该读取
char
类型或
char*
(C样式字符串)的元素吗

< p>检查类型<代码>数据<代码>在声明中(我猜它必须是代码> char数据[Alray-Sige];< /Cord>),并考虑从文件读取为

 infile.get (data,array_size);
编辑2:

正如用户3589654和Vincent Fourmond所说,对于
,循环
中的条件是错误的,但转换也是错误的

 depth[i][j] = (unsigned short)atof(data[i*j]);
是问题的潜在来源:

1)
atof
转换为
float
,但不转换为
int
无符号短字符

2) 表达式
数据[i*j]
将从错误的内存部分获取数据,我想您需要
数据[i*240+j]
(或类似的东西)


3) 如果您的文件不是文本文件,其中数字存储为空格分隔的ASCII符号序列,则此类读取和转换方法绝对不合适

我认为问题在于分配
char*数据[array\u size]
,请尝试
char data[array\u size]
因为您分配的是数组大小的指针,而不是内存块。此外,for循环应该是

for(i=0;i<320;i++)
  for(j=0;j<320;j++)
   { 
        //do your stuff here

       }

for(i=0;iMmm),您似乎对
for
循环有问题。查看:

 for (i=0; i = 320; i++)

这意味着“只要
i=320
为真,从0开始运行
i
循环”问题是,<代码> i=320 < /代码>作为一个值,它总是正确的。你想要<代码> i<320 < /C> > P/> P谢谢你的输入。我已经完全改变了我的代码,现在它正在工作。我用C而不是C++来写。< /P> 基本错误出现在前面提到的for循环中。while循环也是一个错误源,没有必要先以char格式读取它,然后将其转换为unsigned short。下面介绍新代码

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;

int main()
{
unsigned short depth[320][240],x;
int i,j;

FILE *infile = fopen("000000.depth","rb");
fseek(infile, 29, SEEK_SET);
printf("Reading data from depth file");

for (i=0; i < 320; i++)
{
    for (j=0; j<240; j++)
{
        fread(&x, sizeof x, 1, infile);
        depth[i][j] = x;
        printf("\n%d %d",depth[i][j],x);
}
}

fclose(infile);
getch();
return(0);
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
无符号短深度[320][240],x;
int i,j;
文件*infle=fopen(“000000.depth”,“rb”);
fseek(infle,29,SEEK_SET);
printf(“从深度文件读取数据”);
对于(i=0;i<320;i++)
{

for(j=0;用
读取而(eof)
是错误的,不以二进制模式打开文件,for循环使用赋值而不是比较,并且您为
char*
分配空间,而不是为
char
分配空间。如果您提供
000000.depth
文件内容的示例,我会很高兴(前60个字节,只是为了查看格式)我还需要切换维度:depth[240][320],iYeah,你是对的。我已经切换了维度。但是你怎么知道它们必须切换?这是存储/读取图像的常用方法:很少看到它以相反的方式存储。因此,你可以说这是一个幸运的猜测:)
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;

int main()
{
unsigned short depth[320][240],x;
int i,j;

FILE *infile = fopen("000000.depth","rb");
fseek(infile, 29, SEEK_SET);
printf("Reading data from depth file");

for (i=0; i < 320; i++)
{
    for (j=0; j<240; j++)
{
        fread(&x, sizeof x, 1, infile);
        depth[i][j] = x;
        printf("\n%d %d",depth[i][j],x);
}
}

fclose(infile);
getch();
return(0);
}