【问题标题】:Adding a BMP grayscale header添加 BMP 灰度标头
【发布时间】: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个值
  • 您无需编写任何软件就可以做到这一点。能否提供一个示例数据文件?

标签: c++ bmp


【解决方案1】:

如果您使用 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

  • 如果您的数据是小/大端,请添加-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

【讨论】:

    【解决方案2】:

    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

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

    【讨论】:

    • 还有可以接受这样的图像格式吗?
    猜你喜欢
    • 1970-01-01
    • 2012-06-03
    • 2023-04-07
    • 2011-07-18
    • 2013-05-05
    • 2020-03-21
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多