本文介绍了在C ++中的图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中打开一个3.5 MB的文件,读入一个unsigned char数组我初始化的图像数据如下:

I am opening a 3.5 MB file in C reading it into an unsigned char array Image data which I have initialized as follows:

unsigned char *** Imagedata;
Imagedata = new unsigned char**[1278];
for(int i=0; i<1278; i++){
    Imagedata[i]= new unsigned char*[968];
    for(int j=0; j<968; j++){
        Imagedata[i][j]= new unsigned char[3];
    }
}



现在我打开文件并将其读入数组as:

Now i open the file and read it into the array as:

ifstream ifile;
ifile.open("abcde.raw", ios::in|ios::binary);
for(int i=0; i<1278; i++){
    for(int j=0; j<968; j++){
        for(int k=0; k<3; k++){
            ifile>>Imagedata[i][j][k];
        }
    }
}
ifile.close();

下一步是将字节重写为一个新文件..我试图实现它像这样:

The next step is to just rewrite the bytes into a new file.. which i call rawfile.. I have tried to achieve it like this:

ofstream myfile;
myfile.open("rawfile.raw", ios::out|ios::binary);
for(int i=0; i<1278; i++){
    for(int j=0; j<968; j++){
        myfile.write((char *)Imagedata[i][j],3*sizeof(unsigned char));
    }
}
myfile.close();

它不知怎的似乎工作..我得到的图像文件是垃圾..什么可以问题?

It somehow doesn seem to work.. the image file that i get is garbage.. what could be the problem?

推荐答案

ifile>>Imagedata[i][j][k];

这是格式化的输入,即使你指定了 ios :: binary 。使用

This is formatted input, it eats 'whitespace' characters even though you specified ios::binary. Use

Imagedata[i][j][k] = ifile.get();

更好的做法是,为整个文件分配一个大块内存, $ c>读调用。你现在做的是为每个像素分配一个指针,这是非常浪费,特别是对于64位系统。

Even better, allocate one big chunk of memory for the whole file and read it by one read call. What you do now is allocating a pointer for each pixel, which is very wasteful especially for 64-bit systems.

这篇关于在C ++中的图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:54