Interaction with a TImage

Here is my (simple?) problem:

I have a TImage displayed in a canvas and I need to extract the x and y pixel values of the image after a mouse click.

Right now I am trying to get those pixel coordinates by having an ExecuteEvent() function connected to the ProcessedEvent() of the canvas. This work well to extract the coordinates of the canvas, but I don’t know how to get from there to the actual pixel coordinates of the image inside the canvas.

Could someone show me the best way to do that? Could this also work when the image has been zoomed?



Hi Louis,
pixel value in which form?

  • as display dependent pixel values
  • as argb/rgb values?

You can vectorize an image and then status bar will
display x,y coordinates with pixel’s vector value

Regards. Valeriy

Sorry, I wrote x,y pixel values but I meant x,y pixel coordinates.

When I display my image (1600 x 1200 pixels) in a smaller canvas (800 x 600 pixels) and look a the status bar, I see the x,y coordinates of the canvas, but also the x, y coordinates of the image.

Is there a way for my macro to access those coordinates after a mouse click?



Hi Louis,

You can use the following code in your macro:[code]{

// c1 is the pointer of the canvas containing the image
c1->Connect(“ProcessedEvent(Int_t,Int_t,Int_t,TObject*)”, 0, “”,

void onMouseClick(Int_t event, Int_t x, Int_t y, TObject *selected)
if (event != kButton1Down) return;
TCanvas *c = (TCanvas *) gTQSender;
printf(“Canvas %s: event=%d, x=%d, y=%d, selected=%s\n”, c->GetName(),
event, x, y, selected->IsA()->GetName());
}[/code]Cheers, Ilka

Thanks, it works :smiley:

And to get the pixel coordinates of my image I simply do

myImagePtr->GetObjectInfo(x,y) // where x,y are the canvas coordinates


Hi Louis,

That is correct. If you have different objects drawn in the canvas, you may want to filter them looking only for your image. In this case you may check in onMouseClick code: is the selected object is one you are looking for, or does it inherit TImage.

Cheers, Ilka