添加BMP灰度标头

时间:2016-10-28 10:44:36

标签: c++ bmp

我首先要警告你,我只是一个试图让事情发挥作用的物理学家,我对c ++的了解基本上是不存在的。

我目前正在使用GATE模拟CT扫描仪,我需要将输出转换为bmp文件。

Gate生成一系列file_xxx.dat,其中xxx的范围从000到您所做的投影数量,每个投影包含一个32位浮点数组,每个像素对应一个像素。

我需要把它们中的每一个都添加一个灰度bmp标题,这样我就可以用另一个程序重建它们了。在此过程中,我希望保留32位精度,这样我就不会丢失像素中的任何信息,因此如果可能的话,它将是32位灰度级。

我一直在处理另一个输出Gate,一个根文件,试图让bmp头工作。我宁愿避免使用根文件,因为它迫使我查看我不需要的大量信息,这会减慢过程。

我已经写了这段代码的一些部分,我已经复制粘贴了其他部分,有些部分是由我的同事完成的,我几乎不知道我在大部分时间都做了什么。

#include <iostream>
#include <fstream>
#include <ostream>
#include <cerrno>
#include <cstdlib>
#include <set>
#include <cmath>
#include "TApplication.h"
#include "TFile.h"
#include "TTree.h"
using namespace std;

#define MAX_ANGLES  180
#define PIXELS_X    100
#define PIXELS_Y    100
#define MAX_PIXELS  10000

struct SDataA
{
  float A[MAX_ANGLES][MAX_PIXELS];

  void Reset()
  {
    for (int a=0; a<MAX_ANGLES; a++)
    {
      for (int p=0; p<MAX_PIXELS; p++) 
      {
        A[a][p] = 0.0;
      }
    }
  }
};

int main( int argc, char* argv[] )
{

if( argc < 2 )
{
    cerr << "arguments missing" << endl;
    cerr << "Usage : AnalyzeCT myFile.root " << endl;
    exit( EXIT_FAILURE );
}

// Store the root file name in 'fileName' variable
char* const FILENAME = argv[ 1 ];

// parameters for the histograms

float a;

SDataA *dataA=NULL; 
dataA = new SDataA;
dataA->Reset();

int maxangles=MAX_ANGLES;
int pixelsx=PIXELS_X;

TApplication app( "Application", &argc, argv );

// Open (check) and read the root file
TFile* file = new TFile( FILENAME );
if( !file->IsOpen() )
{
    cerr << "problem opening the root file : '" << FILENAME << "'" << endl;
    cerr << strerror( errno ) << endl;
    exit( EXIT_FAILURE );
}

// Take the single tree, where is the position, the energy and the runID
TTree* singlesTree = (TTree*)file->Get( "Singles" );
Int_t runID, pixelID;
singlesTree->SetBranchAddress( "runID", &runID );
singlesTree->SetBranchAddress( "pixelID", &pixelID );

// Number of entries in the single tree
Int_t entriesSingleTree = (Int_t)singlesTree->GetEntries();
cout << "Number of detected photons : " << entriesSingleTree << endl;

for( Int_t i = 0; i != entriesSingleTree; ++i )
{
    singlesTree->GetEntry( i );

  if ((runID < MAX_ANGLES) && (pixelID < MAX_PIXELS))
  {
    dataA->A[runID][pixelID] += 1;
    a=dataA->A[runID][pixelID];
   // cout << "A[" << runID <<"]["<< pixelID<<"] ="<< a << endl;
  }

}

std::ofstream ofile("Slice.bin", std::ios::binary);
int currangle=0;
short BM=19778;
short bfReserved=0,biPlanes=1,biBitCount=32;
int bfSize=54+40000, bfOffBits=54, biSize=40, biWidth=PIXELS_X, biHeight=PIXELS_Y,Zero=0,biClrUsed=1;

ofile.write((char*) &BM, sizeof(short));
ofile.write((char*) &bfSize, sizeof(int)); 
ofile.write((char*) &bfReserved, sizeof(short));
ofile.write((char*) &bfReserved, sizeof(short)); 
ofile.write((char*) &bfOffBits, sizeof(int)); 

ofile.write((char*) &biSize, sizeof(int)); 
ofile.write((char*) &biWidth, sizeof(int)); 
ofile.write((char*) &biHeight, sizeof(int)); 
ofile.write((char*) &biPlanes, sizeof(short)); 
ofile.write((char*) &biBitCount, sizeof(short));
ofile.write((char*) &Zero, sizeof(int));
ofile.write((char*) &Zero, sizeof(int));
ofile.write((char*) &Zero, sizeof(int));
ofile.write((char*) &Zero, sizeof(int));
ofile.write((char*) &biClrUsed, sizeof(int));
ofile.write((char*) &Zero, sizeof(int));

for ( Int_t k =currangle ; k<currangle+1; k++){
  for (Int_t j=0; j<MAX_PIXELS; j++){
     a=dataA->A[k][j];
    //cout<< dataA->A[k][j]<< endl;
    ofile.write((char*) &a, sizeof(float)); //s1
  }

}
ofile.close();

cout << "Done" << endl;

delete singlesTree;
delete gateTree;

app.Run();


return 0;
}

可能有一些看起来不完整的块,但是因为我已经删除了原始代码中被注释掉的部分部分,而且它真的是科学怪人的怪物权利现在

事实是,我实际上得到的文件长度为40054字节(标题为54,10000张浮点数为40000),之后我将其重命名为.bmp,看它是否可以请阅读,但没有办法,我猜标题上可能有一些不一致的定义,但我根本没有任何线索。

我还尝试使用opencv来读取我的dat文件,但我的机器搞砸了,所有我可以解决的是错误

编辑:1) 这是一个dat文件https://ufile.io/86210 我用酰胺打开它们,它们包含模拟产生的一个投影。我想浮动对于它现在包含的数字可能太多,但我可能不得不进入具有更多计数的模拟。

以下是根文件https://ufile.io/a073

编辑:2) 没错,biSize没有设置,现在biSize = 40工作图像全是红色和黑色但它看起来应该是什么。这个链接就像之前在/ 0a111的结局一样(我需要更多的声望才能发布)我很抱歉这是一件很蠢的事情,我已经挣扎了几天而且我失去了焦点

如果有更好的方式链接文件,请告诉我

致以最诚挚的问候,

ASanch

2 个答案:

答案 0 :(得分:2)

如果您使用大多数Linux发行版上安装的 ImageMagick 并且可用于macOS和Windows,则无需编写任何软件即可将此数据转换为传统图像格式。

如果我们假设您的10,000个浮点数对应于100x100像素的图像,每个图像都是浮点数,并且位于名为image.bin的文件中,则可以在命令行中键入此值以获得标准化浮点TIF文件:

convert -size 100x100 -depth 32 -define quantum:format=floating-point gray:image.bin -normalize result.tif

enter image description here

  • 如果您不希望规范化的数据填满整个范围,请忽略-normalize

  • 如果您想要PNG个文件而不是TIF,只需将result.tif更改为result.png

  • 如果您的数据是小/大端,请添加-endian lsb-endian msb

我似乎无法下载较大的文件,但如果您要删除54字节的标头,则可以将命令更改为以下内容:

convert -size 100x100+54 -depth 32 -define quantum:format=floating-point gray:image.bin -normalize result.tif

或使用外部实用程序(如dd)来删除54字节标题:

dd if=image.bin bs=54 skip=1 | convert -size 100x100 -depth 32 -define quantum:format=floating-point gray:-  -normalize result.tif

答案 1 :(得分:0)

BMP文件不支持浮点值,并且它们不支持32位灰度像素。使用BMP最接近的是10位灰度。为此,您将使用每像素32位和BI_BITFIELDS压缩,为每个红色,绿色和蓝色通道提供10位(32位模式不支持灰度,因此,您必须复制所有RGB通道的位)。

拉伸文件格式你可以尝试使用BI_BITFIELDS压缩使用所有32位提供相同的红色,绿色和蓝色掩码,这将有效地编码96位RGB图像,但是其他软件可能会拒绝加载这些图像,因为这样违反了RGB通道掩模不应重叠的规范。 (另见https://msdn.microsoft.com/en-us/library/windows/desktop/dd183381(v=vs.85).aspx

BTW:确保使用正确的编码(小/大端),按原样编写int,不能跨平台移植。