Changeset 4583 for trunk/utils
 Timestamp:
 10/30/08 18:00:28 (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/utils/bfopen.m
r4266 r4583 1 1 function [result] = bfopen(id) 2 2 3 % A script for opening microscopy images in MATLAB using BioFormats. 4 % 5 % Portions of this code were adapted from: 6 % http://www.mathworks.com/support/solutions/data/12WPAYR.html?solution=12WPAYR 7 % 8 % Excluding figure creation, this method is ~1.5x2.5x slower than 9 % BioFormats'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 BioFormats 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 %{ 4 A script for opening microscopy images in MATLAB using BioFormats. 20 5 21 % load BioFormats library into MATLAB environment 6 The function returns a list of image series; i.e., a cell array of cell 7 arrays of (matrix, label) pairs, with each matrix representing a single 8 image plane, and each inner list of matrices representing an image 9 series. See the bottom of this file below for examples of usage. 10 11 Portions of this code were adapted from: 12 http://www.mathworks.com/support/solutions/data/12WPAYR.html?solution=12WPAYR 13 14 This method is ~1.5x2.5x slower than BioFormats's command line showinf 15 tool (MATLAB R14 vs. java 1.6.0_03), due to overhead from reshaping arrays 16 and converting pixel types. 17 18 Thanks to Ville Rantanen for his performance improvements and ideas. 19 Thanks to Brett Shoelson of The MathWorks for his excellent suggestions. 20 Thanks to Martin Offterdinger for his feedback. 21 22 Internet Explorer sometimes erroneously renames the BioFormats library 23 to loci_tools.zip. If this happens, rename it back to loci_tools.jar. 24 %} 25 26 % load the BioFormats library into the MATLAB environment 22 27 javaaddpath('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 %{ 29 Alternately, 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', 'xxxxxxxxxxxxx'); 28 38 29 39 r = loci.formats.ChannelFiller(); … … 33 43 r.setId(id); 34 44 numSeries = r.getSeriesCount(); 45 result = cell(numSeries, 1); 35 46 for s = 1:numSeries 36 47 fprintf('Reading series #%d', s); … … 38 49 w = r.getSizeX(); 39 50 h = r.getSizeY(); 51 shape = [w h]; 40 52 numImages = r.getImageCount(); 53 imageList = cell(numImages, 2); 41 54 for i = 1:numImages 42 55 fprintf('.'); … … 44 57 % convert Java BufferedImage to MATLAB image 45 58 pix = img.getData.getPixels(0, 0, w, h, []); 46 arr = reshape(pix, [w h])';59 arr = reshape(pix, shape)'; 47 60 % build an informative title for our figure 48 61 label = id; … … 80 93 end 81 94 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; 90 98 end 99 % save the whole image list into our master series list 100 result{s} = imageList; 91 101 fprintf('\n'); 92 102 end 93 r.close();94 103 toc 104 105 %{ 106 Here are some examples of accessing data using the bfopen function: 107 108 % read the data using BioFormats 109 data = bfopen('C:/data/experiment.lif'); 110 111 % unwrap some specific image planes from the result 112 numSeries = size(data, 1); 113 series1 = data{1, 1}; 114 series2 = data{2, 1}; 115 series3 = data{3, 1}; 116 % ...etc. 117 series1_numPlanes = size(series1, 1); 118 series1_plane1 = series1{1, 1}; 119 series1_label1 = series1{1, 2}; 120 series1_plane2 = series1{2, 1}; 121 series1_label2 = series1{2, 2}; 122 series1_plane3 = series1{3, 1}; 123 series1_label3 = series1{3, 2}; 124 % ...etc. 125 126 % plot the 1st series's 1st image plane in a new figure 127 figure('Name', series1_label1); 128 colormap(gray); 129 imagesc(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 8bit unsigned data) 135 v = linspace(0, 1, 256)'; 136 cmap = [v v v]; 137 for p = 1:series1_numPlanes 138 M(p) = im2frame(uint8(series1{p, 1}), cmap); 139 end 140 movie(M); 141 %}
Note: See TracChangeset
for help on using the changeset viewer.