Is there an easy way to read in an image (JPEG, GIF, BMP, whatever…) and get the actual pixel information out in an array or histogram? For example, if I have a 400x300 grayscale BMP, can I read it in and output the pixel grayscale data into an array or TH2F (I’d like to do this so I can do some processing and fitting on the image data).
First convert the image to ascii format. I used Imagemagick, but you could also use GIMP or some other free tool. Then write a script to read in the image data to your histogram. Imagemagick produces the following ascii format:
row,column: Red, Green, Blue, Hex Color
So I wrote:
while(1){
in.getline(tempx,16,’,’);
x = atoi(tempx);
in.getline(tempy,16,’:’);
y = atoi(tempy);
in.getline(tempz,16,’,’);
z = atoi(tempz);
in.getline(trash,256);
if (!in.good()) break;
dat->Fill(x,y,z);
tree->Fill();
nlines++;
}
where dat and tree were previously set up as a TH2F histogram and a TTree.
[quote=“Valeriy Onuchin”]Hi Valera,
not TImage, but TASImage - yes.
Regards. Balepa[/quote]
It seems to me I am completely lost with the current design.
I thought the TImage class is an abstract interface
(see:http://root.cern.ch/root/htmldoc/TImage.html) the user works with and TASImage is that interface implementation loaded with ROOT plugin.
Is it still correct?
Anyway the TH2 class dependency of the “image” class is wierd.
Hi Valeri,
as I said TImage doesn’t depend on TH2.
Both TH2 related methods are not abstract.
It’s up to you to implement them or not.
BTW, TImage is a part of libGraph library
which already depends on libHist (say TH2).
[quote=“Valeriy Onuchin”]Hi Valeri,
… yes …
there is some descripency in dependency of TImage class.
We will try to find better solution. Do you have ideas?
Thanks. Regards. Valeriy[/quote]
I did not understand the question.
Idea about what? How to save the TPad in the “pixel” format?
Hi Valeri,
the idea is about "how to organize all in the better way"
It looks like that TImage class must be in the “base” …
not in the graf. Recently I made wrong addons making it
dependent on TGPicture and TH2. For a moment TH2
dependency could be forgiven because graf itself depends on TH2.
What is your opinion how to organaize it in the best way?
Hmm, first you did something odd then you are asking me about the “best way”. I have no such luxury. All my ROOT contributiond had to be approved first by many “commitee”.
Anyway, What is wrong you find with the solution I have posted several times (including this thread):
I mean TVirtualX had provided a generic interface (even several ones) to create the file for any TPad. Why are you introducing another interface to manage 2D histograms only?
What about the implementation of TVirtualX, there you are free to provide any solution for x11/win32gdk etc. Qt layer aready did it. If you think this is wrong tell me to fix.
Hi,
Actually it is the answer on the original question: is there an easy way to read for example BMP image into the array.
The below function do the job. This function is in principle a part of class, we use for storage and processing images. It could be posted if have general interst. Of cause root is not the best tool for image processing, but sometimes it could be convenient for the small samples.
VT
//____________________________________________________________________________________
Int_t EdbImage::LoadBMP( char file )
{
/
Loads a BMP format file… /
typedef struct {
unsigned short int type; / Magic identifier /
unsigned int size; / File size in bytes /
unsigned short int reserved1, reserved2;
unsigned int offset; / Offset to image data, bytes */
} HEADER;
typedef struct {
unsigned int size; /* Header size in bytes /
int width,height; / Width and height of image /
unsigned short int planes; / Number of colour planes /
unsigned short int bits; / Bits per pixel /
unsigned int compression; / Compression type /
unsigned int imagesize; / Image size in bytes /
int xresolution,yresolution; / Pixels per meter /
unsigned int ncolours; / Number of colours /
unsigned int importantcolours; / Important colours */
} INFOHEADER;
HEADER hdr;
INFOHEADER ihdr;
//printf(“hdr: %d ihdr: %dn”,sizeof(hdr), sizeof(ihdr));
char* ptr;
fstream raw(file,ios::in | ios::binary);
ptr=(char*)&hdr;
raw.read(ptr,14);
ptr=(char*)&ihdr;
raw.read(ptr,40);
if(ihdr.bits!=8) { printf(“Error: %d bits/pixel bitmap is not supported!n”,ihdr.bits); return -1;}
int N=ihdr.widthihdr.height;
char byte= new char[N];
raw.read(byte,1024);
raw.read(byte,N);
raw.close();
eBytes=N;
eColumns=ihdr.width;
eRows=ihdr.height;
eColors=256;
eBuffer->Adopt(eBytes,byte);
printf(“EdbImage::LoadBMP: file %s is readed…Image %d x %d pixels.n”, file,eColumns,eRows);
Hi,
I’m actively working on TImage/TASImage right now.
I have to finish with it before Christmass release.
That will provide requested functionality, i.e.
image -> array, array -> image etc. ++ “gifbatch” and
much more.