Trying to read bin content of 12 bits grayscale image

Dear all,

I have a grayscale image in 12 bits and would like to print out each pixel content.
Could anyone tell me how to do it, please?

Thanks,
regards,
Francesca

ROOT has a TImage class for reading images, you can look at the last tutorial on this site https://root.cern.ch/doc/v608/group__tutorial__image.html (the trans_graph.C example). It shows how to access individual pixels from ROOT. If you like python, the PIL library can do that as well and independently of ROOT.

Hello.

Thanks. I had tried, but got something different from what I should have got because I do not understand how to get 12 bits bin contents.

I explain better: from an experiment I got both a *.txt file and a *.png file.
The *.txt file is far bigger than the png and contains the same info as the png in principle.
So, I am trying to see if, when reading the png pixel content I can get an exact copy of the txt file.
This way I can avoid filling my laptop with enormous txt and directly work on the images.

In attachment are my png file and my routine to plot the png as a TH2D.
If I print out the pixels content I get the file test.txt which starts as
row 1 yPixels-col 1028 index 1542
row 1 yPixels-col 1027 index 0
row 1 yPixels-col 1026 index 0
row 1 yPixels-col 1025 index 0
row 1 yPixels-col 1024 index 771
row 1 yPixels-col 1023 index 257
row 1 yPixels-col 1022 index 771
row 1 yPixels-col 1021 index 514
row 1 yPixels-col 1020 index 0

But the head of the *.txt file from the experiment instead looks like

     89     0    30    65    34    37    75    32   126   107   267 
     132   120    95    58   129   124    85   207   239   137   140 
     174   181   125   150   159   152   101   164   186   189   276 
      98   308   279   195   222   197   229   184   239   111   184 
     178   287   109   206   289   190   232   204   221   180   245 
     214   245   288   154   186   310   292   122   260   192   239 
     207   280   344   224   175   255   380   244   262   276   241 
     228   262   251   216   198   270   309   214   344   261   194 

Could anyone please tell me how to get the same out of the png image, please?

Thanks,
regards,
FranciuskaReplot.C (4.3 KB)

I think one problem is here in your bitmask:

float grey = float(argb[index]&0xfff);///256*4096;

That takes the last one and a half bytes of a pixel, but if you only want one channel it should only one byte, 0xff. I can’t test it myself, because the forum decided to change your png into a jpeg, and that loses data.

Hello.

I tried, but now I get
row 1 yPixels-col 1028 index 6
row 1 yPixels-col 1027 index 0
row 1 yPixels-col 1026 index 0
row 1 yPixels-col 1025 index 0
row 1 yPixels-col 1024 index 3
row 1 yPixels-col 1023 index 1
row 1 yPixels-col 1022 index 3
row 1 yPixels-col 1021 index 2
row 1 yPixels-col 1020 index 0
row 1 yPixels-col 1019 index 2
row 1 yPixels-col 1018 index 1
row 1 yPixels-col 1017 index 0
row 1 yPixels-col 1016 index 1
row 1 yPixels-col 1015 index 0
row 1 yPixels-col 1014 index 0
row 1 yPixels-col 1013 index 0
row 1 yPixels-col 1012 index 0

I had tried with different number of “f” in &0xfff, and divided these &0f*** by powers of 2 and also multiplied by powers of 2. But I never got the numbers in the *.txt file.
I have uploaded the *.png here https://drive.google.com/file/d/18a2EZIggHhKHAGHIN9G3Th9bI9MnsDTy/view?usp=sharing

Thanks,
regards,
Francesca

Hi, so I’m not 100% sure, but I think you can’t get the full information from the png: It’s a “PNG image data, 1213 x 1017, 8-bit grayscale, non-interlaced”, so 8-bit per pixel, whereas your data seems to be 12 bit originally. If I compare the start of the file I get from ROOT (p[i] & 0xff), I get

[7, 0, 2, 4, 2, 2, 5, 2, 8, 8, 18]

compared to the first line of the txt
89 0 30 65 34 37 75 32 126 107 267
It looks like 12 bit data mapped into 8 bit data (~factor of 16), and you can’t get the full resolution from the png.

Sorry.

Hello.

Thanks a lot. I thought it was 12 bits.
Could you please tell me how can I check, as you did, that the PNG is 8-bit gray scale so that next time I do not make the same mistake, please?

Thanks,
Francesca

I used the linux command ‘file’, that gives me this output:

>file image.png 
image.png: PNG image data, 1213 x 1017, 8-bit grayscale, non-interlaced

Thanks a lot!

And could you please tell me how you get [7, 0, 2, 4, 2, 2, 5, 2, 8, 8, 18]?

When I print argb[index]&0xff, I get [6 0 0 0 3 1 3 2 0 2 …]

Thanks A LOT for your help,
Francesca

   for (int row=0; row<xPixels; ++row) {
      for (int col=0; col<yPixels; ++col) {
         int index = col*xPixels+row;

That code goes through the pixels numbered 0, xPixels, 2 * xPixels, …, 1, xPixels + 1, 2 * xPixels +1, …,
while I went through 0,1,2,3… But I’m not sure why your first number is 6 instead of 7.

Hello.

Thanks again!

Cheers,
Francesca

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.