【发布时间】:2017-03-11 05:58:21
【问题描述】:
我首先要警告你,我只是一个试图让事情发挥作用的物理学家,我对 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 来自标题,40000 来自我的 10000 个浮点数),然后我将它重命名为 .bmp 以查看它是否可以读取,但有没办法,我猜可能标题上的定义有些不一致,但我一点头绪都没有。
我也尝试使用 opencv 来读取我的 dat 文件,但是我的机器出了问题,我只能从中得到错误
编辑:1) 这是一个 dat 文件https://ufile.io/86210 我用酰胺打开它们,它们包含模拟生成的投影之一。我猜浮点数对于它现在包含的数字来说可能太多了,但我可能不得不进行更多计数的模拟。
这里是根文件https://ufile.io/a073
编辑:2) 没错,biSize 没有设置,现在 biSize=40 工作图像全是红色和黑色,但看起来应该是这样。该链接将与之前以 /0a111 结尾的链接一样(我需要更多声誉才能发布它)我很抱歉这是一件愚蠢的事情,我已经挣扎了好几天,我失去了注意力
如果有更好的链接文件的方法,请告诉我
最好的问候,
阿桑奇
【问题讨论】:
-
我看到的第一件事是错误的是
bisize没有初始化。 -
我不认为你可以获得32位灰度图像..灰度意味着红色分量等于蓝色和绿色,单个分量只能有256个值
-
您无需编写任何软件就可以做到这一点。能否提供一个示例数据文件?