TIFF to array

First question: Is there an easy way to read in a TIFF file and get the array of values from the TIFF into an array, or better yet, a TH2F?

Second question: is it possible to do this for any particular image in a combined TIFF file?

Basically, I’m faced with analyzing images from a CCD, and I’m just interested in grabbing the data out of TIFFs. Currently, I open up the images in matlab, and dump them to ascii arrays, then read them into TTrees, but that’s cumbersome.

Did you see:
root.cern.ch/root/htmldoc//TImage.html
Does it make sense for you ?

Hi,
TImage/TASimage allows to read TIFF files when libtiff
library is installed on the system. For most linuxes it is so.
After image was read one can convert it to a 2D array of doubles
via root.cern.ch/root/htmldoc//TASIm … e:GetArray.

To convert 2D array to TH2D use copy constructor because TH2D class
is derived from TArray2D.

You can specify a different palette on the input which gives a
correspondence between color and double value.

There are two predefined palettes, see
root.cern.ch/root/htmldoc///TImagePalette.html

Regards. Valeriy

[quote=“jwodin”]Currently, I open up the images in matlab, and dump them to ascii arrays, then read them into TTrees, but that’s cumbersome.[/quote]You can use doc.trolltech.com/3.3/qimage.html
see various “use cases” of using Qt inside of ROOT environment
ftp://root.cern.ch/root/doc/chapter26.pdf and the discussion
lists.bnl.gov/pipermail/qt-root- … 00035.html

I think you can use the ImageMagic program imagemagick.org/script/index.php to split the TIFF file and get the proper format instead.
Normaly the ImageMagic’s “convert” utility can be found pre-installed on Linux. imagemagick.org/script/convert.php
The method doc.trolltech.com/3.3/qmovie.html#frameImage of
doc.trolltech.com/3.3/qmovie.html can read the multiply frame image file “in theory”: [quote]The method QMovie class provides incremental loading of animations or images, signalling as it progresses[/quote].

Valeri,

You are totally confusing people bty showing very complicated solutions that nobody is going to use, while there are simple solutions.
The user simply wants to copy the array of pixels from a TIFF to a TH2 object.
Valeriy Onuchin indicated how to do the job correctly.

Rene

I did not know TImage can be used to read the

I did not see the appropriated method. Sorry for the confusion.

Hi,

I’ve a histogram viewer where I should set the IMG src to a PyROOT cgi program like,

The cgi program should get the TImage from the canvas and send it to the print stream
directly after specifying

Content Type: image/png

How can I do that? I could not manage with TImage::GetArray().

Thanks,

  • Subir

Hi Subir,

  1. to create TImage object from canvas use TImage::FromPad method:
    root.cern.ch/root/htmldoc/TASIma … ge:FromPad

  2. to get PNG compressed in-memory array, use TImage::GetImageBuffer method:
    root.cern.ch/root/htmldoc/TASIma … mageBuffer

Regards. Valeriy

Hi Valeriy,

Thanks a lot. It worked nicely!

Cheers,

  • Subir

[quote=“Valeriy Onuchin”]Hi Subir,

  1. to create TImage object from canvas use TImage::FromPad method:
    root.cern.ch/root/htmldoc/TASIma … ge:FromPad

  2. to get PNG compressed in-memory array, use TImage::GetImageBuffer method:
    root.cern.ch/root/htmldoc/TASIma … mageBuffer

Regards. Valeriy[/quote]

Hi Valeriy,

Now that I’m able to create and send an image of a TCanvas over the network on the fly,
I would like to do the same for postscript/pdf. Any suggestions? Thanks.

  • Subir

[quote=“Valeriy Onuchin”]Hi Subir,

  1. to create TImage object from canvas use TImage::FromPad method:
    root.cern.ch/root/htmldoc/TASIma … ge:FromPad

  2. to get PNG compressed in-memory array, use TImage::GetImageBuffer method:
    root.cern.ch/root/htmldoc/TASIma … mageBuffer

Regards. Valeriy[/quote]

Hi Subir,
you can create a temporary PS/PDF file and then send it over network:

TCanvas *c1;
const char *tmp = gSystem->TempDirecotry();
char *file = ConcatFileName(tmp, "my.ps"); //  pdf, svg or any image format, e.g. "my.png" 
c1->Print(file);
...

I will propose to add possibility to create “in-memory” PostScript buffer.
That will allow to avoid creation of a temporary file.

Regards. Valeriy

Hi Valeriy,

I’m doing exactly that right now, like creating a
temprary file /tmp/appname_PID.pdf, reading it back and
finally unlinking the file. Naturally, I’ll prefer the in-memory
option in future; if possible, for all the formats.

Thanks and regards,

  • Subir

[quote=“Valeriy Onuchin”]Hi Subir,
you can create a temporary PS/PDF file and then send it over network:

TCanvas *c1;
const char *tmp = gSystem->TempDirecotry();
char *file = ConcatFileName(tmp, "my.ps"); //  pdf, svg or any image format, e.g. "my.png" 
c1->Print(file);
...

I will propose to add possibility to create “in-memory” PostScript buffer.
That will allow to avoid creation of a temporary file.

Regards. Valeriy[/quote]

Subir,

Did you manage to get GetImageBuffer(…) working with pyroot? If so, how? I’m doing something similar, but the char ** buffer is causing me problems.

Karol.

Hi Carol,

No I did not. I switched to C++ and was happy.

Cheers,

  • Subir

[quote=“rev_karol”]Subir,

Did you manage to get GetImageBuffer(…) working with pyroot? If so, how? I’m doing something similar, but the char ** buffer is causing me problems.

Karol.[/quote]

I got it working in C++ too. Thanks anyway :slight_smile:

Hi,

just for reference, continuation of the discussion as it relates to python is here: http://root.cern.ch/phpBB2/viewtopic.php?p=18639

Cheers,
Wim

Hi Valeriy,

With Root 5.18.00 I’m having a problem in sending in-memory png buffer
over the network; this occurs when
(1) I divide the canvas into multiple zones
(2) superimpose histograms

The following is what I do in order to get the image buffer. Do I need to
modify the code?

[code]// Now extract the image
// 114 - stands for "no write on Close"
TImageDump imgdump(“tmp.png”, 114);
c1->Paint();

// get an internal image which will be automatically deleted
// in the imgdump destructor
TImage *image = imgdump.GetImage();

char buf;
int sz;
image->GetImageBuffer(&buf, &sz); /
raw buffer */

ostringstream out;
HistogramViewer::addHeader(out, “image/png”);
out << "Content-Length: " << sizeof(char)*sz << endl
<< endl;

for (int i = 0; i < sz; i++)
out << buf[i];
cout << out.str();

delete [] buf;
[/code]

Suchandra has independently found a similar problem with Root 5.18.00a (in CMS) and she has also managed to isolate the problem in a little macro. Please find

/afs/cern.ch/user/d/dutta/public/Monitoring/TestImage.C
/afs/cern.ch/user/d/dutta/public/Monitoring/DQM_SiStrip_R000006215.root

Up until 5.14.00 it worked for both for us.

Thanks and regards,

  • Subir

[quote=“Valeriy Onuchin”]Hi Subir,

  1. to create TImage object from canvas use TImage::FromPad method:
    root.cern.ch/root/htmldoc/TASIma … ge:FromPad

  2. to get PNG compressed in-memory array, use TImage::GetImageBuffer method:
    root.cern.ch/root/htmldoc/TASIma … mageBuffer

Regards. Valeriy[/quote]

I ran this macro on my linux box with the latest ROOT version. It generated two png images which look correct for me. Should I run them on some specific architecture to see the problem ?

Hi,

You should also get image3.png which is created from the ImageBuffer.
The first two png files are saved directly from the canvas at two different points.

Thanks for looking into the problem.

  • Subir

[quote=“couet”][quote]
Suchandra has independently found a similar problem with Root 5.18.00a (in CMS) and she has also managed to isolate the problem in a little macro. Please find

/afs/cern.ch/user/d/dutta/public/Monitoring/TestImage.C
/afs/cern.ch/user/d/dutta/public/Monitoring/DQM_SiStrip_R000006215.root
[/quote]

I ran this macro on my linux box with the latest ROOT version. It generated two png images which look correct for me. Should I run them on some specific architecture to see the problem ?[/quote]

I do not get image3.png … I have never tested this fature of " on the flight" creation. That is new for me. I must investigate … I guess that’s something Valeri did…