source: trunk/projects/misc-plugins/src/main/java/loci/plugins/TH_3levels_brush_Ogle.ijm @ 7895

Revision 7895, 4.2 KB checked in by fong, 8 years ago (diff)
RevLine 
[7895]1/*  Threshold and calculate average intensity with Brush Tools
2 *   
3 *      Author: Jimmy Fong (fong@wisc.edu)
4 *     
5 *      Intended for IHC image analysis where the user wants to
6 *      calculate the average intensity per pixel.
7 *     
8 *      Workflow
9 *      1.  Drag in all fluorescent images for analysis.
10 *      2.  Run Macro
11 *      3.  Prompt to drag in any Brightfield images (not analyzed)
12 *      4.  User sets threshold to include only desired pixels
13 *              -Recommended: Choose low level above glass background,
14 *              but below tissue.  Choose high level to be max.
15 *              -NOTE: DON'T click any of the buttons in the threshold window
16 *              Only click OK in the prompt window.
17 *      5.  User draws area to be excluded: pixels not included in
18 *      intensity or area unless later included(if included, will be zero)
19 *      6. User draws are to be included: meant for areas of tissue with
20 *      intensity too low for the threshold, but shouldn't be "background"
21 *      7.  Algorithm calculates the average intensity of the red and
22 *      included areas. 
23 *   
24 *   
25 */
26
27
28//Get ids of only opened fluorescence images
29names = newArray(nImages);
30ids = newArray(nImages);
31for (i=0; i < ids.length; i++){
32        selectImage(i+1);
33        ids[i] = getImageID();
34        names[i] = getTitle();
35}
36
37
38
39numImages = nImages;
40
41/* Prompt user for Brightfield images*/
42
43waitForUser("Drag in any Brightfield Images");
44
45
46//Loop through only fluo images
47for (k = 0; k < numImages; k++){       
48
49        selectImage(ids[k]);
50        run("Threshold...");
51        origID = getImageID();
52        origName = getTitle();
53        run("Select None");
54        getDimensions(width, height, channels, slices, frames);
55        getRawStatistics(nPixels, mean,min,max);
56       
57        /*Prompt to choose threshold*/
58        waitForUser("Move lower/upper limits in Threshold window\nClick OK here when threshold appropriate");
59        getThreshold(lower, upper);
60       
61        /*Exclusion tool - any region selected won't be included */
62
63        setTool("brush");
64        waitForUser("Exclusion Tool: Select area to NOT include\nHold Shift for more selections\nClick ok when done");
65       
66
67
68        if(!(selectionType()== -1)){
69                run("AND...", "value=0");
70        }
71        run("Select None");
72       
73
74        /*Inclusion tool - any region selected will be included, include previously excluded regions*/
75        setTool("brush");       
76        waitForUser("Inclusion Tool:Select area to include\nHold shift for more selections\nClick ok when done");
77
78        selectImage(origID);
79        imgVals = newArray(width*height);
80        numNzero = 0;
81
82        if(!(selectionType()== -1)){    //There is a selection with inclusion tool
83                title = "[Progress]";
84                for (i=0; i < width; i++){
85                        for (j=0; j < height; j++){
86                                currPixel = getPixel(i,j);
87                                if (((currPixel < lower) || (currPixel > upper)) &&  selectionContains(i,j) == 0){
88                                        //Outside range and outside inclusion selection
89                                        setPixel(i,j,0);
90                                        imgVals[i +j*width] = 0;
91                                }else{
92                                        //Within specified range or in my inclusion selection
93                                        imgVals[i+j *width] = currPixel;
94                                        numNzero += 1;
95                                }       
96                                               
97                        }
98
99                //Progress Bar
100                         print("\\Update:"+ i/(width-1)*100 + "%     |" + getBar(i, width-1)+"|");
101
102                }
103        }else{ //no selection with inclusion tool
104                changeValues(0,lower,0);
105                changeValues(upper+1, max,0);
106
107                for (i=0; i < width; i++){
108                        for (j=0; j < height; j++){
109                                imgVals[ i + j*width] = getPixel(i,j);
110                                if (imgVals[i +j*width] !=0)
111                                        numNzero +=1;
112                       
113                        }
114                }
115               
116               
117        }
118        selectImage(origID);
119        close();
120
121        //Calculate average intensity
122        totalIntensity = sumArray(imgVals);
123        intPerPixel = totalIntensity / numNzero;
124
125        currRow = nResults;
126        setResult("Label", currRow, origName);
127        setResult("Intensity/pixel", currRow, intPerPixel);
128        setResult("Total Intensity", currRow, totalIntensity);
129        setResult("NonZero Area (pixels)", currRow, numNzero);
130        setResult("Total Area (pixels)", currRow, nPixels);
131        setResult("Lower Threshold", currRow, lower);
132        setResult("Upper Threshold", currRow , upper);
133        updateResults();
134
135}
136
137
138
139
140//-------------------- Functions -------------------- //
141function sumArray(arr){
142        sum =0;
143        for (i=0;i<arr.length; i++){
144                sum += arr[i];
145        }
146        return sum;
147}
148function getBar(p1, p2) {
149        n = 20;
150        bar1 = "--------------------";
151        bar2 = "********************";
152        index = round(n*(p1/p2));
153        if (index<1) index = 1;
154        if (index>n-1) index = n-1;
155        return substring(bar2, 0, index) + substring(bar1, index+1, n);
156}
157
158
159
160
161
162
163
164
Note: See TracBrowser for help on using the repository browser.