Sei sulla pagina 1di 12

CSE585/EE555: Digital Image Processing II

Computer Project #1:

Mathematical Morphology: Hit-or-Miss Transform

Timothy Hackett, Kenrick Dacumos, Matthew McTaggart

Date: 01/27/2017

A. Objectives

This project had a couple of objectives. The first objective was to gain experience using MATLAB and
some image processing related functions. The second objective was to apply our fundamental
knowledge of erosion, dilation, opening, and closing to design a hit-or-miss transform.

B. Methods

The code for project 1 was functionally divided into six different sections. Below are the details for
each of these sections:

1. Read in image
a. This section is standard. Here, we use a known Matlab function, imread, to load the
.jpg file. For our computations in this project no data type conversion is necessary.
The size of the image was also recorded.
2. Binary-threshold image
a. Here, the original .jpg grayscale image is thresholded so that we obtain a binary
image. This is done by the following:
i. Assign threshold value (found by trial and error)
ii. Compare each pixel in the image to the thresholded value. If above then pixel
= 255. If below pixel = 0.
3. Remove noise
a. The original image contains noise which propagates through after we threshold the
image (salt and pepper noise). We use opening and closing operations to remove
noise in the following way:
i. Generate structuring element which has the shape of a circle and a pixel radius
of 1. This structuring element was chosen to be approximately as large as the
largest pepper noise observed in the image (with trial-and-error used).
ii. Generate another structuring element which has the shape of a circle and a
pixel radius of 3. This structuring element was chosen to be approximately as
large as the largest pepper noise observed in the image (with trial-and-error
used).
iii. Perform opening using the first structuring element.
iv. Perform closing using the second structuring element.
4. Use hit-or-miss transform to extract largest disk
a. For the hit-or-miss transform, there needs to be two different structuring elements.
i. The first structuring element has to be the same size as the largest disk in the
image. After analyzing the image, the largest disk found had a pixel radius of
32. To account for imperfections in the structuring element or the foreground
circles, the first structuring element had a pixel radius of 31 (slightly smaller)
and was the shape of a disk.
ii. The second structuring element must be the complement of the first
structuring element. However, after analyzing the image it was observed that
the disks sometimes had imperfect edges around its borders. To alleviate this
problem the second structuring element had a size slightly larger than the first
structuring element (pixel radius of 33) and the shape of a disk. The radius 33
was chosen by trial and error. This structuring element was then inverted to
obtain the complement.
b. The hit-or-miss transform was then applied as follows:
i. Erode the input image with the first structuring element
ii. Dilate the input image with the second structuring element
iii. Find the complement of the output from step ii.
iv. Find the intersection between the erosion from step i. and the complement
from step iii.
5. Use hit-or-miss transform to extract smallest disk
a. This hit-or-miss transform also needs two different structuring elements.
i. The first structuring element has to be the same size as the smallest disk in the
image. After analyzing the image, the smallest disk found had a pixel radius
of 9. To account for imperfections in the structuring element or the
foreground circles, the first structuring element had a pixel radius of 8
(slightly smaller) and was the shape of a disk.
ii. The second structuring element must be the complement of the first
structuring element. However, after analyzing the image it was observed that
the disks sometimes had imperfect edges around its borders. To alleviate this
problem the second structuring element had a size slightly larger than the first
structuring element (pixel radius of 10) and the shape of a disk. The radius 10
was chosen by trial and error. This structuring element was then inverted to
obtain the complement.
b. The hit-or-miss transform was then applied the same way as in step 4b.
6. Analysis of having salt and pepper noise in Hit-or-Miss transform
Run procedure:
1. Ensure ‘RandomDisks-P10.jpg’ is in the same directory as the main.m function.
2. Open main.m
3. Click “Run” in Matlab

Below is the general hierarchy of the flow of the project and the tasks performed by each function.

main.m

generate_disk_
hit_or_miss_tr
threshold.m closing.m opening.m structuring_ele
ansform.m
ment.m

Pepper Smallest Disk Smallest Disks


Binary-Valued Salt Removal
Removal SE Extraction

Largest Disks
Largest Disk SE
Extraction

Analysis of
HorM with
Noise

Figure 1 – General hierarchy of the flow of the project.


C. Results

Figure 2 – Original “RandomDisks-P10” image (size 808x636).

Figure 2 shows RandomDisks image corrupted with 10% salt and pepper noise. Before processing,
the image was threshold to a binary image. Figure 3 shows the binary threshold image.
Figure 3 – “RandomDisks-P10” after thresholding (size 808x636).

Figure 3 is thresholded so that the background and foreground pixels are gray level intensity values
255 and 0, respectively. Because of thresholding, much of the salt and pepper noise was eliminated
in both the foreground and background elements. This is because the pixel values were only given
two valid intensity values, so many of the darker grays inside the disks changed to black and many
of the lighter grays in the background changed to white. To further clean up the image of the salt
and pepper noise, opening followed by closing was used. The opening operation removed the
pepper noise in the background, while the closing operation removed the salt noise in the foreground
disks. Figure 4 and Figure 5 show the structuring elements used for opening and closing,
respectively. Disks were used as structuring elements as the foreground elements are disks. We had
tried using squares/rectangles, but small rectangle artifacts showed up on some of the edges of some
of the disks. From some trial-and-error, we found that using a structuring element of radius 1
worked best for opening, while a structuring element of radius 3 worked best for the closing. For the
background, the approximate circular shape is not as important for removing the pepper noise, which
is why a radius of 1 worked fine. For the foreground, the circular shape is more important (as the
disks are circular), so a larger structuring element was used to get a better approximate disk shape.
Figure 6 shows the de-noised image after opening and closing.
Figure 4 – Opening structuring element (size 3x3). Figure 5 – Closing structuring element (size 7x7)

Figure 6 – Denoised “RandomDisks-P10” after thresholding and opening/closing (size 808x636).

Looking at result of opening and closing in Figure 6, all of the salt and pepper noise was eliminated
in the foreground and background, respectively. With the noise eliminated, the hit-or-miss transform
could now be used to find the smallest and largest disks. Disk structuring elements were used for the
hit-or-miss transform. To find a disk of a certain radius, the “A” structuring element was chosen to
be a disk with a radius slightly smaller than the disk we’re trying to find. The “B” structuring
element was chosen to be the complement of a disk with a radius slightly larger than the disk we’re
trying to find. The “A” structuring element is used with erosion, so it eliminates any disk with
radius smaller than its radius. The “B” structuring element is used with dilation, so it eliminates any
disk with radius larger than its radius. “A” and “B” differ slightly in radius to account for non-ideal
shaped disks in the image to account for the fact that disks of the “same” diameter may actually be a
pixel or larger or smaller because of digitization and any artifacts left from the opening/closing
operations. Although computationally expensive, disks were used because the objects in the image
were disks.

To find the maximum sized disks, we used disk structuring elements of radius 31 (“A”) and radius
33 (“B”) pixels shown in Figures 7 and 8, respectively.

Figure 7 – “A” structuring element for hit-or-miss Figure 8 – “B” structuring element for hit-or-miss transform
transform for finding the largest disks (size 63x63). for finding largest disks (size 67x67).

Similarly, to find the minimum sized disks, we used disk structuring elements of radius 8 (“A”) and
radius 10 (“B”) pixels shown in Figures 9 and 10, respectively.

Using the structuring elements in Figures 7 and 8 in the hit-or-miss transform resulted in finding the
seven largest disks in the original image (shown in Figure 11). Because the two structuring elements
were not exactly the same size, some locations have a cluster of points for the location of the largest
disk (as opposed to the ideal single point). An arithmetic mean of the cluster of points provides the
actual location of the disk centers. Similarly, using Figure 9 and 10 in the hit-or-miss transform
resulted in finding the five smallest disks in the original image (shown in Figure 12). Once again,
averaging of the cluster of points results in the actual disk center locations.
Figure 9 – “A” structuring element for hit-or-miss Figure 10 – “B” structuring element for hit-or-miss transform
transform for finding the smallest disks (size 17x17). for finding smallest disks (size 21x21).

Figure 11 – “RandomDisks-P10” after thresholding, denoising, and hit-or-miss transform using Figures 7/8 for
structuring elements (size 808x636). The red-circled points are the locations of the largest disks in the original image.
Figure 12 – “RandomDisks-P10” after thresholding, denoising, and hit-or-miss transform using Figures 9/10 for
structuring elements (size 808x636). The red-circled points are the locations of the smallest disks in the original image.

After performing the hit-or-miss transform with the denoised image, we wanted to see if the
transform would work with the non-denoised image. In general, no, with salt-and-pepper noise
added, the hit-or-miss transform will not find all of the smallest/largest disks. This is because of the
salt noise on the foreground disks will cause the transform to not “hit” the disks. That being said, for
the disks that don’t any salt noise, then the hit-or-miss transform will find the disk. Furthermore,
because we use a range of radii that the hit-or-miss transform will “hit”, if the salt noise is close to
the edge of the disk, then the disk may be “hit” still. To make the transform more robust to salt
noise, the range of radii between the structuring elements can be increased. Increasing the range of
radii increases the chance of “hitting” a corrupted disk, but also increases the risk of “hitting” a disk
of a different size radius altogether (a sizing compromise). For disks that have any salt noise closer
to the center of the disk (the larger disks), then the hit-or-miss transform is not going to work.

The following figures show some examples of showing the transform working or not. Figure 13
shows what happens when the hit-or-miss transform is used to find the smallest disks when “A” has
a radius of 8 and “B” has a radius of 11. The transform was able to find four of the five disks.
When we expand the range of radii of “A” and “B” to 7 and 11 pixels, the transform is able to find
all five disks (Figure 14) because the noise on the fifth disk was near the edge.
Figure 13 – “RandomDisks-P10” after thresholding and hit-or-miss transform using disk structuring elements of radii 8
and 11 pixels for “A” and “B”, respectively (size 808x636). The red-circled points are the locations of the smallest disks
found using the transform in the original image.

Figure 14 – “RandomDisks-P10” after thresholding and hit-or-miss transform using disk structuring elements of radii 7
and 11 pixels for “A” and “B”, respectively (size 808x636). The red-circled points are the locations of the smallest disks
found using the transform in the original image.
When trying to find the largest disks, the transform could not find any of the disks because of salt
noise was all over the disks and not just near the edges. Figure 15 shows the result of the hit-or-miss
transform using structuring elements of radii 31 and 33.

Figure 15 – “RandomDisks-P10” after thresholding and hit-or-miss transform using disk structuring elements of radii 31
and 33 pixels for “A” and “B”, respectively (size 808x636). The red-circled points are the locations of the largest disks
found using the transform in the original image.

The blank image above shows the hit-or-miss transform failed to find any of the disks. Increasing
the range of the radius would not help.

D. Conclusions

The original “RandomDisks-P10” image was successfully processed using morphological image
processing to find the smallest and largest disks using a thresholding, de-noising, and hit-or-miss
transforms. This project explored the hit-or-miss transform which can be very effective for finding
objects in an image, given that the image is de-noised. If the image noise cannot be removed on the
foreground elements, the hit-or-miss transform will show mixed results depending on the size and shape
of the foreground elements. The transform is also only useful if the size of the foreground elements is
known. If the shape is known but not the size, potentially a series of hit-or-miss transforms could be
used with varying sized structuring elements.
From the relatively long length of time it took to compute the result of the hit-or-miss transform,
optimizations in computing erosion and dilation should be used, such as using a series of smaller
structuring elements instead of one large one or breaking up the image into sub-images and working on
each image in parallel. In conclusion, this project helped the team really understand the operations of
erosion, dilation, opening, closing, and hit-or-miss transform as well as how to design structuring
elements for these operations.

Potrebbero piacerti anche