Average of first k large values within ROI of a 2D image

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

 

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.

if(checked)
    ReorderImages roiwave,{base}
else
    ReorderImages base,{roiwave}
endif

Andy

alpha.png (24.4 KB) Image set.png (166.45 KB) ROI.png (1.45 MB)

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

 

 

In reply 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

function ImageAnalysis(input,k,i,j,x,y)
    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

 

Screenshot (6).png (114.59 KB)

In reply to 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.

Function ROI_Exclude():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]=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

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:

MatrixOP/O aa=redimension(roiWave*imageWave,numPoints(roiWave),1)
Sort/R aa,aa

It is also straightforward to compute averages using the same method.

AG