You are on page 1of 12

Image cluster analysis of Marr's Data


Image cluster analysis of Marr's Data

Original image (im)

The values of each pixel range from 0 to 1.

Thresholded image (bw1)

If pixel>0.5 then newpixel = 1, else = 0. So this is a bitmap. Uses im2bw(im,0.5)

Zoomed in on single blob showing single pixel hole

Problem is this picture has some holes and the blobs are not well formed.

1 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

Connected region masks (bw4)

We improve the bitmap by closing boundaries of connected regions (bwmorph(bw1,'close')), filling

in isolated empty pixels (bwmorph(bw2,'fill')), and thickening (bwmorph(bw3,'thicken',5)) connected
regions. It is important that the thickening algorithm will not connected disconnected regions.

After this step, bwlabel(bw4) is used to label the pixels of each connected region with a number.

Single connected region (c)

2 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

This allows us to mask out the region corresponding to single blobs from the original picture. We
then take a weighted mean of these pixels to determine the center point. This is done for each of the
19 blobs.

Regions identified and located (x's in middle)

region# x y
1 67.77 66.61
2 75.33 8.30
3 82.30 223.76
4 86.80 114.64
5 101.29 54.31
6 104.31 162.75
7 120.42 102.56

3 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

8 135.45 43.00
9 138.98 153.05
10 149.05 211.71
11 155.37 93.12
12 157.57 14.59
13 170.92 47.64
14 172.17 139.85
15 188.95 80.52
16 193.76 19.94
17 205.60 125.92
18 222.89 68.39
19 240.87 117.67

% File: clusterim.m
% Date: 17-Oct-97
% Author: I. Chuang
% MATLAB5 script: read in an image with white dots and find where they
% are located, quantitatively.

[im,cmap] = tiffread('0-none.tif'); % read in image

if(~isgray(im)) % make sure it's grayscale

disp('oops, image not grayscale!');

bw1 = im2bw(im,0.5); % threshold to get peaks

bw2 = bwmorph(bw1,'close'); % do boundary closure
bw3 = bwmorph(bw2,'fill'); % fill isolated empty pixels
bw4 = bwmorph(bw3,'thicken',5); % thicken patch regions w/o connecting
[cim,num] = bwlabel(bw4); % label each connected region

% at this point, cim is a matrix which contains the integer n at each

% point inside the nth connected region. a connected region is a connected
% blob of points.

% make a grid of point im(i,j) is located at gx(i,j),gy(i,j)

% this makes it easy to calculate location of the blobs
[gx,gy] = meshgrid(1:size(im,2),1:size(im,1));
npix = prod(size(im)); % number of pixels in the image

for k = 1:max(max(cim)) % loop over each connected region found

c = (cim==k) .* im; % extract the kth connected region
wt = npix/sum(sum(c)); % find , by taking a weighted mean
x(k) = mean2(gx.*c)*wt;
y(k) = mean2(gy.*c)*wt;

figure(1); % bring figure #1 forward

clf; % clear current figure
imshow(im); % display the original image
hold on; % allow us to plot on top of the image
plot(x,y,'kx'); % plot black x's where we think blobs are
imzoom on; % allow user to zoom in with mouse

[q,map] = frame2im(getframe); % grab the current plot

tiffwrite(q,map,'0-marked.tif','compress'); % output it to file

fprintf(1,'region# x y\n');
for k = 1:length(x)
fprintf(1,' %3d %8.2f %8.2f\n',k,x(k),y(k));

Matlab image processing info

4 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

>> help images

Image Processing Toolbox.

Version 2.0 Beta 15-Nov-1996

Release notes
Readme - Version 2.0 Beta release notes.

Image types and type conversions.

dither - Floyd-Steinberg image dithering.
gray2ind - Convert gray scale intensity image to indexed image.
im2bw - Convert image to black and white by thresholding.
ind2gray - Convert indexed image to gray intensity image.
ind2rgb - Convert indexed image to an RGB image.
isbw - True for black and white images.
isgray - True for intensity images.
isind - True for indexed images.
mat2gray - Convert matrix to (gray) intensity image.
rgb2gray - Convert RGB values to gray.
rgb2ind - Convert RGB image to indexed image.

Image file I/O.

imread - Read an image file.
imwrite - Write an image file.

Image display.
colorbar - Display color bar (MATLAB Toolbox).
getimage - Get image data from axes.
image - Display indexed image (MATLAB Toolbox).
imagesc - Scale data and display as image (MATLAB Toolbox).
immovie - Make a movie of an image deck.
imshow - Display all types of image data.
imzoom - Zoom in and out an image or 2-D plot.
montage - Display an image deck as a rectangular montage.
subimage - Display multiple images.
truesize - Resize figure so that image is actual size.
warp - Warp an image onto a surface.

Geometric operations.
griddata - Data gridding and surface fitting (MATLAB Toolbox).
imcrop - Crop image.
imresize - Resize image.
imrotate - Rotate image.
interp2 - Two-dimensional data interpolation (MATLAB Toolbox).

Pixel values and statistics.

contour - Contour (level curves) plot (MATLAB Toolbox).
corr2 - Two-dimensional correlation coefficient.
imhist - Image histogram.
impixel - Color of a pixel.
improfile - Intensity profile.
mean2 - Mean of a matrix.
std2 - Two-dimensional standard deviation.

Image analysis.
edge - Edge extraction.
qtdecomp - Quadtree decomposition.
qtgetblk - Get block values according to a quadtree decomposition.
qtsetblk - Assign block values according to a quadtree decomposition.

Image enhancement.
grayslice - Density (intensity) slicing.
histeq - Histogram equalization.
imadjust - Adjust and stretch image intensity.
imnoise - Image noise.
wiener2 - Adaptive 2-D Wiener filtering.

Linear filtering.

5 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

conv2 - Two-dimensional convolution (MATLAB Toolbox).

convmtx2 - Two-dimensional convolution matrix.
convn - N-dimensional convolution (MATLAB Toolbox).
filter2 - Two-dimensional filtering (MATLAB Toolbox).
fspecial - Special 2-D filters.

Linear 2-D filter design.

freqspace - Frequency response spacing (MATLAB Toolbox).
freqz2 - Two dimensional frequency response.
fsamp2 - 2-D FIR filter design via frequency sampling.
ftrans2 - 2-D FIR filter design via frequency transformation.
fwind1 - 2-D FIR filter design using 1-D windows.
fwind2 - 2-D FIR filter design using 2-D windows.

Image transforms.
dct2 - Two-dimensional discrete cosine transform.
dctmtx - Discrete cosine transform matrix.
fft2 - Two-dimensional fast Fourier transform (MATLAB Toolbox).
fftn - N-dimensional fast Fourier transform (MATLAB Toolbox).
fftshift - Move zeroth lag (DC component) to center (MATLAB Toolbox).
idct2 - Two-dimensional inverse discrete cosine transform.
ifft2 - Two-dimensional inverse FFT (MATLAB Toolbox).
ifftn - N-dimensional inverse fast Fourier transform (MATLAB Toolbox).
radon - Radon transform.

Neighborhood and block processing.

bestblk - Best block size for block processing.
blkproc - Process an image in blocks.
col2im - Rearrange distinct or sliding column blocks to form image.
colfilt - Local non-linear filtering as columns.
im2col - Rearrange distinct or sliding blocks into columns.
medfilt2 - Two-dimensional median filtering.
nlfilter - Local non-linear filtering.
ordfilt2 - 2-D order-statistic filtering.

Region-based processing.
mfilter2 - Masked filter.
roicolor - Define region of interest (ROI) by color.
roifill - Smoothly interpolate within a specified region.
roipoly - Define polygonal region of interest (ROI).

Binary image operations.

applylut - Binary image lookup table operation.
bwarea - Area of objects in binary image.
bweuler - Euler number.
bwfill - Binary image flood fill.
bwlabel - Label connected components in a binary image.
bwmorph - Morphological operators.
bwperim - Perimeter of objects in a binary image.
bwselect - Select objects in a binary image.
dilate - Dilate (thicken) a binary image.
erode - Erode (thin) a binary image.
makelut - Generate LUT for use in APPLYLUT.

Colormap manipulation.
brighten - Brighten or darken a colormap (MATLAB Toolbox).
cmgamdef - Default gamma correction table.
cmgamma - Gamma correct colormap.
cmpermute - Permute colormap positions.
cmunique - Find unique colormap colors and corresponding image.
colormap - Set or get the color look-up table (MATLAB Toolbox).
imapprox - Approximate indexed image by an image with fewer colors.
rgbplot - Plot RGB colormap components (MATLAB Toolbox).

Colorspace conversions.
hsv2rgb - Convert HSV values to RGB values (MATLAB Toolbox).
ntsc2rgb - Convert NTSC values to RGB values.
rgb2hsv - Convert RGB values to HSV values (MATLAB Toolbox).
rgb2ntsc - Convert RGB values to NTSC values.

6 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

rgb2ycbcr - Convert RGB values to YCbCr values.

ycbcr2rgb - Convert YCbCr values to RGB values.

>> help bwmorph

BWMORPH Binary image morphological operations.

BW2 = BWMORPH(BW1,OPERATION) applies a specific
morphological operation to the binary image BW1.

The input image BW1 can be of class double or uint8. The

output image BW2 is of class uint8.

BW2 = BWMORPH(BW1,OPERATION,N) applies the operation N

times. N can be Inf, in which case the operation is repeated
until the image no longer changes.

OPERATION is a string that can have one of these values:

'bothat' Subtract the input image from its closing
'bridge' Bridge previously unconnected pixels
'clean' Remove isolated pixels (1's surrounded by 0's)
'close' Perform binary closure (dilation followed by
'diag' Diagonal fill to eliminate 8-connectivity of
'dilate' Perform dilation using the structuring element
'erode' Perform erosion using the structuring element
'fill' Fill isolated interior pixels (0's surrounded by
'hbreak' Remove H-connected pixels
'majority' Set a pixel to 1 if five or more pixels in its
3-by-3 neighborhood are 1's
'open' Perform binary opening (erosion followed by
'remove' Set a pixel to 0 if its 4-connected neighbors
are all 1's, thus leaving only boundary
'shrink' Shrink objects without holes to single pixels;
shrink objects with holes to connected rings
'skel' Remove pixels from the boundaries of objects
without changing their 8-connectivity
'spur' Remove spur pixels
'thicken' Add pixels to the boundaries of objects
without changing their 8-connectivity
'thin' Shrink objects without holes to lines; shrink
objects with holes to connected rings
'tophat' Subtract the opening from the input image


>> help bwlabel

BWLABEL Label connected components in a binary image.

[L,NUM] = BWLABEL(BW,N) returns a matrix L, of the same size
as BW, containing labels for the connected components in BW.
NUM is the number of connected components found.
N can have a value of either 4 or 8, where 4 specifies
4-connected components and 8 specifies 8-connected components.

The elements of L are integer values greater than or equal to

0. The pixels labeled 0 are the background. The pixels
labeled 1 make up one component, the pixels labeled 2 make
up a second component, and so on.

The input image BW can be of class double or uint8. The

output matrix L is of class double.

[L,NUM] = BWLABEL(BW) uses the default 8-connected components.

7 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

See also BWEULER.

MATLAB Image processing toolbox readme (5.1)

>> help images/Readme

README file for the Image Processing Toolbox.

This file contains information about the MATLAB Image Processing

Toolbox, version 2.0. The purpose of this file is to describe the ways
in which the toolbox differs from version 1.0. Topics include:

- general enhancements
- new functions
- enhancements to specific functions
- compatibility issues
- bug fixes


This section summarizes general areas of enhancement to the toolbox. See

the Image Processing Toolbox User's Guide for more information about these

Support for 8-bit image data

Most of the functions in the toolbox have been rewritten to take
advantage of MATLAB's support of unsigned 8-bit integers (uint8 data).
You can now read images into MATLAB as uint8 arrays, and process these
images with the toolbox functions. As a result, memory usage for most
image-processing operations has been dramatically reduced.

N-dimensional arrays
The toolbox takes advantage of another new MATLAB feature, N-dimensional
arrays. For example, an RGB image is now stored in a single
three-dimensional array, rather than requiring three separate matrices.
In addition, multiframe images no longer require image decks, but can
now be stored and processed in four-dimensional arrays.

Several of the toolbox functions have been rewritten to run faster, in
some cases substantially so.


This section describes the new functions in the Image Processing Toolbox,
as well as new functions in the MATLAB Toolbox that are useful for
image-processing applications.

Image file I/O

imfinfo - Return information about image file (MATLAB Toolbox).
imread - Read image file (MATLAB Toolbox).
imwrite - Write image file (MATLAB Toolbox).

Image analysis
qtdecomp - Perform quadtree decomposition.
qtgetblk - Get block values in quadtree decomposition.
qtsetblk - Set block values in quadtree decomposition.

Image enhancement
ordfilt2 - Perform 2-D order-statistic filtering.

Linear filtering
convmtx2 - Compute 2-D convolution matrix.
convn - Perform N-D convolution (MATLAB Toolbox).

8 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

Image transforms
dctmtx - Compute discrete cosine transform matrix.
fftn - Compute N-D fast Fourier transform (MATLAB Toolbox).
ifftn - Compute N-D inverse fast Fourier transform (MATLAB Toolbox).

Binary image operations

applylut - Perform neighborhood operations using lookup tables.
bwfill - Fill background regions in binary image.
bwlabel - Label connected components in binary image.
bwselect - Select objects in binary image.
makelut - Generate lookup table for use with applylut.

Region-based processing
roifill - Smoothly interpolate within arbitrary region.

Toolbox preferences
iptgetpref - Get value of Image Processing Toolbox preference.
iptsetpref - Set value of Image Processing Toolbox preference.

Demos and sample images

There are many new demos and sample images in the toolbox. To see a
complete list, type:

helpwin imdemos


Several functions in the toolbox have been enhanced to provide additional

capabilities. This section summarizes these enhancements.

blkproc - The function you specify no longer needs to return output of

the same size as the input. The output can be any size, including

bwmorph - You can now specify the number of iterations as Inf, in which
case bwmorph repeats the specified operation until the image stops

dilate, erode - You can now choose between a new frequency-domain

algorithm and the spatial-domain algorithm used in version 1. The
frequency-domain algorithm is faster for large structuring elements,
but uses more memory. Also, the spatial-domain algorithm is faster
than it was in version 1.

edge - There are two new methods available, 'log' and 'zerocross'.

hsv2rgb, ntsc2rgb, rgb2hsv, rgb2ntsc - These functions can now convert

images as well as colormaps.

imhist, medfilt2, roipoly - These functions are considerably faster

than in version 1.

imshow - The imshow function has been enhanced in many ways. See the
User's Guide for information about imshow.


In order to take advantage of MATLAB features such as uint8 support and

N-dimensional arrays, most of the functions in the toolbox have been
rewritten. This has introduced some incompatibilities that you should be
aware of. This section summarizes these incompatibilities.

Grandfathered functions

Several functions from version 1 of the toolbox have been

"grandfathered." This means that these functions are no longer

9 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

supported, but still work in version 2. These functions have been

superseded by new functions in the toolbox or in MATLAB. We recommend
that you do not use the grandfathered functions, and that you rewrite
any M-files you have that rely on them. They will be removed from the
toolbox in a future release.

The function mfilter2 has been renamed roifilt2. mfilter2 has been

These toolbox functions are redundant with functions available in


imslice - use N-dimensional arrays and MATLAB indexing

imzoom - use zoom instead

These image file i/o functions from version 1 have been superseded by
imread, imwrite, and imfinfo:


The gifread and gifwrite functions have been removed from the toolbox
due to patent restrictions.

uint8 arrays
Many of the functions in the toolbox now return uint8 arrays. For
example, functions that return binary images now return them as uint8
arrays. If you have code that performs mathematical operations on
binary images, you will need to modify this code to convert the images
to double-precision arrays, because mathematical operations are not
supported for uint8 arrays.

In addition, the conventional data ranges are different for uint8 and
double-precision images. For example, a double-precision intensity
image has data in the range [0,1], while a uint8 intensity image has
data in the range [0,255]. See the User's Guide for more information
about differences between uint8 and double-precision images.

RGB images
The toolbox functions that take RGB images as input now accept a single
three-dimensional array rather than three two-dimensional matrices.
For example:

imshow(R,G,B) - v1 syntax
imshow(RGB) - v2 syntax

The R,G,B syntax for these functions is grandfathered. It still works

in version 2, but is no longer supported and will be removed in a
future release.

Image decks
Multiframe images are now stored as four-dimensional arrays, rather
than as image decks. The functions that work with image decks, such as
montage, now accept a 4-D array as input. The image deck syntaxes have
been grandfathered.

Morphological operations
In version 1, the dilate and erode functions accepted either a
structuring element or a string such as 'thin'. The string syntax has
been grandfathered, because in all cases the same operation is

10 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

available through the bwmorph function.

Similarly, in version 1 the bwmorph function accepted either a string or

a lookup table. The lookup table syntax has been grandfathered, and these
operations are now provided by the makelut and applylut functions.
Also, the syntax in which bwmorph returned a lookup table has been
grandfathered. In some cases, this syntax still works; however, for
many operations, a lookup table is no longer used, and this syntax
produces a warning.

In addition, the bwmorph operation 'fatten' has been grandfathered,

because it does not work well.

Detecting image type

In version 1, some of the toolbox functions could determine the type of
an image based on its data range, and process it accordingly. An image
with integer values greater than 1 was always an indexed image.

In version 2, the image type is more difficult to determine; an image

with integer values greater than 1 can be indexed, intensity, or RGB.
Therefore, certain functions now require you to specify the image
type. These functions are blkproc, colfilt, im2col, medfilt2, and
nlfilter. For example, to process an indexed image with medfilt2:

X2 = medfilt2(X1,'indexed')

If you omit the 'indexed' string, X1 is processed as an intensity

image, and the results may not be correct.

Also, the functions that perform interpolation formerly used a

different default method depending on whether the image was indexed or
not. These functions (improfile, imrotate, and imresize) now always default
to nearest neighbor interpolation, regardless of the image type.

dct2 function
This function produces different results than in version 1, because
it now uses the unitary form of the transform.

edge function
The syntax of the edge function has changed substantially. The 'marr'
method has been grandfathered, and has been replaced by two new
methods, 'log' and 'zerocross'. (The 'marr' method will still work,
but will produce different results from version 1.) Also, the
directionality factor is now specified with a string rather than a
vector, and the order of the arguments has changed. For information
about the new syntax, type "help edge" or see the User's Guide.


This section summarizes the most important bugs in version 1 of the

toolbox that have been fixed in version 2.

bwmorph - The 'thicken', 'skel', and 'thin' operations did not preserve
the Euler number.

corr2 - This function produced incorrect answers for inputs with

nonzero means.

edge - The 'marr' method failed to produce continuous contours and

produced double-pixel rather than single-pixel edges; the 'sobel'
method was computed incorrectly; and the 'roberts' method failed to
find some horizontal and vertical lines.

imnoise - The noise variance calculation for the noise type 'speckle'
was incorrect.

improfile - This function produced an error if the input had duplicate

points; and the distance-along-profile computation was incorrect in

11 of 12 10/5/00 1:40 PM
Image cluster analysis of Marr's Data

some circumstances.

imrotate - In some cases, if you specified a negative angle, imrotate

did not rotate the image.

radon - This function produced inaccurate results.

roipoly - This function sometime produced incorrect results, especially

if the input polygon had closely-spaced vertices.

>> diary off

12 of 12 10/5/00 1:40 PM