Manipulating image file with root

Dear All,
I am trying to do something that I am not sure if it is doable in root but let’s try.
In fact I am doing some measurements by tacking some pictures using a Dino lite microscope.
In the image I have two circles that are suppose to be concentric and this is what I have to check.
I was wondering to fit the two circles so I can have their centers and compare them but this is not something easy to do for me.
I attached the picture here.
I appreciate if someone could help.
Cheers

1 Like

I’d use an image manipulation library, like imagemagick for the edge detection. Then you can use root to fit circles.

Axel

1 Like

There is also this example showing how to convert an image into a TH2D.

1 Like

Dear All,
Thanks for replying.
When I apply Olivier exemple on my picture I have this one attached.


But still don’t know how I can get what I need from this histogram?
I would rather try this option but if it remains complicated I will try Axel suggestion.
Cheers

TH2 has fitting capabilities that’s why I suggested this conversion. Also when converting image to the histogram you can decide to clean up the values you do not really need. And do not fill the histogram with noise.

void img2hist()
{
   TASImage image("circles.png");
   UInt_t yPixels = image.GetHeight();
   UInt_t xPixels = image.GetWidth();
   UInt_t *argb   = image.GetArgbArray();
   TH2D* h = new TH2D("h","circles histogram",xPixels,-1,1,yPixels,-1,1);
   for (int row=0; row<xPixels; ++row) {
      for (int col=0; col<yPixels; ++col) {
         int index = col*xPixels+row;
         float grey = float(argb[index]&0xff)/256;
        // printf("grey = %g\n",grey);
         if (grey>0.35) {
         h->SetBinContent(row+1,yPixels-col,0.35);
         }
      }
   }
   h->Draw("col");
}

More complex cut give better results:

void img2hist()
{
   TASImage image("circles.png");
   UInt_t yPixels = image.GetHeight();
   UInt_t xPixels = image.GetWidth();
   UInt_t *argb   = image.GetArgbArray();
   Int_t i,j,r, ci,cj;


   TH2D* h = new TH2D("h","circles histogram",xPixels,0,xPixels,yPixels,0,yPixels);
   ci = xPixels/2;
   cj = yPixels/2;
   for (int row=0; row<xPixels; ++row) {
      for (int col=0; col<yPixels; ++col) {
         int index = col*xPixels+row;
         float grey = float(argb[index]&0xff)/256;
         if (grey>0.35) {
            i = row+1;
            j = yPixels-col;
            r = sqrt((i-ci)*(i-ci)+(j-cj)*(j-cj));
            if (r>150) {
               if (!((r>160) && (r<220)))
               h->SetBinContent(i,j,0.35);
            }
         }
      }
   }
   h->Draw("cont1");
}

1 Like

You can even materialise the center of the circles:

void img2hist()
{
   TASImage image("circles.png");
   UInt_t yPixels = image.GetHeight();
   UInt_t xPixels = image.GetWidth();
   UInt_t *argb   = image.GetArgbArray();
   Int_t i,j,r, ci,cj;

   gStyle->SetPalette(1);

   TH2D* h = new TH2D("h","circles histogram",xPixels,0,xPixels,yPixels,0,yPixels);
   ci = xPixels/2;
   cj = yPixels/2;
   for (int row=0; row<xPixels; ++row) {
      for (int col=0; col<yPixels; ++col) {
         int index = col*xPixels+row;
         float grey = float(argb[index]&0xff)/256;
         i = row+1;
         j = yPixels-col;

         if (grey>0.35) {
            r = sqrt((i-ci)*(i-ci)+(j-cj)*(j-cj));
            if (r>150) {
               if (!((r>160) && (r<220)))
               h->SetBinContent(i,j,0.35);
            }
            if (j==cj) h->SetBinContent(i,j,2.);
            if (i==ci) h->SetBinContent(i,j,2.);
          }
          if (j==cj) h->SetBinContent(i,j,2.);
          if (i==ci) h->SetBinContent(i,j,2.);
      }
   }
   h->Draw("col");
}

Hi Ollivier,
This is just fantastic!!
Yeah the goal is to fit the big and the small circles and compares their center.
I was saying to put the x and y axis to the same scale otherwise the circles look a little bit an ellipse or maybe this doesn’t affect the fitting.
And also for the fitting I assume I have to extract the values of the big and small circles separately and fit them?
Cheers

I guess you need to make sur your initial pictures well centred . For instance on this one I see there is more space on top than on bottom. I guess you can now go on. Cheers,
Olivier

Hi Olivier
thanks a lot, I will try to do the fit now.
Cheers

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