Difference between revisions of "Assignment 4 part 1: Make a fake image"

From Course Wiki
Jump to: navigation, search
(Modeling optical resolution)
(Modeling optical resolution)
Line 64: Line 64:
  
 
     firstBesselZero = 3.8317;
 
     firstBesselZero = 3.8317;
     normalizedRadius = firstBesselZero .* sqrt( xCoordinate.^2 + yCoordinate.^2 ) ./ airyDiskRadiusInPixels;
+
     normalizedRadius = firstBesselZero .* hypot( xCoordinate, yCoordinate) ./ airyDiskRadiusInPixels;
  
 
     OutputImage = ( 2 * besselj(1, normalizedRadius ) ./ ( normalizedRadius ) ) .^ 2;
 
     OutputImage = ( 2 * besselj(1, normalizedRadius ) ./ ( normalizedRadius ) ) .^ 2;

Revision as of 20:14, 28 September 2017

20.309: Biological Instrumentation and Measurement

ImageBar 774.jpg


Overview

All software has to be tested. The code you will develop for this part of the lab measures the size and position of fluorescent microspheres in images. To test the code you are developing, you will need images of fluorescent microspheres. One way to get some images is to go to the lab, snap a few pictures with your microscope, and use those for testing. This is a fine idea, and you ought to do just that. But there is a drawback to this approach: the microspheres are scattered randomly so you don't know the ground truth of where the microspheres are. How will you know if you got it right? Another good approach is to generate synthetic images of microspheres whose positions you know exactly. The drawback of synthetic images is that thewy may differ in some important way from the actual images that come out of your microscope. It's a good idea to test your code both ways.

Before you start working on your resolution and particle tracking code, let's get started by generating synthetic images.

A useful function: draw a particle

You are going to make synthetic images of fluorescent microspheres. The function below is a useful building block. It draws a single microsphere on a dark background. To make an image of multiple particles, you can add up the images of several individual particles. (You will get funny results if the particles overlap — a physical impossibility IRL.)

The function takes arguments that specify the location of the particle, its radius, and its intensity as well as the size of the image. It returns a two-dimensional array of doubles. The function does not include any noise sources or blurring due to the optical system. We'll that those in a minute.

function OutputImage = DrawOneFluorescentMicrosphere( ...
        YCenter, XCenter, Radius, Intensity, ImageSizeY, ImageSizeX )
    
    [ x, y ] = meshgrid( 1:ImageSizeX, 1:ImageSizeY );

    distanceFromParticleCenterPixels = hypot(x - XCenter, y - YCenter);
    distanceFromParticleCenterRadiuses = distanceFromParticleCenterPixels ./ Radius;
    mask = ( distanceFromParticleCenterRadiuses < 1 );

    OutputImage = Intensity .* mask .* cos( distanceFromParticleCenterRadiuses .* pi ./ 2 );

end

Remember that MATLAB functions have to be stored in a file that has the same name as the function, appended by ".m" Save the function in a file and give it a try ... type something like: imshow( DrawOneFluorescentMicrosphere( 100, 100, 30, 1, 200, 200 ) ); . Play around with the arguments. It's fun. You can make a giant microsphere or a tine one.

The next step is to write a function that can draw more than one particle. The function should take two arguments. The first argument is an N x 4 matrix. Each row of the matrix specifies a single microsphere. The first two columns contain the Y and X coordinates of the particle. The third column is the radius, and the fourth column is the intensity. Hint: use a for loop to iterate through each row of the matrix. Here is a framework for the function:

function OutputImage = SyntheticFluorescentMicrosphereImage( Particles, ImageSize )
    OutputImage = zeros( ImageSize );

    for ii = 1:size( Particles, 1 )
        position = Particles(ii, 1:2);
        radius = Particles(ii, 3);
        intensity = Particles(ii, 4);
        % PUT YOUR CODE HERE
    end
end


Pencil.png

Write a function that draws a simulated image of multiple particles specified in an N x 4 matrix.


Modeling optical resolution

We are going to model the blurring that occurs in optical systems by convolving our synthetic image with an Airy disk. The function below generates an Airy disk for a given NA, magnification, and pixel size.

function OutputImage = SimulatedAiryDisk( ...
        NumericalAperture, Magnification, Wavelength, PixelSize, ImageSize )

    airyDiskRadiusInPixels = 0.61 * Wavelength / NumericalAperture * Magnification / PixelSize;
    airyDiskCenter = ImageSize / 2;

    yAxis = (1:ImageSize(1)) - airyDiskCenter(1);
    xAxis = (1:ImageSize(2)) - airyDiskCenter(2);

    [ xCoordinate, yCoordinate ] = meshgrid( xAxis, yAxis );

    firstBesselZero = 3.8317;
    normalizedRadius = firstBesselZero .* hypot( xCoordinate, yCoordinate) ./ airyDiskRadiusInPixels;

    OutputImage = ( 2 * besselj(1, normalizedRadius ) ./ ( normalizedRadius ) ) .^ 2;
    
    % remove NaN from result if center is an integer
    if( all(airyDiskCenter == fix( airyDiskCenter )) )
        OutputImage( airyDiskCenter(1), airyDiskCenter(2) ) = 1;
    end
 end

Here's how you to use the Airy disk to simulate blurring by your microscope:

psf = SimulatedAiryDisk( 0.65, 40, 590E-9, 7.4E-9, [ 51, 51 ] );
psf = psf / sum( psf(:);
blurredImage = imfilter( IdealImage, psf );