importing Text Image files

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

importing Text Image files

Chinn, Steve Dr CIV USA AMC
(1.43h, Win XP Pro, SP3)

The documentation says that Import-> Text Image works with tab-delimited
text files. We have camera output that saves precise numerical data only
in comma-delimited text format, which cannot be loaded into ImageJ
without an intermediate format conversion step using another application
(e.g. Excel, Igor Pro). I could not find an appropriate ImageJ plug-in
on the web site. Does anyone have such a plug-in or simpler solution?
Could this be added to ImageJ as a Text Image option?

 

Stephen R. Chinn, Ph.D.
US Army RDECOM CERDEC
Night Vision and Electronic Sensors Directorate
RDER-NVS-LT
10221 Burbeck Road
Fort Belvoir, VA 22060-5806
(703) 704-3821; FAX: (703) 704-1752
[hidden email] <mailto:[hidden email]>  
 

 
Reply | Threaded
Open this post in threaded view
|

Re: importing Text Image files

Unruh, Jay-2
Stephen,

Below is the source code for a version of text_reader.java that accepts commas instead of tabs.

Jay Unruh
Research Specialist
Stowers Institute for Medical Research

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import java.awt.image.*;
import java.io.*;
import ij.io.*;
import ij.util.Tools;
//here is a csv reader.  This is just a copy of text_reader.java with the tokenizer changed to accept commas as delimiters
//changes made 4/22/08 by Jay Unruh, Research Specialist, Stowers Institute for Medical Research


public class csv_reader_jru_v1 implements PlugIn {
    int words = 0, chars = 0, lines = 0, width=1;;
    String directory, name, path;
    boolean hideErrorMessages;
   
    public void run(String arg) {
        if (showDialog()) {
            IJ.showStatus("Opening: " + path);
            ImageProcessor ip = open(path);
            if (ip!=null)
                new ImagePlus(name, ip).show();
        }
    }
   
    boolean showDialog() {
        OpenDialog od = new OpenDialog("Open Text Image...", null);
        directory = od.getDirectory();
        name = od.getFileName();
        if (name!=null)
            path = directory + name;
        return name!=null;
    }
   
    /** Displays a file open dialog and opens the specified text file as a float image. */
    public ImageProcessor open(){
        if (showDialog())
            return open(path);
        else
            return null;
    }
   
    /** Opens the specified text file as a float image. */
    public ImageProcessor open(String path){
        ImageProcessor ip = null;
        try {
            words = chars = lines = 0;
            Reader r = new BufferedReader(new FileReader(path));
            countLines(r);
            r.close();
            r = new BufferedReader(new FileReader(path));
            //int width = words/lines;
            float[] pixels = new float[width*lines];
            ip = new FloatProcessor(width, lines, pixels, null);
            read(r, width*lines, pixels);
            r.close();
            ip.resetMinAndMax();
        }
        catch (IOException e) {
            String msg = e.getMessage();
            if (msg==null || msg.equals(""))
                msg = ""+e;
            if (!hideErrorMessages)
            IJ.error("TextReader", msg);
            ip = null;
        }
        return ip;
    }
   
    public void hideErrorMessages() {
        hideErrorMessages = true;
    }

    /** Returns the file name. */
    public String getName() {
        return name;
    }

    void countLines(Reader r) throws IOException {
        StreamTokenizer tok = new StreamTokenizer(r);
        int wordsPerLine=0, wordsInPreviousLine=0;

        tok.resetSyntax();
        tok.wordChars(46, 127);
        tok.whitespaceChars(0, 45);
        tok.whitespaceChars(128, 255);
        tok.eolIsSignificant(true);

        while (tok.nextToken() != StreamTokenizer.TT_EOF) {
            switch (tok.ttype) {
                case StreamTokenizer.TT_EOL:
                    lines++;
                    if (wordsPerLine==0)
                        lines--;  // ignore empty lines
                    if (lines==1)
                        width = wordsPerLine;
                    else if (wordsPerLine!=0 && wordsPerLine!=wordsInPreviousLine)
                         throw new IOException("Line "+lines+ " is not the same length as the first line.");
                    if (wordsPerLine!=0)
                        wordsInPreviousLine = wordsPerLine;
                    wordsPerLine = 0;
                    if (lines%20==0 && width>1 && lines<=width)
                        IJ.showProgress(((double)lines/width)/2.0);
                    break;
                case StreamTokenizer.TT_WORD:
                    words++;
                    wordsPerLine++;
                    break;
            }
        }
        if (wordsPerLine==width)
            lines++; // last line does not end with EOL
   }

    void read(Reader r, int size, float[] pixels) throws IOException {
        StreamTokenizer tok = new StreamTokenizer(r);
        tok.resetSyntax();
        //the next two lines were changed by JU to allow for comma delimiters
        tok.wordChars(46, 127);
        tok.whitespaceChars(0, 45);
        tok.whitespaceChars(128, 255);
        //tok.parseNumbers();

        int i = 0;
        int inc = size/20;
        if (inc<1)
            inc = 1;
        while (tok.nextToken() != StreamTokenizer.TT_EOF) {
            if (tok.ttype==StreamTokenizer.TT_WORD) {
                pixels[i++] = (float)Tools.parseDouble(tok.sval, 0.0);
                 if (i==size)
                     break;
                 if (i%inc==0)
                     IJ.showProgress(0.5+((double)i/size)/2.0);
            }
        }
        IJ.showProgress(1.0);
    }

}

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Chinn, Steve Dr CIV USA AMC
Sent: Tuesday, October 13, 2009 1:42 PM
To: [hidden email]
Subject: importing Text Image files

(1.43h, Win XP Pro, SP3)

The documentation says that Import-> Text Image works with tab-delimited text files. We have camera output that saves precise numerical data only in comma-delimited text format, which cannot be loaded into ImageJ without an intermediate format conversion step using another application (e.g. Excel, Igor Pro). I could not find an appropriate ImageJ plug-in on the web site. Does anyone have such a plug-in or simpler solution?
Could this be added to ImageJ as a Text Image option?

 

Stephen R. Chinn, Ph.D.
US Army RDECOM CERDEC
Night Vision and Electronic Sensors Directorate RDER-NVS-LT
10221 Burbeck Road
Fort Belvoir, VA 22060-5806
(703) 704-3821; FAX: (703) 704-1752
[hidden email] <mailto:[hidden email]>  
 

 
Reply | Threaded
Open this post in threaded view
|

Re: importing Text Image files

Gabriel Landini
In reply to this post by Chinn, Steve Dr CIV USA AMC
On Tuesday 13 October 2009 19:42:04 Chinn, Steve Dr CIV USA AMC wrote:
> The documentation says that Import-> Text Image works with tab-delimited
> text files. We have camera output that saves precise numerical data only
> in comma-delimited text format, which cannot be loaded into ImageJ
> without an intermediate format conversion step using another application
> (e.g. Excel, Igor Pro). I could not find an appropriate ImageJ plug-in
> on the web site. Does anyone have such a plug-in or simpler solution?
> Could this be added to ImageJ as a Text Image option?

Maybe you can write a macro that imports all the text file as one string and
then break it into smaller strings with:  split(string, delimiters).
Something like:

text = File.openAsString("");
lines = split(text,",");

so the "lines" array holds all your data. I have not tried this, though, so it
might not work.

Cheers

G.
Reply | Threaded
Open this post in threaded view
|

Re: importing Text Image files

Wayne Rasband
In reply to this post by Chinn, Steve Dr CIV USA AMC
 > (1.43h, Win XP Pro, SP3)
 >
 > The documentation says that Import-> Text Image works with
 > tab-delimited text files. We have camera output that saves
 > precise numerical data only in comma-delimited text format,
 > which cannot be loaded into ImageJ without an intermediate
 > format conversion step using another application (e.g.
 > Excel, Igor Pro). I could not find an appropriate ImageJ
 > plug-in on the web site. Does anyone have such a plug-in or
 > simpler solution? Could this be added to ImageJ as a Text
 > Image option?

Thanks to Jay Unruh, the File>Import>Text Image command in the v1.43i
daily build opens images in comma-delimited text format.

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: importing Text Image files

Russell_Ligon
Hello,
  I have a similar issue, except my camera spits out semicolon (;) delimited
files. I would really like to be able to open them straight-away through
imageJ without pre-processing because I have hundreds of thousands of these
files. Thanks very much in advance!
Russell



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: importing Text Image files

Russell_Ligon
FWIW, I found a solution. Here is Jay Unruh's original modification plus some
tweaks to make it work on a files that are ';' delimited:



import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import java.awt.image.*;
import java.io.*;
import ij.io.*;
import ij.util.Tools;
//here is a csv reader.  This is just a copy of text_reader.java with the
tokenizer changed to accept commas as delimiters
//changes made 4/22/08 by Jay Unruh, Research Specialist, Stowers Institute
for Medical Research

//Addition that allows semicolon (;) separated files to be read, based on
code found here:
//
http://javaprogramming.language-tutorial.com/2012/01/use-streamtokenizer-object-to-parse.html
// by Russell Ligon on Feb 26,2018
//new file is named 'readplease' (clearly containing hints of my
desperation)


public class readplease implements PlugIn {
    int words = 0, chars = 0, lines = 0, width=1;;
    String directory, name, path;
    boolean hideErrorMessages;

    public void run(String arg) {
        if (showDialog()) {
            IJ.showStatus("Opening: " + path);
            ImageProcessor ip = open(path);
            if (ip!=null)
                new ImagePlus(name, ip).show();
        }
    }

    boolean showDialog() {
        OpenDialog od = new OpenDialog("Open Text Image...", null);
        directory = od.getDirectory();
        name = od.getFileName();
        if (name!=null)
            path = directory + name;
        return name!=null;
    }

    /** Displays a file open dialog and opens the specified text file as a
float image. */
    public ImageProcessor open(){
        if (showDialog())
            return open(path);
        else
            return null;
    }

    /** Opens the specified text file as a float image. */
    public ImageProcessor open(String path){
        ImageProcessor ip = null;
        try {
            words = chars = lines = 0;
            Reader r = new BufferedReader(new FileReader(path));
            countLines(r);
            r.close();
            r = new BufferedReader(new FileReader(path));
            //int width = words/lines;
            float[] pixels = new float[width*lines];
            ip = new FloatProcessor(width, lines, pixels, null);
            read(r, width*lines, pixels);
            r.close();
            ip.resetMinAndMax();
        }
        catch (IOException e) {
            String msg = e.getMessage();
            if (msg==null || msg.equals(""))
                msg = ""+e;
            if (!hideErrorMessages)
                IJ.error("TextReader", msg);
            ip = null;
        }
        return ip;
    }

    public void hideErrorMessages() {
        hideErrorMessages = true;
    }

    /** Returns the file name. */
    public String getName() {
        return name;
    }

    void countLines(Reader r) throws IOException {
                char separator = ';'; //defines separator
        StreamTokenizer tok = new StreamTokenizer(r);
        int wordsPerLine=0, wordsInPreviousLine=0;

        tok.resetSyntax();
    //    tok.wordChars(46, 127);
    //    tok.whitespaceChars(0, 45);
    //    tok.whitespaceChars(128, 255);
               
                //NEW LINES TO ENABLE ; DELIMITED TEXT FILES TO BE READ AS IMAGES
                // from:
http://javaprogramming.language-tutorial.com/2012/01/use-streamtokenizer-object-to-parse.html
                tok.wordChars('\u0000', (char)(separator - 1));                 //
Everything is a word character
                tok.wordChars((char)(separator + 1), '\u00ff');                 // except
for the separator
                tok.whitespaceChars('\n', '\n');                                // Make
end-of-line whitespace(and therefore a word delimiter)
                tok.whitespaceChars(separator, separator);                      //
Delimiter separates words
               
               
        tok.eolIsSignificant(true);

        while (tok.nextToken() != StreamTokenizer.TT_EOF) {
            switch (tok.ttype) {
                case StreamTokenizer.TT_EOL:
                    lines++;
                    if (wordsPerLine==0)
                        lines--;  // ignore empty lines
                    if (lines==1)
                        width = wordsPerLine;
                    else if (wordsPerLine!=0 &&
wordsPerLine!=wordsInPreviousLine)
                         throw new IOException("Line "+lines+ " is not the
same length as the first line.");
                    if (wordsPerLine!=0)
                        wordsInPreviousLine = wordsPerLine;
                    wordsPerLine = 0;
                    if (lines%20==0 && width>1 && lines<=width)
                        IJ.showProgress(((double)lines/width)/2.0);
                    break;
                case StreamTokenizer.TT_WORD:
                    words++;
                    wordsPerLine++;
                    break;
            }
        }
        if (wordsPerLine==width)
            lines++; // last line does not end with EOL
   }

    void read(Reader r, int size, float[] pixels) throws IOException {
                char separator = ';'; //defines separator
        StreamTokenizer tok = new StreamTokenizer(r);
        tok.resetSyntax();
               
        //NEW LINES TO ENABLE ; DELIMITED TEXT FILES TO BE READ AS IMAGES
                // from:
http://javaprogramming.language-tutorial.com/2012/01/use-streamtokenizer-object-to-parse.html
                tok.wordChars('\u0000', (char)(separator - 1));                 //
Everything is a word character
                tok.wordChars((char)(separator + 1), '\u00ff');                 // except
for the separator
                tok.whitespaceChars('\n', '\n');                                // Make
end-of-line whitespace(and therefore a word delimiter)
                tok.whitespaceChars(separator, separator);                      //
Delimiter separates words
               
                //the next two lines were changed by JU to allow for comma delimiters
                //   tok.wordChars(46, 127);
                //   tok.whitespaceChars(0, 45);
                //   tok.whitespaceChars(128, 255);
        // tok.parseNumbers();

        int i = 0;
        int inc = size/20;
        if (inc<1)
            inc = 1;
        while (tok.nextToken() != StreamTokenizer.TT_EOF) {
            if (tok.ttype==StreamTokenizer.TT_WORD) {
                pixels[i++] = (float)Tools.parseDouble(tok.sval, 0.0);
                 if (i==size)
                     break;
                 if (i%inc==0)
                     IJ.showProgress(0.5+((double)i/size)/2.0);
            }
        }
        IJ.showProgress(1.0);
    }

}



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html