Created
April 23, 2012 22:51
-
-
Save agfor/2474376 to your computer and use it in GitHub Desktop.
A brief snippet to help a non-programmer engineer friend using ImageJ to do some basic time domain statistics on a series of raw image files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.lang.Math; | |
public class Temporal_Noise { | |
public static void main(String[] args) { | |
Temporal_Noise tn = new Temporal_Noise(); | |
tn.run(null); | |
} | |
public void run(String dir_name) { | |
if (dir_name == null) | |
// Path to raw image directory goes here | |
dir_name = ""; | |
// Get a list of raw image files in the source directory | |
File source_dir = new File(dir_name); | |
FilenameFilter filter_raws = new FileListFilter(null, "raw"); | |
String[] files = source_dir.list(filter_raws); | |
if (files == null) { | |
// Either dir does not exist or is not a directory | |
} else { | |
int len = files.length; | |
System.out.println(len); | |
short width = 512, height = 512; | |
int pixel_count = width * height; | |
double[] M = new double[pixel_count], S = new double[pixel_count]; | |
double pixel; | |
// iterate over the files | |
for(int i = 0; i < len; i++) { | |
System.out.println(files[i]); | |
// open the file | |
DataInputStream input; | |
try { | |
input = new DataInputStream(new FileInputStream(dir_name + "\\" + files[i])); | |
} catch(FileNotFoundException e) { | |
System.out.println( | |
"Couldn't open input"); | |
return; | |
} | |
// iterate over the pixels calculating the mean and near-variance | |
for(int p = 0; p < pixel_count; p++) { | |
// read one pixel, big endian | |
try { | |
pixel = (double)input.readUnsignedShort(); | |
} catch(IOException e) { | |
System.out.println( | |
"Couldn't read pixel"); | |
return; | |
} | |
if(i == 0) { | |
M[p] = pixel; | |
continue; | |
} | |
M[i] = recurrence_mean(M[i-1], i+1, pixel); | |
S[i] = recurrence_var(S[i-1], M[i], M[i-1], pixel); | |
} | |
} | |
// iterate over the near-variances and calculate the standard deviations | |
// write the pixels out as 16 bit integers | |
DataOutputStream output; | |
try { | |
output = new DataOutputStream(new FileOutputStream("C:\\Users\\Adam\\Documents\\imagej\\output.raw")); | |
} catch(FileNotFoundException e) { | |
System.out.println("Couldn't open output"); | |
return; | |
} | |
double dlen = (double)len; | |
short s = 0; | |
for(int p = 0; p < pixel_count; p++) { | |
s = (short)Math.sqrt(S[p] / (dlen - 1)); | |
try { | |
output.writeShort(s); | |
} catch(IOException e) { | |
System.out.println("Couldn't read pixel"); | |
return; | |
} | |
} | |
} | |
} | |
// This version of calculating the mean and variance from TAoCP | |
// don't require aggregating the pixel values | |
// and are resistant to floating point errors | |
public double recurrence_mean(double m1, int k, double pixel) { | |
return m1 + (pixel - m1) / (double)k; | |
} | |
public double recurrence_var(double v1, double m, double m1, double pixel) { | |
return v1 + (pixel - m1) * (pixel - m); | |
} | |
// I found this snippet somewhere but don't know where | |
public class FileListFilter implements FilenameFilter { | |
private String name; | |
private String extension; | |
public FileListFilter(String name, String extension) { | |
this.name = name; | |
this.extension = extension; | |
} | |
public boolean accept(File directory, String filename) { | |
boolean fileOK = true; | |
if (name != null) { | |
fileOK &= filename.startsWith(name); | |
} | |
if (extension != null) { | |
fileOK &= filename.endsWith('.' + extension); | |
} | |
return fileOK; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment