advanced matlab tutorial

44
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

Upload: saukani-halim

Post on 02-Dec-2014

56 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: 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

Page 2: 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

‐ Raw data is typically uint but images should be doubles between 0 and 1use function mat2gray to convert the image.

Page 3: 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

‐ 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))

Page 4: 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

‐ 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);

Page 5: Advanced Matlab Tutorial

Filtering images in Matlab

A spatial filter requires a filter mask. Example an averaging filter:

Page 6: Advanced Matlab Tutorial

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;

Page 7: Advanced Matlab Tutorial

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)

Page 8: Advanced Matlab Tutorial

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));

Page 9: Advanced Matlab Tutorial

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)

Page 10: Advanced Matlab Tutorial

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

Page 11: Advanced Matlab Tutorial

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));

Page 12: Advanced Matlab Tutorial

Even rice… yeah baby!

Look at the original image with the uneven background subtracted

Page 13: Advanced Matlab Tutorial

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);

Page 14: Advanced Matlab Tutorial

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

Page 15: Advanced Matlab Tutorial

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.

Page 16: Advanced Matlab Tutorial

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

Page 17: Advanced Matlab Tutorial

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’);

Page 18: Advanced Matlab Tutorial

Segmented rice

Now we can answer questions like:

What is the mean area of the rice? The variance in the mean? The perimeter?

Page 19: Advanced Matlab Tutorial

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”

Page 20: Advanced Matlab Tutorial

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

Page 21: Advanced Matlab Tutorial

PSF: Why?

Page 22: Advanced Matlab Tutorial

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)… 

Page 23: Advanced Matlab Tutorial

Numerical Aperture

NA = n ∙ sin(α)n: index of refraction of medium between specimen and objective.Why does NA depend on n?

Page 24: Advanced Matlab Tutorial

Putting it together: Resolution

• Rayleigh Criterion: convention for defining resolution

• r = 1.22∙λ/(NAobj+ NAcond)• r ≈ 250 nm for typical 

imaging conditions

Page 25: Advanced Matlab Tutorial

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.

Page 26: Advanced Matlab Tutorial

Space: The Final Frontier

Load provided image of point sources (“points.tif”)

Page 27: Advanced Matlab Tutorial

Space: The Final Frontier

Load provided image of point sources (“points.tif”)

P = imread(‘points.tif’);imshow(P)

Page 28: Advanced Matlab Tutorial

Experimental PSF

Load provided image of point spread function (“psf.tif”)

psf = imread(‘psf.tif’);imshow(psf)

Page 29: Advanced Matlab Tutorial

[Aside: improfile]Use improfile to estimate the minimum resolvable distance according to Rayleigh criterion

Page 30: Advanced Matlab Tutorial

[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.

Page 31: Advanced Matlab Tutorial

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…)

Page 32: Advanced Matlab Tutorial

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,[])

Page 33: Advanced Matlab Tutorial

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?

Page 34: Advanced Matlab Tutorial

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

Page 35: Advanced Matlab Tutorial

The Math

Page 36: Advanced Matlab Tutorial

How many spots?Open “howmany.tif” in  the tutorial folder.

Page 37: Advanced Matlab Tutorial

How many spots?Open “howmany.tif” in  the tutorial folder.

J = imread(‘howmany.tif’);

Page 38: Advanced Matlab Tutorial

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)

Page 39: Advanced Matlab Tutorial

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);

Page 40: Advanced Matlab Tutorial

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

Page 41: Advanced Matlab Tutorial

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,:);

Page 42: Advanced Matlab Tutorial

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,:);

Page 43: Advanced Matlab Tutorial

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,:);

Page 44: Advanced Matlab Tutorial

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?