TSpectrum/TImage problem

I use: ROOT 6.02/08
Generally my plan was to make normal C++ program, but it did not want to work so I made a script.

Dear ROOT Wizards,

What I want to do:
I generate pictures with peaks using tutorial peaks2.C . I convert them into files.png in grey scale (gStyle->SetPalette(52) - I would like to use 16 bit grey scale, but this is another problem). I cut them so I receive something which looks like a photo from a black and white camera (this is my camera simulation). That part works perfectly.

Than I want to read generated pictures, convert them to histograms and find peaks. That part does not work.
This is my code:

#include <all needed libraries>

using namespace std;

void picture(){

	TCanvas *c2 = new TCanvas("c2","c2",600,400);

	TImage * img = TImage::Open("histogram.png");
	if (!img) {
      		printf("Could not create an image... exit\n");
      		return;
   	}

	int height = img -> GetHeight();
	int width = img -> GetWidth();
	UInt_t *argb   = img->GetArgbArray();

	TH2F* h = new TH2F("h","argbArray",width,0,width,height,0,height);

//	gStyle->SetPalette(52);

	for (int row=0; row<width; ++row) {
		for (int col=0; col<height; ++col) {
		int index = col*width+row;
		float grey = float(argb[index]&0xFF)/256; 
		h->SetBinContent(row+1,height-col,grey);
		}
	}

	h->Draw();

	TSpectrum2 *s = new TSpectrum2();

	h->Draw("col");

  	int nfound = s->Search(h,2);
	printf("Found %d candidate peaks\n",nfound);
	s->Print();

   c2->WaitPrimitive();
}

My problem: when the code is like that and I want to draw my histogram I receive completely black histogram and no peaks are found.
When I use: "h->Draw(“col”) - I see peaks on the Picture, but in RGB scale and still no peaks are found.

My question: how can I convert my picture to the histogram to receive correct results in grey scale?
I did almost the same things like in an example peaks2.C . However, as the tutorial works perfectly, my program does not.
I tried to use different colour palettes and simple getarray, but probably I am doing a lot of mistakes that is why it does not work:

TImagePalette pal = img->GetPalette();
TArrayD arg = img -> GetArray(width,height,&pal);
TH2F
h = new TH2F(“h”,“argbArray”,width,0,width,height,0,height);

for (int row=0; row<width; ++row) {
	for (int col=0; col<height; ++col) {
	int index = col*width+row;
	float grey = float(arg[index]&0xFF)/256; //HERE I HAVE CONVERSION PROBLEM WHICH I DO NOT KNOW HOW TO SOLVE
	h->SetBinContent(row+1,height-col,grey);
	}
}

Thank you in advance!

Attached you will find histogram.png which is my test file.

Try:
int nfound = s->Search(h, 10, “noMarkov colz”, 0.1);

Notes:

1, instead of:
h->SetBinContent(row+1,height-col,grey);
you may also use:
h->Fill(row+0.5, height-col-0.5, grey);

  1. when you say:
    gStyle->SetPalette(52);
    you tell ROOT to use a “grey scale” palette with 255 “shades of grey” (if you want only 16, you will need to create a new palette yourself)

  2. if you say:
    h->Draw();
    you tell ROOT to draw a “scatter plot” and, as the majority of your bins are not empty, you get the whole picture black (full of black “scatter plot dots”)

Thank you very very much- finally it worked!

About colours: I want to use 2^16 grey palette, instead of 2^8. I hope I will find one.:slight_smile:

About colours: I want to use 2^16 grey palette, instead of 2^8. I hope I will find one.:)

Create your own as explained here:
root.cern/doc/master/classTColor.html#C05
(2nd example)