Advanced Matlab Tutorial
-
Upload
saukani-halim -
Category
Documents
-
view
42 -
download
6
Transcript of Advanced Matlab Tutorial
Manipulating images with Matlab
Monochrome images in Matlab are represented by and NxM matrix‐N is the height of the image, M is the width
Load an image into variable “J”: J=imread(‘spine.tif’);
Let’s get some information about the image: whos J
Manipulating images with Matlab
Monochrome images in Matlab are represented by and NxM matrix‐N is the height of the image, M is the width
Load an image into variable “J”: J=imread(‘spine.tif’);
Let’s get some information about the image: whos J
‐ Raw data is typically uint but images should be doubles between 0 and 1use function mat2gray to convert the image.
Manipulating images with Matlab
Monochrome images in Matlab are represented by and NxM matrix‐N is the height of the image, M is the width
Load an image into variable “J”: J=imread(‘spine.tif’);
Let’s get some information about the image: whos J
‐ Raw data is typically uint but images should be doubles between 0 and 1use function mat2gray to convert the image.
To view the image use: imtool(J) (alternatively, imshow(J))
Manipulating images with Matlab
Monochrome images in Matlab are represented by and NxM matrix‐N is the height of the image, M is the width
Load an image into variable “J”: J=imread(‘spine.tif’);
Let’s get some information about the image: whos J
‐ Raw data is typically uint but images should be doubles between 0 and 1use function mat2gray to convert the image.
To view the image use: imtool(J) (alternatively, imshow(J))
‐ Try manually set contrast: imtool(J,[lower, upper])‐ Pixels ≤ lower are black; Pixels ≥ upper are white‐ automatically set lower to the minimum pixeland upper to the maximum imtool(J,[ ])‐ You can also adjust the gamma and pixel mappingwith imadjust( J, [],[], gamma);
Filtering images in Matlab
A spatial filter requires a filter mask. Example an averaging filter:
Filtering images in Matlab
A spatial filter requires a filter mask. Example an averaging filter:
Each pixel in the resulting image has a value equal to the sum of the pixel values of the original image x filter mask when the mask is centered on that pixel: imfilter
q=Original Image Filter mask
filt=ones(3,3);filt=filt*1/9;
Filtering images in Matlab
A spatial filter requires a filter mask. Example an averaging filter:
Each pixel in the resulting image has a value equal to the sum of the pixel values of the original image x filter mask when the mask is centered on that pixel: imfilter
Try applying a Gaussian filter to your image (hint: Gaussians are readily made using fspecial)
q=Original Image Filter mask
filt=ones(3,3);filt=filt*1/9;
imfilt(q,filt)
Filtering images in Matlab
A spatial filter requires a filter mask. Example an averaging filter:
Each pixel in the resulting image has a value equal to the sum of the pixel values of the original image x filter mask when the mask is centered on that pixel: imfilter
Try applying a Gaussian filter to your image (hint: Gaussians are readily made using fspecial)
q=Original Image Filter mask
filt=ones(3,3);filt=filt*1/9;
imfilt(q,filt)
J_gaus=imfilt(J,fspecial(‘gaussian’,11,3));
Another example – morphological operations
Read the stock image “rice.png”: rice=mat2gray(imread(‘rice.png’));
View the image, notice the background is not even: imtool(rice,[]);
We will “open” this image, using the function imopen, to removethe small features and get a look at the background.
‐ imopen is a morphological erosion followed by a dilation‐ imclose is the reverse
As an example, load the image “sticksandballs.tif”‐We can use the imopen feature to remove the sticks ‐ Try opening this image and removing the sticks with a disc shaped structural element (strel)
Another example – morphological operations
Read the stock image “rice.png”: rice=mat2gray(imread(‘rice.png’));
View the image, notice the background is not even: imtool(rice,[]);
We will “open” this image, using the function imopen, to removethe small features and get a look at the background.
‐ imopen is a morphological erosion followed by a dilation‐ imclose is the reverse
As an example, load the image “sticksandballs.tif”‐We can use the imopen feature to remove the sticks ‐ Try opening this image and removing the sticks with a disc shaped structural element (strel)
sb_open=imopen(sb,strel(‘disk’,5));
Now let’s return to our unevenly illuminated rice:‐ open the image such that only the background remains
Another example – morphological operations
Read the stock image “rice.png”: rice=mat2gray(imread(‘rice.png’));
View the image, notice the background is not even: imtool(rice,[]);
We will “open” this image, using the function imopen, to removethe small features and get a look at the background.
‐ imopen is a morphological erosion followed by a dilation‐ imclose is the reverse
As an example, load the image “sticksandballs.tif”‐We can use the imopen feature to remove the sticks ‐ Try opening this image and removing the sticks with a disc shaped structural element (strel)
sb_open=imopen(sb,strel(‘disk’,5));
Now let’s return to our unevenly illuminated rice:‐ open the image such that only the background remains
rice_open=imopen(rice,strel(‘disk’,20));
Even rice… yeah baby!
Look at the original image with the uneven background subtracted
Even rice… yeah baby!
Look at the original image with the uneven background subtracted‐ rice_nobg=rice‐rice_open‐ imshow(rice_nobg)
But we aren’t using the entire contrast range now, adjust the image‐ rice_nobg=imadjust(rice_nobg);
Even rice… yeah baby!
Look at the original image with the uneven background subtracted‐ rice_nobg=rice‐rice_open‐ imshow(rice_nobg)
But we aren’t using the entire contrast range now, adjust the image‐ rice_nobg=imadjust(rice_nobg);
Segmentation is a common need in biology image processing; let’s segment the rice using a simple threshold as a criterion for what is rice and what is not.
‐ imhist gives a histogram of the intensity of the image pixels‐ chose a threshold between the background (around 0) and the rice (around 1)‐ Using im2bwmake the pixels below the threshold black and the ones above white
Even rice… yeah baby!
Look at the original image with the uneven background subtracted‐ rice_nobg=rice‐rice_open‐ imshow(rice_nobg)
But we aren’t using the entire contrast range now, adjust the image‐ rice_nobg=imadjust(rice_nobg);
Segmentation is a common need in biology image processing; let’s segment the rice using a simple threshold as a criterion for what is rice and what is not.
‐ imhist gives a histogram of the intensity of the image pixels‐ chose a threshold between the background (around 0) and the rice (around 1)‐ Using im2bwmake the pixels below the threshold black and the ones above white
rice_bw=im2bw(rice_nobg,0.6);
Each connected region of 1’s can be given it’s own label with bwlabel.
Even rice… yeah baby!
Look at the original image with the uneven background subtracted‐ rice_nobg=rice‐rice_open‐ imshow(rice_nobg)
But we aren’t using the entire contrast range now, adjust the image‐ rice_nobg=imadjust(rice_nobg);
Segmentation is a common need in biology image processing; let’s segment the rice using a simple threshold as a criterion for what is rice and what is not.
‐ imhist gives a histogram of the intensity of the image pixels‐ chose a threshold between the background (around 0) and the rice (around 1)‐ Using im2bwmake the pixels below the threshold black and the ones above white
rice_bw=im2bw(rice_nobg,0.6);
Each connected region of 1’s can be given it’s own label with bwlabel.rice_label=bwlabel(rice_bw);‐View the resulting image‐ Find the area and perimeter of each labeled regionwith regionprops
Even rice… yeah baby!
Look at the original image with the uneven background subtracted‐ rice_nobg=rice‐rice_open‐ imshow(rice_nobg)
But we aren’t using the entire contrast range now, adjust the image‐ rice_nobg=imadjust(rice_nobg);
Segmentation is a common need in biology image processing; let’s segment the rice using a simple threshold as a criterion for what is rice and what is not.
‐ imhist gives a histogram of the intensity of the image pixels‐ chose a threshold between the background (around 0) and the rice (around 1)‐ Using im2bwmake the pixels below the threshold black and the ones above white
rice_bw=im2bw(rice_nobg,0.6);
Each connected region of 1’s can be given it’s own label with bwlabel.rice_label=bwlabel(rice_bw);‐View the resulting image‐ Find the area and perimeter of each labeled regionwith regionprops
rice_dims=regionprops(rice_label, ‘Area’, ‘Perimeter’);
Segmented rice
Now we can answer questions like:
What is the mean area of the rice? The variance in the mean? The perimeter?
Segmented rice
Now we can answer questions like:
What is the mean area of the rice? The variance in the mean? The perimeter?
areas=[rice_dims.Area]mean(areas)var(areas)hist(areas)
and on and on
For fun, try segmenting the supplied image “fish.tif”
Point Spread Function
• For an ideal optical system, a point sourceis not a point at the image plane.
• Instead, it appears as a Point Spread Function(PSF) – aka the impulse response function for the optical system.
Put experimental one here
PSF: Why?
PSF: Why?
• The objective cannot capture all of the light radiated by the point source.
• Equivalently: high‐frequency Fourier components are lost.
• Missing components in the Fourier domain correspond to uncertainty in position in physical space.
• How much light is captured? Quantify by Numerical Aperture (NA)…
Numerical Aperture
NA = n ∙ sin(α)n: index of refraction of medium between specimen and objective.Why does NA depend on n?
Putting it together: Resolution
• Rayleigh Criterion: convention for defining resolution
• r = 1.22∙λ/(NAobj+ NAcond)• r ≈ 250 nm for typical
imaging conditions
Fluorescence Microscopy: How are images formed?
• The image detected by your CCD is the convolution of the PSF with the point sources (e.g., fluorescent protein molecules, quantum dots, fluorescent dye molecules) within the field of view.
• We’ll explore this idea in the next few slides.
Space: The Final Frontier
Load provided image of point sources (“points.tif”)
Space: The Final Frontier
Load provided image of point sources (“points.tif”)
P = imread(‘points.tif’);imshow(P)
Experimental PSF
Load provided image of point spread function (“psf.tif”)
psf = imread(‘psf.tif’);imshow(psf)
[Aside: improfile]Use improfile to estimate the minimum resolvable distance according to Rayleigh criterion
[Aside: improfile]Use improfile to estimate the minimum resolvable distance according to Rayleigh criterion (recall this is the distance from central peak to first minimum).
About 7 pixels or so.
Convolving the point sources
Now convolve your image of point sources with the point spread function, using imfilter.(Hint: it’s usually a good idea to convert all your images to doubles before filtering them, so you may want to use im2double…)
Convolving the point sources
Now convolve your image of point sources with the point spread function, using imfilter.(Hint: it’s usually a good idea to convert all your images to doubles before filtering them, so you may want to use im2double…)
PD = im2double(P);psfd = im2double(psf);J = imfilter(PD,psfd);imshow(J,[])
Beating the Limit: Deconvolution
• We’ve seen that even for an ideal optical system, there exist hard limits on performance.
• Is there any way we could cleverly get around these limits?
Beating the Limit: Deconvolution
• We’ve seen that even for an ideal optical system, there exist hard limits on performance.
• Is there any way we could cleverly get around these limits?
• Yes: Deconvolution!• Basic idea: we know what a PSF looks like, so extrapolate backwards
The Math
How many spots?Open “howmany.tif” in the tutorial folder.
How many spots?Open “howmany.tif” in the tutorial folder.
J = imread(‘howmany.tif’);
How many spots?Open “howmany.tif” in the tutorial folder.
J = imread(‘howmany.tif’);
Now, deconvolve the image using deconvwnr, which performs Wiener deconvolutionn.Syntax: deconvwnr(I,psf)
How many spots?Open “howmany.tif” in the tutorial folder.
J = imread(‘howmany.tif’);
Now, deconvolve the image using deconvwnr, which performs Wiener deconvolutionn.Syntax: deconvwnr(I,psf)
JD = deconvwnr(J,psf);
Least‐Squares Fitting
Fitting involves finding the curve which minimizes the sum of the squares of the offsets ("the residuals") of the given data points from the curve:
} offset
x
f(x)
Included in the distributed files is D1Resids.m for fitting a 1D‐Gaussian to data. Let’s try and fit one to a 1D intensity profile of the image “psf.tif”.
First make the intensity profile which you will fit: I chose a horizontal slice through the middle
Least‐Squares Fitting
Fitting involves finding the curve which minimizes the sum of the squares of the offsets ("the residuals") of the given data points from the curve:
} offset
x
f(x)
Included in the distributed files is D1Resids.m for fitting a 1D‐Gaussian to data. Let’s try and fit one to a 1D intensity profile of the image “psf.tif”.
First make the intensity profile which you will fit: I chose a horizontal slice through the middle
Now, try minimizing the residuals with D1Resids.m
psf=mat2gray(imread(‘psf.tif'));psf_profile=psf(18,:);
Least‐Squares Fitting
Fitting involves finding the curve which minimizes the sum of the squares of the offsets ("the residuals") of the given data points from the curve:
} offset
x
f(x)
Included in the distributed files is D1Resids.m for fitting a 1D‐Gaussian to data. Let’s try and fit one to a 1D intensity profile of the image “psf.tif”.
First make the intensity profile which you will fit: I chose a horizontal slice through the middle
Now, try minimizing the residuals with D1Resids.mfitp = fminsearch(@(x) D1Resids(x,inputdata),[17,3,.04,.01])
Overlay your fitted function with psf.tif
psf=mat2gray(imread(‘psf.tif'));psf_profile=psf(18,:);
Least‐Squares Fitting
Fitting involves finding the curve which minimizes the sum of the squares of the offsets ("the residuals") of the given data points from the curve:
} offset
x
f(x)
Included in the distributed files is D1Resids.m for fitting a 1D‐Gaussian to data. Let’s try and fit one to a 1D intensity profile of the image “psf.tif”.
First make the intensity profile which you will fit: I chose a horizontal slice through the middle
Now, try minimizing the residuals with D1Resids.mfitp = fminsearch(@(x) D1Resids(x,inputdata),[17,3,.04,.01])
Overlay your fitted function with psf.tifx=[1:35]y=fitp(3)*exp(‐(x‐fitp(1)).^2/fitp(2))+fitp(4)plot(psf_profile,'k'); hold; plot(x,y,'r')
psf=mat2gray(imread(‘psf.tif'));psf_profile=psf(18,:);
Fitting in 2D
You should now have the proper tools to fit a 2D Gaussian to “psf.tif”
Give it a try!
Can you localize the quantum dot in “psf.tif” to sub pixel resolutiohn?