Sei sulla pagina 1di 28

CSE585/EE555: Digital Image Processing II

Computer Project #3:

Nonlinear Filtering and Anisotropic Diffusion

Timothy Hackett, Kenrick Dacumos, Matthew McTaggart

Date: 03/24/2017

A. Objectives

This purpose of this project is to understand nonlinear filtering and anisotropic


diffusion for image filtering. The objectives of the project are:
• Perform 7x7 median, 7x7 alpha-trimmed mean, 7x7 sigma filter, 7x7
symmetric nearest-neighbor mean filtering on the “disk.gif” and
“cwheelnoise.gif” images for 1 and 5 iterations
• To compare the histograms resulting from the nonlinear filtering
B. Methods

Part 1
The first objective of the project was to filter noise from the “disk.gif” and
“cwheelnoise.gif” images using nonlinear filters. The filters are 7x7 median, 7x7
alpha-trimmed mean, 7x7 sigma, and 7x7 symmetric nearest-neighbor mean filter.
Each filter was iterated five times with the first and fifth iteration being of interest.

The median filter is applied with a 7x7 mask that selects the middle order statistic
within the mask that cycled through all the image pixels. This was done using the
“medfilt2.m” function. A first order statistic is the lowest gray level within the mask,
and an nth order statistic refers to the nth lowest gray level within the mask. The
erosion operation refers to the first (min) order statistic filter, and the dilation
operation refers to the max order statistic filter.

The alpha-trimmed mean filter is applied with a 7x7 mask. The alpha parameter used
in the project is 0.25. The alpha value determines or “trims” the range of order
statistics to be used in the mean calculation. The alpha value must be between 0 and
0.5, with 0 approaching a mean filter and 0.5 approaching a median filter. An alpha of
0 approaches a mean filter because it does not shorten the range of order statistics. An
alpha of 0.5 approaches a median filter because it shortens the range of order statistics
towards the middle value. The alpha-trimmed mean filter was implemented using the
“alphatrim.m” function. The following equation outlines the mathematics for the
alpha-trimmed mean filter. N2 refers to the number of pixels within the mask. The
alpha value, α, controls the amount of order statistics to be used in the trimmed mean.
𝑁2 −⌊𝛼𝑁⌋
1
𝑦𝑘 = ( 2 ) ∑ 𝑥(𝑖)
𝑁 − 2⌊𝛼𝑁⌋
𝑖=⌊𝛼𝑁⌋+1

The “alphatrim.m” function accepts an input image, an arbitrary user defined mask
(7x7 in this case), and the user defined alpha value. The output is the alpha-trimmed
image.
The sigma filter is applied with a 7x7 mask. Sigma was set to the value of 20 pixels.
The sigma value (standard deviation) determines which pixels are included in the
filter and which are excluded. Specifically, all pixels in the mask that are within 2
standard deviations (±2σ) of the pixel of interest in the original image are included in
the mask. These pixels are then averaged together to get the filtered output of the
pixel of interest. Using a low standard deviation (sigma) value implies that local
neighborhood pixels in the original (uncorrupted) image have a low variation, which
is generally the case with natural images. The sigma filter preserves the local
structure around a point by only averaging “similar” neighbor pixels. It preserves
edges (transition from bright to dark or vice versa), edge junctions, and lines. It
doesn’t perform well with strictly salt noise on a light region and pepper noise on a
dark region. This is because when the filter is currently operating on a salt/pepper
pixels on opposite regions, there are generally no pixels within ±2σ of its value, and
as a result, the output of the filter is just the input (no neighbors are averaged).

The sigma filter was implemented using the “sigmafilter.m” function. The following
equation outlines the mathematics for the sigma filter. In the equation below, 2N+1
represents the total pixels in the mask, and NC represents the number of pixels
included from the mask in the filter (those within ±2σ).
𝑁
1
𝑦𝑘 = ( ) ∑ 𝛿𝑖 𝑥𝑘−𝑖
𝑁𝐶
𝑖=−𝑁

1, |𝑥𝑘−𝑖 − 𝑥𝑘 | ≤ 2σ
𝛿𝑖 = {
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
The “sigmafilter.m” function accepts an input image, the height and width of a
rectangular structuring element (7x7 in this case), and the user defined sigma value.
The output is the sigma filtered image.

The symmetric nearest neighbor mean filter is applied with a 7x7 mask. For each pair
of points symmetrically opposite each other
{𝑥(𝑘−𝑖,𝑙−𝑗) , 𝑥(𝑘+𝑖,𝑙+𝑗) },
the point 𝑥 closer in value (|𝑥 − 𝑥𝑘,𝑙 |) to the pixel of interest (𝑥𝑘,𝑙 ) is selected. All of
the selected points and the pixel of interest are then averaged together to create the
new filtered value of the pixel of interest. Because this filter is uses whichever point
in each pair is closer to the pixel of interest (regardless of different the values are),
this filter works better on salt and pepper noise than the sigma filter.

The symmetric nearest neighbor mean filter was implemented using the
“snnmfilter.m” function. The following equation outlines the mathematics for the
symmetric NN mean filter. The operator [𝑘1 , 𝑘2 ] means to choose the point in the
pair whose value is closer to 𝑥𝑘,𝑙 .
𝑦𝑘,𝑙 = 𝑚𝑒𝑎𝑛{[𝑎1 , 𝑎2 ], [𝑏1 , 𝑏2 ], [𝑐1 , 𝑐2 ], . . . , 𝑥𝑘,𝑙 }

The “snnmfilter.m” function accepts an input image and the height and width of a
rectangular structuring element (7x7 in this case). The output is the symmetric NN
mean filtered image.

To study the nonlinear filtering, the histograms of the filtered image will be
determined. This provides the gray level intensity distribution throughout the image.
These are calculated using the “histo.m” function. The histo.m function accepts the
input image and the number of bits for the amount of gray levels. This function counts
the number of pixels of each gray level, in this case there are 256 gray levels. For this
project, the histogram is divided by the number of pixels in the image to normalize the
histogram.
To execute the nonlinear filtering, the user should run the “main1.m” script in the
MATLAB directory. Below is the general hierarchy of the flow of part 1.

main1.m

medfilt2.m alphatrim.m sigmafilter.m snnmfilter.m histo.m

ImDisk{M,A,S,N}1
ImDiskM1 ImDiskA1 ImDiskS1 ImDiskN1
_hn

ImDisk{M,A,S,N}5
ImDiskM5 ImDiskA5 ImDiskS5 ImDiskN5
_hn

ImCWNoise
ImCWNoiseM1 ImCWNoiseA1 ImCWNoiseS1 ImCWNoiseN1
{M,A,S,N}1_hn

ImCWNoise
ImCWNoiseM5 ImCWNoiseA5 ImCWNoiseS5 ImCWNoiseN5
{M,A,S,N}5_hn
C. Results

Part 1

Figure 1 – Original “disk.gif” image

Figure 2 – Normalized histogram of “disk.gif” image.


Figure 3 – Original “cwheelnoise.gif” image

Figure 4 – Normalized histogram of “cwheelnoise.gif” image

Figures 1 to 4 shows the original images used for the nonlinear filtering and their
respective normalized hisograms. The following figures 5 to 8 shows the 7x7 medium
filter implementation and the following histograms for one and five iterations on the
“disk.gif” image.

Figure 5 – One iteration of 7x7 median filter on “disk.gif” image

Figure 6 – Normalized histogram of one iteration of 7x7 median filter on “disk.gif”


image
Figure 7 – Five iterations of 7x7 median filter on “disk.gif” image

Figure 8 – Normalized histogram of five iterations of 7x7 median filter on “disk.gif”


image

The 7x7 median filter is good at preserving edges and structure, while reducing the
standard deviation of the noise around the gray levels of interest per the disks. The
median filter effectively removes the salt and pepper noise in the image. As the
median filter is iterated, the appendages on the lightest disk begin to be removed.

The original image has a mean of 161.0478 and standard deviation of 68.969. After
one iteration, the mean of the brightest and largest disk is 160.0694 and the standard
deviation is 63.3736. After five iterations, the mean becomes 159.4788 and the
standard deviation becomes 63.7434. The median filter improves the standard
deviation; however, it lowers the average gray level value in the disks image. The
other filters have shown to preserve the mean value of the gray level while improving
the standard deviation.

Figures 9 to 12 shows the 7x7 medium filter implementation and the following
histograms for one and five iterations on the “cwheelnoise.gif” image.

Figure 9 – One iteration of 7x7 median filter on “cwheelnoise.gif” image


Figure 10 – Normalized histogram of one iteration of 7x7 median filter on
“cwheelnoise.gif” image

Figure 11 – Five iterations of 7x7 median filter on “disk.gif” image


Figure 12 – Normalized histogram of five iterations of 7x7 median filter on
“disk.gif” image

For the cwheelnoise image, the 7x7 median filter preserved shape well, but began to
blur with small details such as the inner “spokes” on the image. The median filter was
effective at removing salt and pepper noise from the image. The larger areas retain
the structure around the outside of the image. The median filter preserves and
emphasizes the peak from the noise at the unique gray level values which were
present in the cwheelnoise image before being corrupted with noise.

Figure 13 to 16 shows the 7x7 alpha-trimmed mean filter implementation and the
following histograms for one and five iterations on the “disk.gif” image.
Figure 13 – One iteration of 7x7 alpha-trimmed mean filter on “disk.gif” image

Figure 14 – Normalized Histogram of one iteration of 7x7 alpha-trimmed mean filter


on “disk.gif” image
Figure 15 – Five iterations of 7x7 alpha-trimmed mean filter on “disk.gif” image

Figure 16 – Normalized histogram of five iterations of 7x7 alpha-trimmed mean filter


on “disk.gif” image

The 7x7 alpha-trimmed mean filter reduces the standard deviation of the noise around
the interested gray level values. It is effective at removing salt and pepper noise.
Since this is a mean filter, the noise removal in the darkest region, increases the gray
level very slightly. The structure begins to blur with more iterations as the alpha-
trimmed mean filter behaves as a low pass filter attenuating high frequency
components. After one iteration, the mean of the image is 160.3150 and the standard
deviation is 68.9694. After five iterations, the mean of the image is 159.5644 and the
standard deviation is 61.7438. This filter is similar to the median filter in that it
slightly reduces the average gray level over the largest brightest disk in the image.
Also, this filter reduces the standard deviation the most as iterations occur in
comparison to all filters.

Figure 17 to 20 shows the 7x7 alpha-trimmed mean filter implementation and the
following histograms for one and five iterations on the “cwheelnoise.gif” image.

Figure 17 – One iteration of 7x7 alpha-trimmed mean filter on “cwheelnoise.gif”


image
Figure 18 – Normalized Histogram of one iteration of 7x7 alpha-trimmed mean filter
on “cwheelnoise.gif” image

Figure 19 – Five iterations of 7x7 alpha-trimmed mean filter on “cwheelnoise.gif”


image
Figure 20 – Normalized Histogram of five iterations of 7x7 alpha-trimmed mean
filter on “cwheelnoise.gif” image

The 7x7 alpha-trimmed mean decreases the noise standard deviation with iterations. It
is effective at removing salt and pepper noise. However, unintentional gray level
values (“harmonics”) starts to form in the histogram between the intended gray level
values. The alpha-trimmed filter is blurring the structure of the cwheelnoise image as
it contains small details, edges, and higher frequencies components. Unlike the
median filter, each gray level peak is more rounded and does not include the
“impulse” at the middle of each gray level grouping.
Figure 21 – One iteration of 7x7 sigma filter on “disk.gif” image

Figure 22 – Normalized Histogram of one iteration of 7x7 sigma filter on “disk.gif”


image
Figure 23 – Five iterations of 7x7 sigma filter on “disk.gif” image

Figure 24 – Normalized Histogram of five iterations of 7x7 sigma filter on “disk.gif”


image

Figures 21 to 24 show the 7x7 sigma filter results and their respective histograms for
one and five iterations on the “disk.gif” image. After one iteration, the noise standard
deviation decreased slightly. The edges and the structure, in general, were well
preserved. After five iterations, the sharpening is very noticeable in the histogram.
We can see in Figure 23 that the edges are still very well preserved and most of the
noise has been removed. The majority of the remaining noise is salt and pepper
noise, which is difficult to remove with a sigma filter. Although the edges are sharp
(in terms of a lack of blurring), they are jagged and not as well rounded as the original
image. If a filter was used to remove the salt and pepper noise beforehand, the output
of the sigma filter after 5 iterations would be even cleaner.

After one iteration, the mean of the interior pixels in the large disk region was
calculated to be 171.2259, which is about 10 values brighter than the original image.
The standard deviation was calculated to be 62.8712, which is 6 values less than the
original image. After five iterations, the mean of the interior pixels in the large disk
region was calculated to be 175.1404, which is about another 5 values brighter after
the first iteration. The standard deviation continued to decrease to 55.5526, another 7
values less than after the first iteration. In general, the sigma filter slightly raised the
mean and significantly dropped the standard deviation of the interior pixels in the
large disk region.

Figures 25-28 show the 7x7 sigma filter results and their respective histograms for
one and five iterations on the “cwheelnoise.gif” image. Once again, after one
iteration, the noise standard deviation only decreased slightly (and the filtered image
only looks slightly different than the original image). The edges and local structure
were very well preserved. After five iterations, the edges were pretty well preserved
and the noise was significantly reduced. The insides of the spokes in the wheel were
subject to some blurring on the edges, but overall, much better than a standard mean
filter. This image more prominently shows the sigma filter’s inefficacy against salt
and pepper noise, which is still sprinkled on the image after 5 iterations of the filter.
Even with more iterations, the salt and pepper noise would continue to be there.
Figure 25 – One iteration of 7x7 sigma filter on “cwheelnoise.gif” image

Figure 26 – Normalized Histogram of one iteration of 7x7 sigma filter on


“cwheelnoise.gif” image
Figure 27 – Five iterations of 7x7 sigma filter on “cwheelnoise.gif” image

Figure 28 – Normalized Histogram of five iterations of 7x7 sigma filter on


“cwheelnoise.gif” image
Figure 29 – One iteration of 7x7 symmetric nearest neighbor mean filter on
“disk.gif” image

Figure 30 – Normalized Histogram of one iteration of 7x7 symmetric nearest


neighbor mean filter on “disk.gif” image
Figure 31 – Five iterations of 7x7 symmetric nearest neighbor mean filter on
“disk.gif” image

Figure 32 – Normalized Histogram of five iterations of 7x7 symmetric nearest


neighbor mean filter on “disk.gif” image
Figures 29 to 32 show the 7x7 symmetric nearest neighbor filter results and their
respective histograms for one and five iterations on the “disk.gif” image. After one
iteration, the noise standard deviation decreased slightly. The edges and the structure,
in general, were well preserved. After five iterations, the sharpening is very
noticeable in the histogram. We can see in Figure 31 that the edges are still very well
preserved and most of the noise has been removed. Unlike the sigma filter, the
filtered image in Figures 29 and 31 were not still corrupted by salt and pepper noise.
Although the edges are sharp (in terms of a lack of blurring), they are jagged and not
as well rounded as the original image. There is slight intensity variation inside the
circles. If more iterations were run, this variation would continue to decrease.

After one iteration, the mean of the interior pixels in the large disk region was
calculated to be 160.7654, which is about the same as the original image (unlike the
sigma filter). The standard deviation was calculated to be 65.2902, which is 3 values
less than the original image. After five iterations, the mean of the interior pixels in
the large disk region was calculated to be 161.0524, which very slightly larger than
after one iteration. The standard deviation actually increased to 66.2698, just one
value larger. In general, the symmetric nearest neighbor mean filter keeps the mean
constant and drops the standard deviation of the interior pixels in the large disk
region.

Figures 33-36 show the 7x7 symmetric nearest neighbor mean filter results and their
respective histograms for one and five iterations on the “cwheelnoise.gif” image.
Once again, after one iteration, the noise standard deviation only decreased slightly
(and the filtered image only looks slightly different than the original image). The
edges and local structure were very well preserved. After five iterations, the edges
were pretty well preserved and the noise was significantly reduced. Unlike the sigma
filter, the insides of the spokes in the wheel remained sharp. As with the disk image,
the image exemplifies the property that the symmetric nearest neighbor filter works
well with salt and pepper noise.
Figure 33 – One iteration of 7x7 symmetric nearest neighbor mean filter on
“cwheelnoise.gif” image

Figure 34 – Normalized Histogram of one iteration of 7x7 symmetric nearest


neighbor mean filter on “cwheelnoise.gif” image
Figure 35 – Five iterations of 7x7 symmetric nearest neighbor mean filter on
“cwheelnoise.gif” image

Figure 36 – Normalized Histogram of five iterations of 7x7 symmetric nearest


neighbor mean filter on “cwheelnoise.gif” image
D. Conclusions

In Part 1, we learned about the performance of the median, alpha-trimmed mean,


sigma, and symmetric nearest neighbor mean filters. The median filter was effective
at reducing noise and removing salt and pepper noise. It was effective at preserving
shape, but began to lose accuracy at the smaller details. The alpha-trimmed mean
filter was effective at reducing noise and removing salt and pepper noise. As it is an
averaging filter, it attenuates high frequency components so blurring occurred along
edges. The sigma filter proved to keep local structure and edges intact in filtering out
most of the noise. However, it did not perform well in filtering out salt and pepper
noise. The degree at which it can filter out high/low intensity noise will depend on
the sigma value chosen. The symmetric nearest neighbor mean filter performed
similarly to the sigma filter, but also performed well against salt and pepper noise.
Given that the symmetric nearest neighbor mean filter does not require a
hyperparameter like the sigma filter (the sigma filter requires the sigma value), we
would generally choose the symmetric nearest neighbor mean filter on natural
images.

While studying the largest and brightest disk in the disks image, the sigma and
symmetric nearest neighbor mean filters reduced the standard deviation of the noise
while keeping the mean relatively unchanged. The median and alpha-trimmed mean
filters reduced the standard deviation as well, but reduced the mean slightly. The best
filter at reducing the standard deviation of the noise is the alpha-trimmed mean filter,
with the second best being the sigma filter.

Potrebbero piacerti anche