1. Elijah Willie
February 16, 2015
301193627
Introduction:
In this document I will attempt to devise an algorithm that will be able to efficiently identify
the location of cigarette butts in an image. This algorithm uses segmentation techniques,
enhancement techniques and coloring techniques all present in matlab to aid in locating the
cigarette butts. This algorithm mainly aims to turn the original image to binary image which
is segmented according to color.
Image Processing
The test image(s)
This algorithm (procedure) is intended to create a binary image/mask from the original which is
segmented according to color of the cigarette butts and the edges present in the image which
will prove to be useful later on to sketch out the shapes of the objects still present in the image.
1.1 Color Segmentation and Edge Detection.
For each pixel, if its color is close enough to the target color, take it. The distance is
defined as: Color = ((R-a).^2+(G-b).^2+(B-c).^2).^(1/2) suppose the target color is
(a,b,c). Since butts commonly are white, tan, or both in color, I have designated
them as follows:
White = (255,255,255),
Tan = (191, 153, 78)
2. Result of Tan filter:
Result of White Filter:
BW2 = (BW < 40) | (BW1 < 40);
Next we further segment the image by selecting only regions of the segmented images with
intensity values below a certain threshold. In this case I chose 40.
3. Result after threshold segmentation:
As you can see, most of the background noise has been eliminated by the color segmentation
but still some remains.
[~, threshold] = edge(I, 'prewitt');
This array stores all the edges in the image I and the threshold value given by the prewitt filter
after it has been applied to the image. We can now create a binary mask of the image.
BinaryMask = edge(I,'prewitt', threshold)
If the image has a lot of clutter (noise) in the background, it can’t be avoided that the new
BinaryMask will also have those noisy edges in the back ground.
The Binary Mask after prewitt filter has been applied:
4. As you can see, the edges of the target object has been signaled out. However there is still
some background noise. It has to be minimized as much as possible before trying to enhance
the image. By using the ‘bwareaopen’ function in matlab, it is possible to reduce some of the
noise in the image background by removing all connected objects that fever than a set amount
of pixel. In this case I arbitrarily chose 110 pixels. I also used the ‘imclearborder’ function to
help kill some more noise.
1.2 Binary Image Enhancement
The below processes are done to help smoothen out the image by filling in gaps
between the edges, dilating the edges for a more rigid shape and assigning arbitrary
colors to the various edges for easier detection.
se90 = strel('line', 3, 90); %vertical element.
se0 = strel('line', 3, 0);%horizontal element.
Maksdilated = imdilate(BinaryMask, [se90 se0]);
The strel function creates vertical and horizontal structuring elements that is passed
to the imdilate function to dilate the edges in the binary image.
Result of Dilation:
Maskfilled = imfill(Maksdilated, 'holes');
If the Binary image has any apparent holes between the edges, the imfill function
fills those hole. In the case of this test image, none of the holes were filled, but it
does not cause enough distraction to render the algorithm useless. The algorithm
would function just fine with or without it. I have decided to leave it in.
5. Masknoborders = imclearborder(Maskfilled, 8);
Masknoborders = bwareaopen(Masknoborders,110);
These function are applied to clear any edges connected to borders in the binary
image. For some images this effectively removes most of the noise only leaving the
cigarette butts with a small amount of background noise.
Result of the previous two functions:
Compared to the previous image, most of the background noise has been eliminated.
We now proceed to again use the strel function to create a diamond structure that will be used
to smoothen out the newly formed binary mask by the imerode function.
seDiamond = strel('diamond',1); %creates a structuring diamond element.
FinalMask = imerode(Masknoborders,seDiamond); %Smoothes out the image.
6. Result of Diamond smoothing:
Next, the objects (egdes) in the final image are labelled by using random colors to help better
visualize the remaining objects and noise present in the image.
labeledImage = bwlabel(Masknoborders, 8);
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
The bwlabel function labels all the objects (edges) within in the image so that the label2rgb can
assign (in this case) random colors to the labels. It just helps for better visualization.
Result of colored labelled image:
As you can see, the apparent objects (edges) have been labelled with random colors.
7. 2. Final Image
After the image has been processed with the segmentation and enhancement
techniques, all that remains is to output the final image with some sort of signal of
where the objects of interest are in the image. I have not yet managed to effectively
filter out most of the back ground noise as it will be apparent in the test results for the
image. However, I have been able to identify a great majority of the objects of interests
even if they are amongst noisy backgrounds.
I used a loop to iterate over all the edges in the images and highlight them out in blue
including the objects of interest and excess noise present.
BlobBoundaries = bwboundaries(Masknoborders);
Bound_Num = size(BlobBoundaries);
for x = 1 : Bound_Num
thisBoundary = BlobBoundaries{x};
plot(thisBoundary(:,2), thisBoundary(:,1), 'b', 'LineWidth', 2);
end
Test Results:
8.
9.
10. 3. Conclusions
From the test results, it is clear that more work needs to be done to help eliminate the
unwanted noise in the image. I have not yet found a method to do so. I need to take
the shape of the butts into account as I try to minimize the noise. All the methods tried
so far yielded similar results and this was the best of all the possible test results. Maybe
putting a constraint on rectangularity might help to decrease the noise problem.
For now my algorithm will work well for images that do not have a lot of clutter in the
background which will produce noise to distract from the object of interest. If the
background has a lot of clutter, this algorithm will do a decent job of identifying the
butts amongst the noisy background.
This algorithm is limited by how clustered the image background is, and how many
white, tan or both colored objects are present in the image.