ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D....

98
ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015

Transcript of ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D....

Page 1: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

ECES 490/690Cell & Tissue Image Analysis

Lecture #6: Clusters and Denoising

Andrew R. Cohen, Ph.D.

2/16/2015

Page 2: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Recap• Automatic thresholding

– Otsu• Optimal

– Cluster analysis• K-means

• GMM

• Adaptive thresholding– Sliding window– Other?

Page 3: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Today

• A bit more on clustering

• Noise removal

Page 4: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Dealing with “Bad” Neighbors

• Some forms of noise produce drastic and highly localized changes in pixel values– “Outliers”– Can have huge effect on the averages– Recognizing and eliminating these

outliers is one other way to treat neighbors differently

Page 5: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

The Median Filter• The median = The “middle value” of a bunch of

numbers– Just sort the numbers and extract the middle number– Robust to “outliers”

• “salt & pepper” noise

– Introduces no “new” values• e.g., does not make image dimmer

– Will smooth (in the extreme, “posterize”) upon repeated application

– Does not shift boundaries (edges)

• There are many variations on the median filter– E.g., center weighted median

• MATLAB – imf=medfilt2(im,[m n]);

Page 6: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Max/Min Filters• Basic Idea:

– The extreme values in a neighborhood can form the basis for useful operations

• Min: Can help us ignore bright outliers - “salt”

• Max: Can help us ignore dim outliers – “pepper”

– Suppose our objects are bright against a dark background

• Min: will erode our objects

• Max: will fatten (dilate) our objects

Page 7: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Max/Min Filters• Also called grayscale erosion and dilation

– Works just as well for binary-valued images

• We can use them together to smooth images:– First pass: pixel value replaced by max{neighbors}

– Second pass: pixel value replaced by min{neighbors}

• What happens if we reverse the order (i.e., min followed by max)?

• The size and shape of the neighborhood (“kernel”) makes a big difference.

Page 8: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Mathematical Morphology

se = strel('disk',10);imNoHoles = imclose(im,se);

Also:•imclose•imopen•imerode•imdilate•bwmorph(…)

Page 9: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Morphological Operations

Erosion- min operation

Dilation- max operation

Opening- erosion followed by dilation- removes small objects

Closing- dilation followed by erosion- closes gaps

All operations can be grayscale or binaryEfficient in vector (e.g. MATLAB) or parallel formulations

Page 10: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Operation

'bothat''branchpoints''bridge''clean''close''diag''dilate''endpoints''erode''fill''hbreak''majority''open''remove''shrink''skel''spur''thicken''thin''tophat'

BW2 = (BW,operation,n)

Morphology on logical images

Page 11: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Grayscale Closing

3 × 3 rectangular kernel 9 × 9 rectangular kernelMagnify for a closer look

Page 12: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Closing with circular kernel

9 × 9 circular9 × 9 rectangular kernelMagnify for a closer look

Page 13: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Opening

3 × 3 rectangular 9 × 9 rectangularMagnify for a closer look

Page 14: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Opening with Circular Kernel

9 × 9 circular9 × 9 rectangularMagnify for a closer look

Page 15: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Doing it in MATLAB

SE = strel(shape, parameters)

Shape: disk, diamond, line, octagon, rectangle, square, etc.

‘arbitrary’ shape allows us to specify a shape using a matrix of 1’s and 0’s

Can also specify 3-D neighborhoods such as an ellipsoid or an arbitrary 3-D matrix

First, define the “structuring element’

Page 16: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Standard Morphological Processing Operations IM2 = imerode(IM,SE)

IM2 = imdilate(IM, SE) IM2 = imclose(IM,SE)

IM2 = imopen(IM,SE)

What type of morphological processing steps will extract just the background pixels in these images?

Page 17: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Using Grayscale Morphology Functions for Pre-thresholding

Can you think of ways to process the Unscheduled DNA synthesis assay image using grayscale morphology instead of the top-hat filter that we discussed earlier?

problem 1: Find the cell nucleiproblem 2: Find the grains

Page 18: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Binary Morphological Operators

BW = bwmorph(bw, ‘operation’, n)- logical operations- open, close, erode, dilate- skeletonize- endpoints- branchpoints- many more functions

Page 19: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Introducing the Idea of Iterative Smoothing

• Smoothing is performed in a series of small steps, instead of just one step

• One simple approach is to apply the same algorithm repeatedly– Repeated median gives an

unchanging pattern called the “median root”

– Consists of flat regions with sloped edges

Median root, radius =1

Smoothing

Page 20: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Adaptive Smoothing

• Basic Idea: Apply different algorithms and/or settings to different pixels based on some local criteria.– No “universal” algorithm, need to build a custom algorithm for each

application/modality/object type

– More computation, but often well worth it.

• Example #1: – Smooth the background, but not the objects of interest. – Pixels brighter than threshold T are left untouched, while

others are replaced by neighborhood averages (i.e., smoothed over)

• Example #2: – Suppress the averaging across known object boundaries

Page 21: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Soft/Fuzzy Thresholding• Thus far, we have talked about thresholding as a

hard decision (foreground/background)– In some applications, we are better served by a soft

thresholding. Each pixel is labeled by a real number m (class membership value) that is in the range [0, 1].

– Values closer to 1 indicate foreground and vice versa.– Many applications:

• Adaptive smoothing is perhaps the most direct application

– Many approaches: • Use weights from mixture of probability distributions

– A variation on your previous homework exercise

• Use fuzzy k-means clustering instead of the regular k-means function (MATLAB function fcm instead of )– [center,U,obj_fcn] = fcm(data,cluster_n)

Page 22: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Using Hints from Object Boundaries to Improve Smoothing

• Local Object Boundary

• Local Intensity Gradient– No smoothing in this

direction please!!

• Local Perpendicular to Gradient– Smoothing in this

direction is helpful!!

Page 23: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Simple-minded idea: Directional 3x3 Smoothing

• Replace pixel value with weighted average for each kernel

• Pick the one that results in the least change– Implicitly selects the kernel that is

perpendicular to the boundary

• The type of averaging can vary• E.g., center-weighted average

• E.g., median instead of mean

0 0 1

0 1 0

1 0 0

0 1 0

0 1 0

0 1 0

0 0 0

1 1 1

0 0 0

1 0 0

0 1 0

0 0 1

Page 24: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Practical Considerations• This algorithm is cute but naïve

– Every little “wrinkle” in the image appears like an edge to this algorithm

– It has a limited 33 view of the image

• Need to distinguish real object boundaries, and avoid false internal ones– Unfortunately, smoothing can

change that answer– We need ways to prevent

premature decisions about object boundaries

– Iterative algorithms can help!

Page 25: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Edges, Edgels, & Boundaries• Edge: Locations of abrupt change in image

properties• Intensity, Texture, Color (spectral signature)

– “Edge” also used for connected chains of edge points, i.e., fragments of object contours

– Edge points also called “edgels” (for edge elements)

• The term “boundary” is used for the real thing– Edges are a fragmentary estimate of the boundary

Biggest Problem: Separating the edges that really correspond to object boundaries from others associated with texture and/or noise

Page 26: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Broader Indications of Boundaries

Step

Ramp

Roof

Ridge

True intensityNoisy observation

Page 27: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Basic Data Structures• Segmentation Labels

– Basis for choosing a “custom” averaging formula, or a parameter, at each pixel

• Edge Sites– Basis for suppressing

averaging across object boundaries

• Better segmentation helps boundary detection, and vice versa– Classic “chicken and egg”

situation

0 0 1

0 1 1

1 1 1

Page 28: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Local Intensity Gradient

r

( , ) ( , ) ( , )I x y I x y x I x y y

r x r y r

( , ) ( , )cos sin

I x y I x y

x y

is a maximum when

xyxI

yyxI

),(

),(

arctan

Object boundaries in fluorescence images commonly occur at locations of high intensity gradients

Page 29: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Edge Operators

• Finite-difference approximations to gradient• Inherently, a “noise enhancing” operation

– Responds to every little change in I(x,y)

x

yxxIyxxI

x

yxI

2

),(),(),(

x

yxIyxxI

x

yxI

),(),(),(

Central DifferenceApproximation

0 0 0

-1 0 1

0 0 0

0 0 0

0 -1 1

0 0 0

Page 30: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Ways to Combat Noise

-1 0 1

-1 0 1

-1 0 1

Averaging along the boundary(Prewitt operator)

-1 0 1

-2 0 2

-1 0 1

Weighted Averaging(Sobel operator)

Another idea:

Fit a plane to a local neighborhood of pixels yx

22),( yxIThen,

Page 31: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Exampleim=imread('ER81_E12pt5_0527090025_3_2AB02_05_t270.tif');im=mat2gray(im);level=graythresh(im);bwHalo=im2bw(im,level);imagesc(bwHalo) im=im2double(im);imagesc(im);colormap(gray) hy = fspecial('sobel') hx=hy'imx = imfilter(im,hx,'replicate');imy = imfilter(im,hy,'replicate'); mag=sqrt(imx.^2+imy.^2);hold off;imagesc(mag);colormap(hot) [r c]=find(bwHalo);pix=find(bwHalo);hold onquiver(c,r,imx(pix),imy(pix),0)

Page 32: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.
Page 33: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Another Noise Combating Method

• Just suppress weak edges!– Non-maximum suppression

• At each pixel (x,y), look within a small neighborhood

• Find the maximum gradient magnitude• If the gradient magnitude at (x,y) is less than the

maximum, simply attenuate it, or set it to zero!

– Better:• Find the local gradient direction• Do the suppression just along this direction

Page 34: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Thresholding for Edges

• Really, another noise combating method– Non-maximum suppression still leaves noise-

caused local maxima

– If the gradient magnitude is greater than a threshold T, set edgel to 1

• Else, set edgel to 0.

• Choosing a threshold– T too low Pick up too many noisy edgels

– T too high Miss real edgels, making boundaries discontinuous.

Page 35: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Bringing All These Ideas Together: The Canny Edge Detector

• Classic (circa 1986) and most popular edge detection algorithm

– Optimal for step edges corrupted by Gaussian Noise

– Three optimality criteria:1. Best Detection: Don’t miss correct edges, don’t pickup false edges

2. Spatial Localization: Distance between correct edge and estimated edge should be minimal

3. Minimal response: Minimize multiple responses at a single edge

Page 36: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Canny’s Algorithm

• Step 1: Image is smoothed using a Gaussian Filter

• Step 2: Compute the intensity gradient of the smoothed image– Gives us a gradient magnitude (“edge strength”)

and local direction at each pixel

• Step 3: Perform non-maximum suppression but only along the gradient direction

• Step 4: ‘Clever’ Thresholding– Let’s talk about Canny’s idea next…

Page 37: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Thresholding with a hysteresisBasic Intuition: Object boundaries are smooth and

connected.

1. We start off with two thresholds2. Get the most prominent edges:

– The most prominent edges will have a response that exceeds the higher threshold (THI). We detect them first.

3. Get the less prominent edges:– Starting from the most prominent edges, and using the

edge direction, seek out neighboring edges.– Include the neighbors whose strengths exceed the lower

threshold (TLOW). – In other words, we only include the weak edges as long

as they are connected to the strong edges. Unconnected weak edges are rejected as being due to noise.

{ , }LOW HIT T

Page 38: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Edge Detection in MATLAB

[BW,threshold] = edge(I,<method>,<paremeters>)

I is the grayscale image

<method> can be ‘sobel’ / ‘prewitt’ / ‘roberts’ / ‘zerocross’ / ‘canny’

<parameters> depend on the method used.

What parameters do we need to specify in canny’s Method?Answer: amount of Gaussian smoothing, and the thresholds

Page 39: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.
Page 40: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

original Canny

Sobel

Page 41: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

A Fresh, Look at Gaussian Smoothing

Pretend for now that pixel intensities I(x,y) describe the temperature profile over a 2-D region.

With time t, this profile spreads out according to the following formula:

),,(),,(),,(),,( 2

2

2

2

2

zyxIDy

tyxI

x

tyxID

t

tyxI

Solution for this differential equation:

),,(*2

exp2

1),,(

22tyxI

Dt

yx

DttyxI

convolution

Page 42: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

A Fresh Look at Gaussian Smoothing

),,(*2

exp2

1),,(

22tyxI

Dt

yx

DttyxI

One way to perform Gaussian smoothing by a kernel of standard width is to solve the heat diffusion equation, with

Dtt )(

Dtt )(Parameter Scale 2

Page 43: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Discrete Scale Space

2

2 ),(),(

x

xIxI

),(),(),( xIxIxI

22

2 ),(),(2),(),(

x

xxIxIxxI

x

xI

Dt

),(),()21(),(),( xxIxIxxIxI

Consider the 1-D case for simplicity:

Page 44: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Lindeberg’s Result),(),()21(),(),( xxIxIxxIxI

Our two scale space requirements are met if and only if:

4

1

Limiting case =1/4 leads to a very simple update equation:

),(4

1),(

2

1),(

4

1),( xxIxIxxIxI

Page 45: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Lindeberg’s ResultRecall: Approximate Gaussian Smoothing Filter:

¼ ½ ¼ 4

)(

2

)(

4

)()(

xxIxIxxIxI

oldoldoldnew

Result of successive [1/4, 1/2, 1/4] Filtering

0

0.2

0.4

0.6

0.8

1

1.2

1 3 5 7 9

11

13

15

17

19

21

23

25

27

29

31

33

x

I(x

,ite

rati

on

)

Page 46: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Practical Observations

• The [ ¼ ½ ¼ ] filter preserves the summed intensity of the image

• N iterations give us a variance of N2

• Computation of this filter is extremely efficient

• Lends itself to parallel computation nicely• Bottom Line:

– The Gaussian can be realized by simulating a diffusion process

Page 47: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Smoothing Before Differentiation

Approximate Gaussian Smoothing Filter:

¼ ½ ¼

-1 0 1

Differentiator:

-2 -1 0 1 2Single operation that combines both:

4

)(

2

)(

4

)()(

xxIxIxxIxI

oldoldoldnew

)()(Result xxIxxI newnew

2

)(

4

)2(

2

)(

4

)2( xxIxxIxxIxxI oldoldoldold

8

1

(scale factor)

Page 48: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Smoothing Along a Boundary

-2 -1 0 1 2

-2 -1 0 1 2

-2 -1 0 1 2

-2 -1 0 1 2

-2 -1 0 1 2

Note: If image brightness goes up from left to right, the result “response” is positive

The sign of the response can be put to good use

Page 49: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Anisotropic Diffusion

2( , , )( , , ) { ( ( , , ))}

I x y tD I x y t D I x y t

t

Isotropic heat diffusion

)},,(),({),,(

tyxIyxDt

tyxI

Anisotropic version:

y

IyxD

yx

IyxD

x),(),(

constant

Analogous to edge sites

Interpret edge sites as “conduction coefficients”

Page 50: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

The “conduction coefficients”

2),(

exp),(k

yxIyxD

Better: Use a smoothed version of I(x,y) insteadPeople have proposed other ways to choose D(x,y)

Basically, any good edge indicator in the range (0,1) usually works.

Page 51: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Discrete Approximation

y

IyxD

yx

IyxD

xt

I),(),(

),( yxh ),( yxv

Discrete Approximation:y

IIyxv

x

IIyxh

t

II yyyxxxttt

),(),(

Iterative Solution:

y

IIyxv

x

IIyxhtII yyyxxx

ttt ),(),(

Page 52: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Anisotropic Diffusion

y

IIyxv

x

IIyxhtII yyyxxx

ttt ),(),(

))(,())(,( yyyxxxttt IIyxvIIyxhII

At each iteration, we also update the edge sites according to:

2

exp),(k

IIyxh xxx

Page 53: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Example

original Gaussian, radius = 0.7

Page 54: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Example

Anisotropic diffusionGaussian, radius = 0.7

Page 55: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Example

Median Anisotropic diffusion

Page 56: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Anisotropic Diffusion Smoothing

( , ) ( , ) ( , ) ( , )( , ) ( , ) ( , ) ( , )t t t t

t t t t t

I x x y I x y I x y y I x yI x y I x y t h x y v x y

x y

0 0 1

0 1 1

1 1 1

2( , ) ( , )

( , ) exp t t t tt t

I x x y I x yh x y

k

( , )h x y

( , )v x y

2( , ) ( , )

( , ) exp t t t tt t

I x y y I x yv x y

k

; go backt t t

0

0 0

:

( , ) ( , );

( , ) ( , ) 0.5

Initialize

I x y I x y

h x y v x y

Repeat until convergence :

Think of edge sites as “conduction coefficients” and solve the heat diffusion equation:

Page 57: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Example

Median Anisotropic diffusion

Page 58: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Example – Liver Cells

Need to count nuclei in this confocal image

Identify pairs of nuclei that are closely situated

Page 59: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Desired Final Results

Page 60: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Review of Steps

6 iterations of smoothingwith anisotropic diffusions

After grayscale min operator smoothing

Gradient Adaptive Thresholding of gradient

Page 61: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

After Thresholding,…Labeling Connected Components

• Given a binary image, group pixels into connected components• A “connected component” is a set of pixels that are connected• In the end, each pixel has a label that indicates a region number

– A “higher-level” entity than foreground/background labeling– Often, but not always, corresponds directly to “objects” such as nuclei

Smoothing &Thresholding

ConnectedComponents

Labelingimage

Binary imageRegionslabeled

Page 62: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

What we want next

Convenient to use indexed color, and a random color table to display connected components

Image Pre-processing+ Binary Segmentation

Connected Components Labeling

Page 63: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Toy Example

0 1 1 0 1 0 0

0 1 1 0 1 0 1

1 1 1 0 1 0 1

0 0 0 0 1 1 1

0 1 0 0 0 0 0

0 1 1 1 1 1 0

0 1 1 1 0 0 0

0 1 1 0 2 0 0

0 1 1 0 2 0 2

1 1 1 0 2 0 2

0 0 0 0 2 2 2

0 3 0 0 0 0 0

0 3 3 3 3 3 0

0 3 3 3 0 0 0

Page 64: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

A Basic Operation

• Connected components labeling is a basic, core operation that is widely used in image analysis

• It can be used in a variety of creative ways– limited only by our imagination

• Useful any time we need to work with a connected set of pixels– Keep/Delete components based on size

• Get rid of small islands

– Hole removal• More on this shortly

– Identify background– Many others

Page 65: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Connected Pixels

Two pixels p and q are said to be connected if there is a sequence of foreground (1) pixels

0 1, ,.... np p pSuch that

0

1 is a neighbor of n

i i

p p

p q

p p

We can reach p from q by jumping from one neighbor to another

Page 66: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Pixel Neighbors

0 1 0

1 ? 1

0 1 0

Four nearest neighbors

1 1 1

1 ? 1

1 1 1

Eight nearest neighbors

A pixel’s neighbors are said to be “adjacent”

Page 67: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

4- and 8-Connected Pixels

• When only the four nearest neighbors are considered part of the neighborhood, then pixels p and q are said to be “4-connected”

• When the 8 nearest neighbors are considered part of the neighborhood, then pixels p and q are said to be “8-connected”

• Natural Question:– Which one is “better?”

Page 68: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Interesting Theorem

• Suppose that – C is a component of 1’s based on 4-connectedness– Suppose that D is an adjacent component of 0’s based on

8-connectedness– Then, either C surrounds D or vice versa

• Idea: Use 4-connectedness for 1’s and 8-connectedness for 0’s or vice versa– Surroundedness gives us a way to identify “holes”– Borders of both regions form closed curves

Page 69: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Example0 0 0 0 0

0 0 1 0 0

0 1 0 1 0

0 0 1 0 0

0 0 0 0 0

0 0 0 0 0

0 0 1 0 0

0 2 0 3 0

0 0 4 0 0

0 0 0 0 0

0 0 0 0 0

0 0 1 0 0

0 1 0 1 0

0 0 1 0 0

0 0 0 0 0

0 0 0 0 0

0 0 1 0 0

0 1 0 1 0

0 0 1 0 0

0 0 0 0 0

Binary Image 4-adjacency for 1’s and 0’sNumber labels used for 1’sColor labels used for 0’s

8-adjacency for 1’s and 0’s 8-adjacency for 1’s4-adjacency for 0’s

Page 70: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Hole Filling Example

Find 4-connected components for the background pixelsComponents smaller than a set threshold, and not touching the image border are set to foreground.

Page 71: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Connected Component Labeling Algorithm

I(x,y)

y

x

Y

X

0 1 1 0 1 1 0

0 1 1 0 1 1 0

0 1 1 1 1 1 0

Example ImageX = 7Y = 3

Page 72: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Step 1: Initialization

“Initialization of each 1-pixel to a unique label”

for x 1 to X do

for y 1 to Y do

if I(x,y) = 1

then LABEL (x,y) NEWLABEL( )

else LABEL(x,y) 0

end for

end for;

Keep incrementingat each call

Page 73: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Illustrating Step 1

0 1 1 0 1 1 0

0 1 1 0 1 1 0

0 1 1 1 1 1 0

0 1 2 0 3 4 0

0 5 6 0 7 8 0

0 9 10 11 12 13 0

Original Binary Image Initial Unique Labeling

Page 74: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Step 2: Top-down & Bottom-up Passes

repeat

CHANGE false;

Do “top-down pass”;

Do “bottom-up pass”;

Until CHANGE = false

Set each label to the minimum of connected neighbors

Page 75: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

The Top-down Pass

for x 1 to X do for y 1 to Y do if LABEL(x,y) 0 then begin

M := MIN(LABELS(NEIGHBORS((x,y)) (x,y))); if M LABEL(x,y) then CHANGE true; LABEL(x,y) M end

end forend for;

Page 76: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Illustration

0 1 1 0 1 1 0

0 1 1 0 1 1 0

0 1 1 1 1 1 0

0 1 2 0 3 4 0

0 5 6 0 7 8 0

0 9 10 11 12 13 0

Original Binary Image Initial Unique Labeling

0 1 1 0 3 3 0

0 1 1 0 3 3 0

0 1 1 1 1 1 0

After First Top-Down Pass

Page 77: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Bottom-up Pass

for x X to 1 by –1 do for y Y to 1 by –1 do if LABEL(x,y) 0 then begin M MIN(LABLE(NEIGHBORS((x,y)) (x,y)));

if M LABEL(x,y) then CHANGE true; LABEL(x,y) M end

end forend for

Backwards

Page 78: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Illustration

0 1 1 0 1 1 0

0 1 1 0 1 1 0

0 1 1 1 1 1 0

0 1 2 0 3 4 0

0 5 6 0 7 8 0

0 9 10 11 12 13 0

Original Binary Image Initial Unique Labeling

0 1 1 0 3 3 0

0 1 1 0 3 3 0

0 1 1 1 1 1 0

0 1 1 0 1 1 0

0 1 1 0 1 1 0

0 1 1 1 1 1 0

After First Top-Down Pass After First Bottom-Up Pass

Page 79: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Computing Connected Components in MATLAB

[L,num] = bwlabel(BW,n)

• BW is a binary label image• n specifies the type of connectivity you want

to use (4 or 8)• num is the number of connected components

found• L is an image with integer valued pixels

(values go from 0, 1, 2, ….up to num-1)Note: There is a related function bwlabeln that works for n-dimensional images

Page 80: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Related Functions

RGB = label2rgb(L)

• This function takes the label image L returned by bwlabel, and returns a color image that has a different color for each label.

Page 81: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Objects & Connected Components

• Many of the connected components represent objects of interest to us

• Some of them don’t:– Could be clusters of joined

objects (under segmentation)– Could be fragments of objects

(over segmentation, or artifacts)

• We need a way to figure out what is going on!

Page 82: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Modeling Objects via Features• A feature is a number that describes some aspect

of a connected components– E.g., shape, size, etc.– We can “model” an object as a function of one or more

features.

• Numerous features are defined in the literature– You can invent your own for a given problem

• Basic Types of Features– Shape Features– Size Features– Brightness/Absorption Features– Texture Features

Page 83: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

A Simple Model for NucleiOur modeling efforts thus far have focused on

intensity-based properties

Now that we have connected components, we can consider a richer set of properties:

– Circular: circularity, Ci

– or Elliptical: eccentricity, Ei – Convex: convexity, Si – Bounded size: Ai

– No voids

Example model:

Ci TC orEi TE andSi TS andAmin Ai A

max

Page 84: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Size Features

• Just compute a histogram of the connected components labeled image!

• Yields the area of each object, indexed by label

• Easy to detect smallest and largest objects

• Easy to filter/edit objects by size – just modify the labels

0 1 1 0 2 0 0

0 1 1 0 2 0 2

1 1 1 0 2 0 2

0 0 0 0 2 2 2

0 3 0 0 0 0 0

0 3 3 3 3 3 0

0 3 3 3 0 0 0

Label Count

0 25

1 7

2 8

3 9

Histogram:

Page 85: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Accuracy of Size Measurement • Counting pixels is an unbiased estimator of area

• Most of the variation in the measurement is due to the pixels along the border.

• One simple idea: – Assume that, on average, the pixels on the shape boundary

are really half on the object, and half on the background– Subtract half the area associated with these boundary pixels

Area 12

boundaryNp

Area p #pixels

2( )mean p R For approx. circular object

Note: In the end, we must scale the area by the physical specimen area covered by each pixel.

Page 86: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

More Accurate Size Measurement • Most of the variation in area measurement

occurs due to the pixels at the border.

• Approximate calculation: – Assume object is roughly circular, with radius R

– Random Poisson sampling of the border region gives a mean and variance of

– More generally,

– Denser sampling leads to better precision

http://www.ph.tn.tudelft.nl/People/albert/papers/ISBI2004_LVPVTY.pdf

2 2var( ) ( )p R R

2( )mean p R

3/ 2

var( )( ) 2 1( )

( ) ( )

pstdev pCV p

mean p mean p R

3/ 2

1( )CV p k

R

Page 87: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

MATLAB Area Function• total = bwarea(BW)

estimates the area of the objects in binary image BW

• Algorithm used:– Look at a pixel’s 2x2

neighborhood blocks– Keep in mind that each

pixel is part of four different 2-by-2 blocks.

– So, a single on pixel surrounded by 0 pixels has a total area of 1.

# of 1’s 2x2 Area

Contribution

0 0

1 ¼

2 adjacent ½

2 diagonal ¾

3 7/8

4 1

X

Page 88: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Extracting the coordinates of a specific connected component

[r,c] = find(bwlabel(BW)==n);

• This will return the row and column values for connected component n

• Convenient way to write programs to compute features for a connected component

[L num]=bwlabel(bw,8);STATS = regionprops(L, 'perim','area');

Page 89: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

The Feret Box & Minimum Enclosing Rectangle

• Feret Box: – Smallest rectangle

oriented along the x,y axes enclosing the object

• Minimum Enclosing Rectangle (MER): – Smallest rectangle

(oriented along any direction) that encloses the shape

– Rotate the object, and make a Feret box for each angle. Smallest box is the MER

Page 90: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Finding the Feret Box• The smallest aligned

rectangular box that contains the object– A useful way to localize

computations!

• Just find the max and min x and y coordinates for the connected component

x

y

),( maxmin yx

),( minmax yx

Page 91: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Diameter

• Find the two points that are furthest apart

• Find the distance between them

• The line joining the extreme points is called the maximum chord

Page 92: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Centroid

• Also known as the first-order moments

i

ixN

x1

i

iyN

y1

Page 93: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Summary• Smoothing followed by thresholding yields a binary

image representing the foreground objects of interest– Use iterative and adaptive smoothing and/or thresholding algorithms

as needed to improve these steps

• Connected component labeling allows us to identify potential objects (or parts thereof, or groups of them, as the case may be)– This is a “super powerful” and highly versatile operation – Can be used in diverse ways, limited only by our imagination– More on this next class

• To go from connected components to objects, we need to create a mathematical “model” describing the objects of interest to us– Features help us do this– We’ll explore more features next class

Page 94: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Summary

• Pre-processing prior to thresholding– Local contrast– We’ll discuss more methods (e.g., grayscale

morphology based methods)

• Cluster Analysis (grouping) is another idea for achieving automatic threshold selection

• Automatic and locally-adaptive thresholding

Page 95: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Summary• Imaging Noise & distortions

– 3 main sources– Flat-field and dark-field corrections

• Pre-processing can improve thresholding results– Cleanup noise – Rank filters (min, median, max) – Grayscale Erosion, Dilation, Opening, Closing are basic

cleanup operators

Page 96: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Homework1. Download the image “cell_image.TIF” from

the course website– Write a program that automatically extracts the

foreground in this image using any two non-adaptive methods discussed in class

– Write a second program that adaptively extracts the foreground in this image using any combination of methods discussed in class

– Submit a description of your algorithm, a printout of your code, and the results

2. Keep working on the course project!

Page 97: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Random Useful Slide – Michel implementation

h = fspecial('gaussian',GS,GV);

imfilt = imfilter(im, h, 'symmetric');

for j=1:LPF

imfilt = imfilter(imfilt, h, 'symmetric');

end

imhfreq = max((im - imfilt), zeros(size(im)));

imf = medfilt2(imhfreq,[MF MF]);

imf = mat2gray(imf);

Page 98: ECES 490/690 Cell & Tissue Image Analysis Lecture #6: Clusters and Denoising Andrew R. Cohen, Ph.D. 2/16/2015.

Instructor Contact Information

Andrew R. CohenAssociate Prof.Department of Electrical and Computer EngineeringDrexel University3120 – 40 Market St., Suite 110Philadelphia, PA 19104office phone: (215) 571 – 4358http://bioimage.coe.drexel.edu