添加 BMP 灰度 header
Adding a BMP grayscale header
首先我想警告你,我只是一个试图让事情发挥作用的物理学家,我对 c++ 的了解基本上是 non-existent。
我目前正在使用 GATE 模拟 CT 扫描仪,我需要将输出转换为 bmp 文件。
Gate 制作了一系列 file_xxx.dat,其中 xxx 的范围从 000 到您所做的投影数量,并且每个都包含一个 32 位浮点数组,一个对应于您的检测器具有的每个像素。
我需要对它们中的每一个进行拍摄并添加一个灰度 bmp header 这样我就可以用另一个程序重建它们。在此过程中,我想保留 32 位精度,这样我就不会丢失像素中的任何信息,因此如果可能的话,它将是 32 位灰度。
我一直在使用 Gate 的另一个输出,一个根文件,试图让 bmp header 工作。我宁愿避免使用根文件,因为它会迫使我浏览大量不需要的信息,并且会减慢处理速度。
我写了这段代码的一些部分,我有 copy-pasted 其他部分,有些部分是由我 co-workers 完成的,大部分时间我几乎不知道自己在做什么。
#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 字节长的文件(header 中有 54 个字节,我的 10000 个浮点数中有 40000 个字节),之后我将其重命名为 .bmp 以查看是否可以读但是没办法,估计header上可能有一些不一致的定义,但是我一点头绪都没有。
我也尝试过使用 opencv 读取我的 dat 文件,但是我的机器出了问题,我只能排除错误
编辑:1)
这是一个 dat 文件 https://ufile.io/86210
我用酰胺打开它们,它们包含模拟生成的投影之一。我想一个浮点数对于它现在包含的数字来说可能太多了,但我可能不得不进入具有更高计数的模拟。
编辑:2)
是的,biSize 没有设置,现在 biSize=40 有效图像全是红色和黑色,但看起来应该是这样。 link 会像前面的 /0a111 结尾(我需要更多声望才能 post 它)我很抱歉这是一个愚蠢的事情,我已经苦苦挣扎了好几天而且我失去了注意力
如果有任何更好的方法来 linking 文件,请告诉我
此致,
阿桑奇
BMP 文件不支持浮点值,也不支持 32 位灰度像素。使用 BMP 可以获得的最接近的是 10 位灰度。为此,您将使用每像素 32 位和 BI_BITFIELDS 压缩,为每个红色、绿色和蓝色通道提供 10 位(32 位模式不支持灰度,因此,您必须为所有 RGB 通道复制这些位).
拉伸文件格式,您可以尝试使用所有 32 位使用 BI_BITFIELDS 压缩提供相同的红色、绿色和蓝色掩码,这将有效地编码 96 位 RGB 图像,但其他软件可能会拒绝加载这些图像,因为这违反了 RGB 通道的掩码不应重叠的规范。
(另请参阅 https://msdn.microsoft.com/en-us/library/windows/desktop/dd183381(v=vs.85).aspx)
顺便说一句:确保您使用的是正确的编码(little/big 字节序),按原样编写 int 不可跨平台移植。
如果你使用 ImageMagick,你可以将这些数据转换成传统的图像格式,而无需编写任何软件,它安装在大多数 Linux 发行版上,并且适用于 macOS 和Windows.
如果我们假设您的 10,000 个浮点数对应于一个 100x100 像素的图像,每个像素都是一个浮点数,并且在一个名为 image.bin
的文件中,您可以在命令行中键入它来获取规范化浮点 TIF 文件:
convert -size 100x100 -depth 32 -define quantum:format=floating-point gray:image.bin -normalize result.tif
如果您不希望标准化的数据填满整个范围,请省略 -normalize
。
如果您想要 PNG
文件而不是 TIF
,只需将 result.tif
更改为 result.png
如果您的数据是 little/big 字节序,请添加 -endian lsb
或 -endian msb
。
我似乎无法下载你的大文件,但如果你有一个 54 字节 header 要删除,你可以将命令更改为如下内容:
convert -size 100x100+54 -depth 32 -define quantum:format=floating-point gray:image.bin -normalize result.tif
或使用外部实用程序,例如 dd
删除 54 字节 header:
dd if=image.bin bs=54 skip=1 | convert -size 100x100 -depth 32 -define quantum:format=floating-point gray:- -normalize result.tif
首先我想警告你,我只是一个试图让事情发挥作用的物理学家,我对 c++ 的了解基本上是 non-existent。
我目前正在使用 GATE 模拟 CT 扫描仪,我需要将输出转换为 bmp 文件。
Gate 制作了一系列 file_xxx.dat,其中 xxx 的范围从 000 到您所做的投影数量,并且每个都包含一个 32 位浮点数组,一个对应于您的检测器具有的每个像素。
我需要对它们中的每一个进行拍摄并添加一个灰度 bmp header 这样我就可以用另一个程序重建它们。在此过程中,我想保留 32 位精度,这样我就不会丢失像素中的任何信息,因此如果可能的话,它将是 32 位灰度。
我一直在使用 Gate 的另一个输出,一个根文件,试图让 bmp header 工作。我宁愿避免使用根文件,因为它会迫使我浏览大量不需要的信息,并且会减慢处理速度。
我写了这段代码的一些部分,我有 copy-pasted 其他部分,有些部分是由我 co-workers 完成的,大部分时间我几乎不知道自己在做什么。
#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 字节长的文件(header 中有 54 个字节,我的 10000 个浮点数中有 40000 个字节),之后我将其重命名为 .bmp 以查看是否可以读但是没办法,估计header上可能有一些不一致的定义,但是我一点头绪都没有。
我也尝试过使用 opencv 读取我的 dat 文件,但是我的机器出了问题,我只能排除错误
编辑:1) 这是一个 dat 文件 https://ufile.io/86210 我用酰胺打开它们,它们包含模拟生成的投影之一。我想一个浮点数对于它现在包含的数字来说可能太多了,但我可能不得不进入具有更高计数的模拟。
编辑:2) 是的,biSize 没有设置,现在 biSize=40 有效图像全是红色和黑色,但看起来应该是这样。 link 会像前面的 /0a111 结尾(我需要更多声望才能 post 它)我很抱歉这是一个愚蠢的事情,我已经苦苦挣扎了好几天而且我失去了注意力
如果有任何更好的方法来 linking 文件,请告诉我
此致,
阿桑奇
BMP 文件不支持浮点值,也不支持 32 位灰度像素。使用 BMP 可以获得的最接近的是 10 位灰度。为此,您将使用每像素 32 位和 BI_BITFIELDS 压缩,为每个红色、绿色和蓝色通道提供 10 位(32 位模式不支持灰度,因此,您必须为所有 RGB 通道复制这些位).
拉伸文件格式,您可以尝试使用所有 32 位使用 BI_BITFIELDS 压缩提供相同的红色、绿色和蓝色掩码,这将有效地编码 96 位 RGB 图像,但其他软件可能会拒绝加载这些图像,因为这违反了 RGB 通道的掩码不应重叠的规范。 (另请参阅 https://msdn.microsoft.com/en-us/library/windows/desktop/dd183381(v=vs.85).aspx)
顺便说一句:确保您使用的是正确的编码(little/big 字节序),按原样编写 int 不可跨平台移植。
如果你使用 ImageMagick,你可以将这些数据转换成传统的图像格式,而无需编写任何软件,它安装在大多数 Linux 发行版上,并且适用于 macOS 和Windows.
如果我们假设您的 10,000 个浮点数对应于一个 100x100 像素的图像,每个像素都是一个浮点数,并且在一个名为 image.bin
的文件中,您可以在命令行中键入它来获取规范化浮点 TIF 文件:
convert -size 100x100 -depth 32 -define quantum:format=floating-point gray:image.bin -normalize result.tif
如果您不希望标准化的数据填满整个范围,请省略
-normalize
。如果您想要
PNG
文件而不是TIF
,只需将result.tif
更改为result.png
如果您的数据是 little/big 字节序,请添加
-endian lsb
或-endian msb
。
我似乎无法下载你的大文件,但如果你有一个 54 字节 header 要删除,你可以将命令更改为如下内容:
convert -size 100x100+54 -depth 32 -define quantum:format=floating-point gray:image.bin -normalize result.tif
或使用外部实用程序,例如 dd
删除 54 字节 header:
dd if=image.bin bs=54 skip=1 | convert -size 100x100 -depth 32 -define quantum:format=floating-point gray:- -normalize result.tif