Average of first k large values within ROI of a 2D image
TRK
Hi Users,
I wanted to average the first k large values within ROI of a 2D image. The ImageStats returns only the max value. Is there any in-built function or any quick tips for this along the line below? Also now the ROI appears as a separate image and appending it to the original image blocks the original one. Can ROI be made transparent or just the border over the original image? Thank you in advance !
function ImageAnalysis(input)
wave input
Duplicate input myROI
Redimension/B/U myROI
myROI=0
myROI[274,285][259,270]=input
ImageStats/BEAM/M=1/RECT={274,285, 259,270} input
END
wave input
Duplicate input myROI
Redimension/B/U myROI
myROI=0
myROI[274,285][259,270]=input
ImageStats/BEAM/M=1/RECT={274,285, 259,270} input
END
Hi,
A trick I have done to show an ROI is to set the color range. For example the ROI ranges in value from 0 to 1. I then set the range to be 0.1 to 0.9 and set the out of range colors to be transparent by setting the alpha to 0.5.
I also have a panel to show and hide the ROIwave. The trick here is since you can't hide an image like a trace but, you can rearrange the order. To hide it set the image order to first, to show to last.
ReorderImages roiwave,{base}
else
ReorderImages base,{roiwave}
endif
Andy
August 17, 2020 at 06:13 am - Permalink
To find the largest 20 points, you could redimension the ROI wave to be one-dimension then reverse sort and take the first 20 points in the sorted wave. See help for "sort."
The syntax for the reverse sort should be something like:
sort\R my1dWave, my1dWave
August 17, 2020 at 06:17 am - Permalink
In reply to Hi, A trick I have done to… by hegedus
Thank you Andy for the reply. I noticed a different problem while appending the image which I had not seen before. Basically it does not let me to change the color. Please see the attached image for clarification. Is this problem coming from my code? And yes, thanks jtigor for introducing me with 'sort' command
wave input
variable k, i,j,x,y
wave mydata1
Duplicate/O input mydata, myROI
myROI=0
myROI[i,j][x,y]=mydata
Duplicate/O myROI, ROI
Redimension/N=300000 ROI
Sort/R ROI, ROI
Make/D/O/N=1 avg
avg=0
for (k=0;k<5; k+=1)
avg=avg+ROI[k]
endfor
avg=avg/k
//ImageStats/BEAM/M=1/RECT={274,285,259,270} input
End
August 17, 2020 at 07:30 pm - Permalink
In reply to Thank you Andy for the reply… by TRK
Hi,
GenerallyI use an ROIwave that is an unsigned Byte 8 bit wave. My technique assumes a single layer ROIwave. I think what is happening in your duplication step to create the roiwave is it is based on a wave that is 3 (or 4) layers. This is typical in color images especially if you are using png or jpg as an import. Sometimes even seemingly gray scale images are really color images in their data representation. In the experiment I copied my code from, I import an image and then "flatten" it with an image transform operation.
You could also do a redimension on the roiwave.
Also here i the code I use to modify the roiwave using the graph marquee functionality. I simplified version of the builtin one as part of image processing.
wave roiwave
getmarquee/w=graph0,left,top
//Check the bounds
V_Left =Max(0,V_Left)
V_Top=Max(0,V_top)
V_right = Min((dimsize(roiwave,0)-1),V_right)
V_Bottom = Min((dimsize(roiwave,1)-1),V_Bottom)
roiwave[V_left,v_right][V_top,V_bottom]=1
end
Function ROI_Include():graphmarquee
wave roiwave
getmarquee/w=graph0,left,top
//Check the bounds
V_Left =Max(0,V_Left)
V_Top=Max(0,V_top)
V_right = Min((dimsize(roiwave,0)-1),V_right)
V_Bottom = Min((dimsize(roiwave,1)-1),V_Bottom)
roiwave[V_left,v_right][V_top,V_bottom]=0
end
Andy
August 17, 2020 at 08:41 pm - Permalink
In this application it is common to use the ROI wave as a mask where pixels are set to 1 in the ROI and zero outside. In that case you should have the roiWave and the imageWave both having the same dimensions so you can execute:
Sort/R aa,aa
It is also straightforward to compute averages using the same method.
AG
August 18, 2020 at 12:33 pm - Permalink
Thank you AG and Andy. It was helpful.
August 23, 2020 at 12:06 pm - Permalink