together
together

Reputation: 75

Calculate total counts/intensity of different ROIs by DM

Try to calculate the total counts/intensity of different ROIs by DM, but, weirdly, it keeps giving the counts of the left 1st ROI., and I tracked the second ROI by the result. It looks like it gave the whole image. I believe something is wrong, or there is a better method to do this. Any suggestion appreciated enter image description here

ROI roi_2 = NewROI()
ROISetRange( roi_2, 10, 30 )
cropdisp.ImageDisplayAddROI( roi_2)
imagedisplaysetroiselected(cropdisp, roi_2,1)
number count_l, count_r
image img1 := GetFrontImage()
number integral = sum( img1[] )
number scale = img1.ImageGetIntensityScale()
number origin = img1.ImageGetIntensityOrigin()
number t,l,b,r
img1.GetSelection(t,l,b,r)
result("\n select1 is"+t+","+l+","+b+","+r)
number nChannels = r - l
number integral_cal = (integral - origin * nChannels) * scale
count_l= integral_cal
result("\n count_l= "+count_l +", ")
ROI roi_3 = NewROI()
ROISetRange( roi_3, 35, 55 )
cropdisp.ImageDisplayAddROI( roi_3)
imagedisplaysetroiselected(cropdisp, roi_3,1)
showimage(cropped)
image img2 := GetFrontImage()
number integral2 = sum( img2[] )
number scale2 = img2.ImageGetIntensityScale()
number origin2 = img2.ImageGetIntensityOrigin()
t2=0;l2=x0+1;b2=1;r2=xmax+1
img1.GetSelection(t2,l2,b2,r2)
result("\n select is"+t2+","+l2+","+b2+","+r2)
number nChannels2 = r2-l2
number integral_cal2 = (integral2 - origin2 * nChannels2) * scale
count_r= integral_cal2
result("\n count_r= "+count_r +", ")

Upvotes: 0

Views: 56

Answers (1)

BmyGuest
BmyGuest

Reputation: 2949

The code snipped as shown above is incomplete and does not run. I think you wanted to show the following:

image img:=RealImage("Test",4,400)
img=icol
img.ShowImage()

imagedisplay disp = img.ImageGetImageDisplay(0)

ROI r1 = newROI()
r1.RoiSetRange(10,20)
disp.ImageDisplayAddROI(r1)
disp.ImageDisplaySetRoiSelected(r1,1)
Result("\n R1: "+sum(img[]))

ROI r2 = newROI()
r2.RoiSetRange(100,120)
disp.ImageDisplayAddROI(r2)
disp.ImageDisplaySetRoiSelected(r2,1)
Result("\n R2: "+sum(img[]))

disp.ImageDisplaySetRoiSelected(r1,1)
Result("\n R1?: "+sum(img[]))

This code outputs:

 R1: 145
 R2: 79800
 R1?: 79800

And the reason is: The script img[] addresses the first found selected ROI region. If you select more than one ROI, it will always return the sum of the first ROI area only. As adding a ROI to the display adds it "on top" and you are adding R1 then R2 you have the following situation:

  • nothing selected: img[] refers to the full iamge
  • Only r1 selected: img[] refers to the region R1
  • Only r2 selected: img[] refers to the region R2
  • Both r1&r2 selected: img[] refers to the region R2 (because it is "on top")

You could fix this, be ensuring you don't have the first ROI selected anymore:

image img:=RealImage("Test",4,400)
img=icol
img.ShowImage()

imagedisplay disp = img.ImageGetImageDisplay(0)

ROI r1 = newROI()
r1.RoiSetRange(10,20)
disp.ImageDisplayAddROI(r1)
disp.ImageDisplaySetRoiSelected(r1,1)
Result("\n R1: "+sum(img[]))

ROI r2 = newROI()
r2.RoiSetRange(100,120)
disp.ImageDisplayAddROI(r2)
disp.ImageDisplaySetRoiSelected(r1,0)
disp.ImageDisplaySetRoiSelected(r2,1)
Result("\n R2: "+sum(img[]))

disp.ImageDisplaySetRoiSelected(r2,0)
disp.ImageDisplaySetRoiSelected(r1,1)
Result("\n R1?: "+sum(img[]))

However, this is all rather unnecessary complicated code!

Why rely on the "selected" state and a visible ROI in the first place? You can always address an area by the coordinates directly:

image img:=RealImage("Test",4,400)
img=icol
img.ShowImage()

imagedisplay disp = img.ImageGetImageDisplay(0)

ROI r1 = newROI()
r1.RoiSetRange(10,20)
disp.ImageDisplayAddROI(r1)


ROI r2 = newROI()
r2.RoiSetRange(100,120)
disp.ImageDisplayAddROI(r2)

number start,end
r1.ROIGetRange(start,end)
Result("\n R1: "+sum(img[0,start,1,end]))
r2.ROIGetRange(start,end)
Result("\n R2: "+sum(img[0,start,1,end]))

Upvotes: 0

Related Questions