Changeset 4583 for trunk/utils


Ignore:
Timestamp:
10/30/08 18:00:28 (12 years ago)
Author:
curtis
Message:

Improved version of bfopen MATLAB script that
returns the entire dataset in a cell array.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/utils/bfopen.m

    r4266 r4583  
    11function [result] = bfopen(id) 
    22 
    3 % A script for opening microscopy images in MATLAB using Bio-Formats. 
    4 % 
    5 % Portions of this code were adapted from: 
    6 % http://www.mathworks.com/support/solutions/data/1-2WPAYR.html?solution=1-2WPAYR 
    7 % 
    8 % Excluding figure creation, this method is ~1.5x-2.5x slower than 
    9 % Bio-Formats's command line showinf tool (MATLAB R14 vs. java 1.6.0_03), 
    10 % due to overhead from reshaping arrays and converting pixel types. 
    11 % 
    12 % Thanks to Ville Rantanen for his performance improvements and ideas. 
    13 % Thanks to Brett Shoelson of The MathWorks for his excellent suggestions. 
    14 % 
    15 % To install, download loci_tools.jar from: 
    16 %   http://www.loci.wisc.edu/ome/formats.html 
    17 % Internet Explorer sometimes erroneously renames the Bio-Formats library 
    18 % to loci_tools.zip. If this happens, rename it back to loci_tools.jar. 
    19 % Place loci_tools.jar and this script (bfopen.m) in your MATLAB work folder. 
     3%{ 
     4A script for opening microscopy images in MATLAB using Bio-Formats. 
    205 
    21 % load Bio-Formats library into MATLAB environment 
     6The function returns a list of image series; i.e., a cell array of cell 
     7arrays of (matrix, label) pairs, with each matrix representing a single 
     8image plane, and each inner list of matrices representing an image 
     9series. See the bottom of this file below for examples of usage. 
     10 
     11Portions of this code were adapted from: 
     12http://www.mathworks.com/support/solutions/data/1-2WPAYR.html?solution=1-2WPAYR 
     13 
     14This method is ~1.5x-2.5x slower than Bio-Formats's command line showinf 
     15tool (MATLAB R14 vs. java 1.6.0_03), due to overhead from reshaping arrays 
     16and converting pixel types. 
     17 
     18Thanks to Ville Rantanen for his performance improvements and ideas. 
     19Thanks to Brett Shoelson of The MathWorks for his excellent suggestions. 
     20Thanks to Martin Offterdinger for his feedback. 
     21 
     22Internet Explorer sometimes erroneously renames the Bio-Formats library 
     23to loci_tools.zip. If this happens, rename it back to loci_tools.jar. 
     24%} 
     25 
     26% load the Bio-Formats library into the MATLAB environment 
    2227javaaddpath('loci_tools.jar'); 
    23 % Alternately, you can add the library to MATLAB's static class path: 
    24 %   1. Type "edit classpath.txt" at the MATLAB prompt. 
    25 %   2. Go to the end of the file, and add the path to your JAR file 
    26 %      (e.g., C:/Program Files/MATLAB/work/loci_tools.jar). 
    27 %   3. Save the file and restart MATLAB. 
     28%{ 
     29Alternately, you can add the library to MATLAB's static class path: 
     30    1. Type "edit classpath.txt" at the MATLAB prompt. 
     31    2. Go to the end of the file, and add the path to your JAR file 
     32       (e.g., C:/Program Files/MATLAB/work/loci_tools.jar). 
     33    3. Save the file and restart MATLAB. 
     34%} 
     35 
     36% to work with Evotec Flex, fill in your license code from LuraWave 
     37%java.lang.System.setProperty('lurawave.license', 'xxxxxx-xxxxxxx'); 
    2838 
    2939r = loci.formats.ChannelFiller(); 
     
    3343r.setId(id); 
    3444numSeries = r.getSeriesCount(); 
     45result = cell(numSeries, 1); 
    3546for s = 1:numSeries 
    3647    fprintf('Reading series #%d', s); 
     
    3849    w = r.getSizeX(); 
    3950    h = r.getSizeY(); 
     51    shape = [w h]; 
    4052    numImages = r.getImageCount(); 
     53    imageList = cell(numImages, 2); 
    4154    for i = 1:numImages 
    4255        fprintf('.'); 
     
    4457        % convert Java BufferedImage to MATLAB image 
    4558        pix = img.getData.getPixels(0, 0, w, h, []); 
    46         arr = reshape(pix, [w h])'; 
     59        arr = reshape(pix, shape)'; 
    4760        % build an informative title for our figure 
    4861        label = id; 
     
    8093            end 
    8194        end 
    82         % plot the image plane in a new figure 
    83         figure('Name', label); 
    84         imagesc(arr); 
    85         % If you have the image processing toolbox, you could use: 
    86         %imshow(arr, []); 
    87         % You could do something else with each plane 
    88         % here, rather than just overwriting the result. 
    89         result = arr; 
     95        % save image plane and label into the list 
     96        imageList{i, 1} = arr; 
     97        imageList{i, 2} = label; 
    9098    end 
     99    % save the whole image list into our master series list 
     100    result{s} = imageList; 
    91101    fprintf('\n'); 
    92102end 
    93 r.close(); 
    94103toc 
     104 
     105%{ 
     106Here are some examples of accessing data using the bfopen function: 
     107 
     108% read the data using Bio-Formats 
     109data = bfopen('C:/data/experiment.lif'); 
     110 
     111% unwrap some specific image planes from the result 
     112numSeries = size(data, 1); 
     113series1 = data{1, 1}; 
     114series2 = data{2, 1}; 
     115series3 = data{3, 1}; 
     116% ...etc. 
     117series1_numPlanes = size(series1, 1); 
     118series1_plane1 = series1{1, 1}; 
     119series1_label1 = series1{1, 2}; 
     120series1_plane2 = series1{2, 1}; 
     121series1_label2 = series1{2, 2}; 
     122series1_plane3 = series1{3, 1}; 
     123series1_label3 = series1{3, 2}; 
     124% ...etc. 
     125 
     126% plot the 1st series's 1st image plane in a new figure 
     127figure('Name', series1_label1); 
     128colormap(gray); 
     129imagesc(series1_plane1); 
     130 
     131% Or if you have the image processing toolbox, you could use: 
     132% imshow(series1_plane1, []); 
     133 
     134% Or animate as a movie (assumes 8-bit unsigned data) 
     135v = linspace(0, 1, 256)'; 
     136cmap = [v v v]; 
     137for p = 1:series1_numPlanes 
     138    M(p) = im2frame(uint8(series1{p, 1}), cmap); 
     139end 
     140movie(M); 
     141%} 
Note: See TracChangeset for help on using the changeset viewer.