The project can be subdivided
into two parts:
1. Input image
processing.
2. Applying distance
formula to compare objects.
Input image processing:
Image processing part implies the
following functionality:
ü Read "*.jpg" file
which contains an object of interest (apple or orange).
ü Identify round
objects within the image.
ü Crop identified
object (apple or orange) into a separate image (the new image will be a
rectangle of the smallest area containing the object of interest).
ü Resize the image obtained
in the result of the previous step to 1000 by 1000 pixels.
ü Create an "average"
object ("average apple" or "average orange") by reading a set of images of the
same type of object (of apples or oranges), preprocessing them as described
above and using image arithmetic routines for images addition and division by
an integer number.
The described steps are mainly
encapsulated in matlab functions identify_round.m
and average.m.
Implementation comments.
Function "identify_round.m":
Receives two parameters:
- Initial image data in unit8
matrix (each matrix cell consists of (R,G,B) vector); and
- Technical parameter for
object roundness metric (from 0 to 1) according to the measure introduced in "Identifying Round
Objects" demo.
The code of this function is
based on the algorithm described in "Identifying Round
Objects" demo with the following modifications:
Ø Automatic set up
for threshold parameter returned by graythresh
function. It helps to prevent situations when the initial threshold value
returned by graythresh is not high enough
to distinguish separate round objects, e.g. round objects can be consolidated
with a piece of background (because of a background light/shadows) and lose
their "roundness" after using im2bw
function. The program increases the
threshold value by .05 either until a round object is found or the threshold
value is more/equal than 1.
Ø When computing an
estimate of an object perimeter use:
delta_sq = diff(stats(k).ConvexHull).^2;
instead of (introduced
in "Identifying Round
Objects" demo)
delta_sq
= diff(boundary).^2;
That will make the
estimate more universal by removing possible errors given by oscillations on a
boundary.
Ø In case there are
several round objects found - choose the one with the largest area.
Ø Set error message
if no round object was found.
Ø Set the identified
round object into original color.
Ø Crop and resize the
identified round object.
Function "average.m":
Receives two parameters:
- Array of *.jpg image files
names (5 in this problem); and
- Technical parameter for
object roundness metric (from 0 to 1) according to the measure introduced in "Identifying Round
Objects" demo (to be passed to identify_round function).
The purpose of the
function is to create an "average" object ("average apple" or "average orange")
based on the five provided sample images of apples/oranges (actually, an
"average" object can be created by any number of sample images, for this
exercise only 5 samples are considered).
Further on, the input
image will be 1) preprocessed and then 2) approximated by one of the "average"
objects (by an "average apple" or "average orange"). Based on the errors of
approximation the final conclusion will be made. The distance formula is
introduced below.
Note: Sample images of apples/oranges are supposed
to be in:
"~root\samples\apples" or "~root\samples\oranges" .
Images of "average" objects:


Applying distance formula to
compare objects:
The following distance formula
is introduced to compare preprocessed apples and oranges:
,
where
,
- normalization
factor.



is number of pixels,
is value of component
in the RGB
description of pixel number
on image
.
The implementation of the
formula can be found in matlab function distance.m
Some distance values
(examples):
|
|
apples.a1 |
apples.a2 |
apples.a3 |
apples.a4 |
apples.a5 |
AVAP |
AVOR |
|
apples.a1 |
0 |
0.162 |
0.0995 |
0.2068 |
0.1750 |
0.0928 |
0.1743 |
|
oranges.o1 |
0.2135 |
0.2436 |
0.2163 |
0.2815 |
0.2598 |
0.2257 |
0.1003 |
The input image is first
approximated by an "average orange". If an error of approximation is less than q = .12
then it is an orange, else the input image is approximated by an "average apple".
Here, if an error of approximation is less than q = .12 then it is an apple.
Otherwise, the object under consideration is neither an apple nor an orange.
This logic is provided in
driver function driver.m .