EPFL – BIOP Advanced Image Processing, MATLAB 20-22.04.2015 O. Burri, R. Guiet, A. Seitz Image Processing in Matlab Summary 1. Opening Images imopen() Single Files Series Visualization • • • 2. 3. 4. 5. 6. • • • • • • • • • • • Filtering Images Gray Level Filtering Binary Filtering Segmenting Images Color Segmentation Thresholding Active Contours Extracting Statistics MIJ The regionprops() Function Measuring Intensities Based On Mask View, export results Using Fiji in Matlab Some Nice Features Large Image Processing GUIDE 2 The Image Processing Toolbox • Large Improvements each year. • We are using the R2015a version. • Well Documented http://www.mathworks.com/help/images http://www.mathworks.com/help/images/functionlist.html 3 Before Starting • Check your Matlab version >> version ans = 8.5.0.197613 (R2015a) If you have a lower version, you may have to revise some of the code presented. • Copy all the sample data to a local folder 4 Images, Examples & Comments • notepad.cc is a shared notepad, where we will post download links during the session. http://notepad.cc/biopadip password: biop 5 Opening Images The imread() function • Reads an image file into a Matlab Array I = imread('rice.png'); 6 Extensive Documentation 7 Displaying Images The imshow(I) function • Displays the array I as a figure Note that imshow() overwrites the current figure Call figure; before to make a new one. Useful Functions figure, imshow(I) % Shows a new figure of I close all % Closes all figures clear all % Clears all variables in workspace 8 Note: Check the Function Help • Highlight a function and press F1 or Right-click and choose Help on Selection • Most Matlab functions come with examples 9 Displaying Images The imshow(I) function Useful Accompanying Functions title('Some Title') % Sets the title of the figure colormap map % Changes the lookup table (eg. jet) axis on % Shows Axes xlabel('X') % Name of the X axis ylabel('Y') % Name of the Y axis 10 Displaying Images The imshow(I) function I = imread('rice.png'); imshow(I) axis on title('Image of rice.tif') colormap jet colorbar http://tiny.cc/adip-ml-open1 11 What About Stacks? • imread() only opens one image at a time BUT understands that there can be several images in a single TIFF file • Need to use a loop, then sort through the dimensions… • RGB or 3 channel TIFFs can be opened directly % MultiChannel TIFF Stack in Time % Get Information on the image info = imfinfo('mitosis5D.tif'); % Open the whole stack for i=1:length(info) K(:,:,i) = imread('mitosis5D.tif',i); end http://tiny.cc/adip-ml-open2 % Open as 5D Array k=1; I=[]; for fr=1:51 % 51 frames for sl=1:5 % 5 slices for ch=1:2 % 2 channels I(:,:,ch,sl,fr) = ... imread('mitosis5D.tif',k); k=k+1; end end end 12 What About Stacks? Once you have a 3D Array, you can use implay(I) to view it in a movie player. Unfortunately implay does not respond to the colormap, etc.. functions. You must configure the scale and lookup table within the tool manually 13 Alternative: BioFormats • The same library used in Fiji • Opens all sorts of microscopy formats • Can parse metadata • Well Documented http://tiny.cc/adip-ml-bf http://www.openmicroscopy.org/site/support/bio-formats5.1/developers/matlab-dev.html 14 Filtering The basic tool is imfilter(I,h); I is the image and h is the filter, which is created with fspecial(type) Exercise: Load FluorescentCells.tif and filter the DAPI signal 15 Filtering http://tiny.cc/adip-ml-filter 16 Segmenting Three Examples 1. Threshold Based 2. Active Contour Based 3. K-Means Based Exercise: Threshold the Filtered DAPI channel from last exercise with graythresh. We will then try to refine the result using active contours. Useful Functions graythresh im2bw activecontour 17 Segmenting %% Active Contours % First threshold the image roughly mask = im2bw(I, level*0.50); figure, imshow(mask); title('Initial Contour Location'); % Then use active contours on I with the mask image as a starting point for 100 iterations and a smoothing factor of 2. bw = activecontour(I,mask,100,'Chan-Vese',2); figure, imshow(bw); title('Segmented Image with Active Contours'); 18 K-Means Segmentation %% K-Means on Color Images colIm = imread('HESample.jpg'); % Image too big, resize colIm = imresize(colIm, 0.25); % Smoothing colImSmooth = imfilter(colIm,fspecial('gaussian', [3 3], 1.0)); disp(['Applying K-MEANS Clustering with 3 Colors...']); [pixel_labels, centers] = doKMeans(colImSmooth, 3); % Display figure, imshow(colIm,[]), title('Color Image'); figure, imshow(pixel_labels,[]), title('Image labeled by cluster index'); % Make a mask: % Pick dimmest center, this should be the nuclei, as they are dark [mi, id] = min(mean(centers,2)); mask = zeros(size(colIm,1),size(colIm,2)); %Do a watershed segmentation mask(pixel_labels == id) = 1; D = -bwdist(~mask); D(~mask) = -Inf; L = watershed(D); figure, imshow(label2rgb(L,'jet','w')), title('Watershed Segmentation on Nuclei'); http://tiny.cc/adip-ml-segment1 19 Extracting Statistics The regionprops() function Exercise: Segment the 3 Channel DAPI_EDU_H2B.tif image on the first channel and measure Intensities in channels 2 and 3 'Area' 'BoundingBox' 'Centroid' 'ConvexArea' 'ConvexHull' 'ConvexImage' 'Eccentricity' 'EquivDiameter' 'EulerNumber' 'Extent' 'Extrema' 'FilledArea' 'FilledImage' 'Image' 'MajorAxisLength' 'MinorAxisLength' 'Orientation' 'Perimeter' 'PixelIdxList' 'PixelList' 'Solidity' 'SubarrayIdx' 'MaxIntensity' 'MeanIntensity' 'MinIntensity' 'PixelValues' 'WeightedCentroid' 20 Extracting Statistics The regionprops() function % Get the statistics stats = regionprops('table',bw,'Centroid ',... 'Area', 'MajorAxisLength','MinorAxisLeng th', 'Orientation') http://tiny.cc/adip-ml-stats 21 Filtering Statistics Thresholded Cleaned After Filtering Leftovers http://tiny.cc/adip-ml-statfilter 22 MIJI Can call ImageJ/Fiji from within Matlab Just need to include the Path to the 'scripts' folder % Adding path to Fiji addpath('C:\Fiji.app\scripts'); Instead of run(…) you can use MIJ.run('COMMAND', 'ARGUMENTS'); Mind the simple quotes! http://tiny.cc/adip-ml-miji http://fiji.sc/Miji 23 Loads Of Tools • Visualizing Very Large Files rsetwrite() • Block Processing blockproc() http://tiny.cc/adip-ml-large 24 GUIDE • Building GUIs in Matlab http://tiny.cc/adip-ml-guide 25 Finished • Thank you for your time. 26
© Copyright 2024