Skip to content

Instantly share code, notes, and snippets.

@romainGuiet
Last active November 16, 2023 09:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainGuiet/3dc23527406205b36c41b1baf41240cc to your computer and use it in GitHub Desktop.
Save romainGuiet/3dc23527406205b36c41b1baf41240cc to your computer and use it in GitHub Desktop.
An ImageJ macro to determine False Positive threshold on control images. #BIOP #FIJI #Macro #Coloc
#@File() image_path
#@Integer() ch_idx
table_name = "FP_thresholds";
open(image_path);
img_name = getTitle();
print(img_name);
run("Duplicate...", "title=ch"+ch_idx+" duplicate channels="+ch_idx);
getDimensions(width, height, channels, slices, frames);
if ( bitDepth() == 8){
greyValue = 256 ;
} else if ( bitDepth() == 16){
greyValue = 65536 ;
} else {
return;
}
total_pixelNbr = width*height*slices ;
print ("Total Pixel Number : "+total_pixelNbr);
run("Clear Results");
if (slices >1){
Stack.setSlice( 1 );
getHistogram(values, counts, greyValue);
for (slc = 2; slc <= slices; slc++) {
Stack.setSlice( slc );
getHistogram(values, counts_v, greyValue);
for (i = 0; i < counts.length; i++) {
counts[i] += counts_v[i];
}
}
}else{
getHistogram(values, counts, greyValue);
}
// sum content of each bin
bins_sum = 0;
for ( bin_i = counts.length-1; bin_i >= 0; bin_i--) {
bins_sum += counts[bin_i];
setResult("Bin", nResults, bin_i);
setResult("Count", nResults-1, counts[bin_i]);
setResult("Count (Sum)", nResults-1, bins_sum);
}
bins_sum_array = Table.getColumn("Count (Sum)");
bins_fromTop_array = Table.getColumn("Bin");
// Find value for : False Positive ZERO
idx_FPZero = findThresholdValue(bins_sum_array, 0);
print( "False Positive ZERO : "+bins_fromTop_array[idx_FPZero-1]);// idy is the one who is above threshold, take idx-1
// Find value for : False Positive Tolerance
tol1 = 0.1 ;
tol2 = 0.01 ;
tol3 = 0.001;
pixelCount_tol1 = round (width*height*slices*tol1 /100);
pixelCount_tol2 = round (width*height*slices*tol2 /100);
pixelCount_tol3 = round (width*height*slices*tol3 /100);
idx_FPTol1 = findThresholdValue(bins_sum_array, pixelCount_tol1 );
idx_FPTol2 = findThresholdValue(bins_sum_array, pixelCount_tol2 );
idx_FPTol3 = findThresholdValue(bins_sum_array, pixelCount_tol3 );
if (isOpen(table_name)){
selectWindow(table_name);
}else {
Table.create(table_name);
}
row = Table.size;
if (row < 0 ) row =0;
row = Table.size;
Table.set("Image name" , row, img_name);
Table.set("Channel" , row, ch_idx);
Table.set("False Positive - %" , row, tol1);
Table.set("False Positive - Pixel count" , row, pixelCount_tol1);
Table.set( "Threshold value " , row, bins_fromTop_array[idx_FPTol1-1]);
row = Table.size;
Table.set("Image name" , row, img_name);
Table.set("Channel" , row, ch_idx);
Table.set("False Positive - %" , row, tol2);
Table.set("False Positive - Pixel count" , row, pixelCount_tol2);
Table.set( "Threshold value " , row, bins_fromTop_array[idx_FPTol2-1]);
row = Table.size;
Table.set("Image name" , row, img_name);
Table.set("Channel" , row, ch_idx);
Table.set("False Positive - %" , row, tol3);
Table.set("False Positive - Pixel count" , row, pixelCount_tol3);
Table.set( "Threshold value " , row, bins_fromTop_array[idx_FPTol3-1]);
row = Table.size;
Table.set("Image name" , row, img_name);
Table.set("Channel" , row, ch_idx);
Table.set("False Positive - %" , row, 0);
Table.set("False Positive - Pixel count" , row, 0);
Table.set( "Threshold value " , row, bins_fromTop_array[idx_FPZero-1] );
run("Close All");
function findThresholdValue(array, threshold){
continueSearch = true ;
idx = 0 ;
while ( continueSearch ){
if ( bins_sum_array[idx] > threshold ) continueSearch = false ;
else idx++;
}
return idx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment