Re: Importing ASCII file
Posted by
Vasyl Shynkar on
May 26, 2014; 12:21pm
URL: http://imagej.273.s1.nabble.com/Importing-ASCII-file-tp5007863p5007923.html
Hi Michael,
Thank you a lot for your help.
Vasyl
Le 26/05/14 13:52, Michael Schmid-3 [via ImageJ] a écrit :
Hi Vasyl,
below is a macro that imports a file with x, y, pixel_value in
each line and creates an image from these values. You may modify
it to have two 'v' values, say v1 and v2, and create two images or
a two-image stack.
It assumes integer x and y coordinates. If you have non-integer
steps in x and y, you have to divide x and y by the step size in
the setPixel command (last line).
Beware of possible line breaks caused by the mailer: All lines
between the start of the 'for' loop and the curly braces preceding
the 'if (n==0)' statement should be indented. If a line in this
range is not indented, join it with the previous line.
Michael
________________________________________________________________
// ImageJ Macro
// Open an ascii file with x, y, pixel_value in each line and
convert to an image
// M. Schmid 26-May-2014
path = File.openDialog("Select ascii file with x, y, pixel
value");
str = File.openAsString(path);
lines = split(str,"\n");
xmax = 0; ymax = 0;
x = newArray(lengthOf(lines));
y = newArray(lengthOf(lines));
v = newArray(lengthOf(lines));
n = 0;
print(lengthOf(lines)+" lines read");
for (i=0; i<lengthOf(lines); i++) {
lines[i] = replace(lines[i],"^\\s*", ""); //remove leading
whitespace
if (matches(lines[i],"[0-9\.].*")) { //remove empty lines
& comments
numbers = split(lines[i],"[\t ,;]"); //split the numbers
within a line
if (lengthOf(numbers) < 3) exit("ERROR: Missing data in
line "+i+", only "+lengthOf(numbers)+" values\n"+lines[i]);
x[n] = parseFloat(numbers[0]);
y[n] = parseFloat(numbers[1]);
v[n] = parseFloat(numbers[2]);
if (!isNaN(x[n]) && !isNaN(y[n])) {
if (x[n] > xmax) xmax = x[n];
if (y[n] > ymax) ymax = y[n];
n++;
} else
print("Bad line "+i+":
x,y="+numbers[0]+","+numbers[1]+"\n"+lines[i]);
} //else
//print("skipped line: "+lines[i]);
}
if (n==0) exit ("ERROR: No data found");
if (xmax>100000 || ymax>100000 || xmax*ymax>1e8 ||
xmax<1 || ymax<1)
exit("ERROR: Invalid image dimensions: "+xmax+"x"+ymax);
newImage(File.getName(path), "32-bit", xmax, ymax, 1);
run("Set...", "value=NaN");
for (i=0; i<n; i++)
setPixel(x[i], y[i], v[i]);
________________________________________________________________
On May 22, 2014, at 18:20, Vasyl Shynkar wrote:
> Dear all,
>
> I am doing image acquisition with homemade microscope and I
am writing the
> two piezo-scanner coordinates X and Y in first two columns
and the two
> measured signals into third and fourth columns. Is there a
possibility to
> read this file by ImageJ to get to image with real position
values in µm for
> visualisation purpose? Thank you for your help!
>
> Vasyl
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html