Вы находитесь на странице: 1из 40

1.

Central Electronics Engineering Research Institute (CEERI)


Central Electronics Engineering Research Institute (CEERI) is a
part of the Council of Scientific and Industrial Research (CSIR) and is committed to
research and developments in electronics to meet the requirements of its customers.
CEERI has been continually identifying the thrust areas of key National/Industrial
relevance and has been developing competitive technologies with a goal to achieve
excellence and self-reliance in these areas.
CEERI Centre Chennai, a pioneering Institute in the field of
Quality Control Instrumentation for process industries, is a regional Centre of Central
Electronics Engineering Research Institute (CEERI), Pilani Rajasthan. The center has
a rich experience and expertise in this field and has developed many online
monitoring systems for various process industries like pulp and paper, plastics, food
processing, leather, etc.,
CEERI concentrates on indigenous development of special
sensors and systems suited to the online measurement and control and these involve
varying technologies such as near infrared gauging, beta gauging, optical,
electromechanical methods and the currently emerging image processing techniques.
Presently the center has been extensively contributing its expertise for fruit sorting.

















1
2. INTRODUCTION

IMAGE PROCESSING is one of the most stimulating research
areas under both the scientific and technology perspective in the present millennium.
Many research organizations have started involving themselves in some aspect of
image processing and recently emphasis has been laid on making it realtime. Our
project is a part of effort by CEERI,Chennai to develop fruit-sorting and grading
system.
In India, manual inspection techniques are widely used for sorting
foodstuffs. The bruises are detected in India by looking for discolored tissues or dents
in the check. In order to check for internal defects, fruits from each batch are
randomly chosen and cut open. These methods are not reliable and could lead to large
losses.
Machine vision techniques, which when used in conjunction with
manual inspection would guarantee international food quality and there by help in
improving Indian economy through export.
Most of the time the importer provides the specifications, normally
for machine sorted and packed products. Due to this Indian exporters find it very
difficult to make the employees involved in manual sorting understand the exact
requirements. Further, in manual sorting, it is impossible to maintain uniform sorting
as each person involved in the sorting will not take identical decisions if the sorting is
based on color, shape and size etc.
With all these things in mind, the government of our country is
putting more emphasis on value added agriculture product exports.

2.1 SCOPE OF THE PROJECT
The principle steps in the project include reading the image ,
converting the image into a binary image and extracting the feature such as major and
minor axes of the mango.
These steps are done using Image Processing Toolbox in Matlab,
where in the functions for reading, processing, writing, analyzing, etc., are already
present. Our project mainly aims to find the minor axis of the mango, which is the
measure for the size of the mango.
2
3. IMAGE PROCESSING

3.1 INTRODUCTION
Digital Image Processing refers to processing the digital images by means of digital
images. Digital Image Processing can be divided into three types of processes:
Low Level Processes
Medium Level Processes
High Level Processes
Low Level Processes These involveprimitive processes such as image processing to
reduce the noise, contrast enhancement, and image sharpening. Low-level processes
are characterized by the fact that both inputs and outputs are images.
Medium Level Processes These involve task such as segmentation (portioning of
image into regions or objects) and classification of these objects. Mid-level processes
are characterized by the fact that inputs generally are images, but outputs are the
attributes of these images.
High Level Processes These involve making sense of an ensemble of recognized
objects, as in image analyses and at the far end performing the functions relates to
human vision.

3.2 IMAGE REPRESENTATION
An image may be defined as a two-dimensional function, f(x, y),
where x and y are spatial (plane) coordinates, and the amplitude of f at any pair of
coordinates(x, y) is called the intensity or gray level of the image at that point.

When x, y, and the amplitude values of f are all finite, discrete
quantities, we call the image a digital image. The field of digital image processing
refers to processing digital images by means of a digital computer.




3
PIXEL
A digital image is composed of a finite number of elements, each of which has a
particular location and value. These elements are referred to as picture elements,
image elements, pels, and pixels.

3.2.1 FUNDAMENTAL STEPS IN DIGITAL IMAGE PROCESSING

All the methodologies that can be applied to images for different purposes and
possibly with different objectives are as follows:

Image acquisition Compression
Image enhancement Morphological processing
Image restoration Segmentation
Color image processing Representation & description
Wavelets and multiresolution
processing
Object recognition

Image Acquisition: This stage involves preprocessing, such as scaling. This can be as
simple as being given an digital image.

Image Enhancement: The idea is to bring out detail that is obscured, or simply to
highlight certain features of interest in an image. It is based on human subjective
preferences regarding what constitutes a good enhancement result.

Image Restoration: This deals with improving the appearance of an image.
However, unlike enhancement, which is subjective, image restoration is objective, in
the sense that restoration techniques tend to be based on mathematical or probabilistic
models of image degradation.

Color Image Processing: Color is used as the basis for extracting features of interest
in an image. It has been gaining in importance because of the significant increase in
the use of digital images over the Internet.

4
Wavelets: These are the foundation for representing images in various degrees of
resolution.

Compression: It deals with techniques for reducing the storage required to save an
image, or the bandwidth required to transmit it. Although storage technology has
improved significantly over the past decade, the same cannot be said for transmission
capacity.

Morphological processing: It deals with tools for extracting image components that
are useful in the representation and description of shape.

Segmentation: This procedures partition an image into its constituent parts or objects.
In general, autonomous segmentation is one of the most difficult tasks in digital image
processing. A rugged segmentation procedure brings the process a long way toward
successful solution of imaging problems that require objects to be identified
individually. On the other hand, weak or erratic segmentation algorithms almost
always guarantee eventual failure. In general, the more accurate the segmentation, the
more likely recognition is to succeed.

Representation and description : This almost always follow the output of a
segmentation stage, which usually is raw pixel data, constituting either the boundary
of a region (i.e., the set of pixels separating one image region from another) or all the
points in the region itself. Description, also called feature selection, deals with
extracting attributes that result in some quantitative information of interest or are
basic for differentiating one class of objects from another.

Recognition: This is the process that assigns a label (e.g., vehicle) to an object
based on its descriptors.

3.2.2 REPRESENTATION OF DIGITAL IMAGE
If an image f(x, y) is sampled so that the resulting digital image has
M rows and N columns. The values of the coordinates (x, y) now become discrete
quantities. Integer values are used to represent these discrete coordinates. Thus, the
values of the coordinates at the origin are (x, y)=(0, 0). The next coordinate values
5
along the first row of the image are represented as (x, y)=(0, 1). These are the actual
values of physical coordinates when the image was sampled.
Hence these digital images can be represented as matrices of size
M*N. Each element of this matrix array is called an image element, picture element,
pixel, or pel.

3.2.3 RELATIONS BETWEEN PIXELS

3.2.3.1 NEIGHBORS OF PIXELS
A pixel p at coordinates (x, y) has four horizontal and vertical
neighbors whose coordinates are given by
(x+1, y), (x-1, y), (x, y+1), (x, y-1).
This set of pixels, called the 4-neighbors of p, is denoted by N4(p).
Each pixel is a unit distance from (x, y), and some of the neighbors of p lie outside the
digital image if (x, y) is on the border of the image.

The four diagonal neighbors of p have coordinates
(x+1, y+1), (x+1, y-1), (x-1, y+1), (x-1, y-1)
and are denoted by ND(p). These points, together with the 4-neighbors, are called the
8-neighbors of p, denoted by N8(p). As before, some of the points in ND(p) and N8(p)
fall outside the image if (x, y) is on the border of the image.

3.2.3.2 ADJACENCY
There are 3 types of adjacency:
4-adjacency: Two pixels p and q with values from V are 4-adjacent if q is
in the set N4(p).

8-adjacency.:Two pixels p and q with values from V are 8-adjacent if q is
in the set N8(p).

m-adjacency (mixed adjacency): Two pixels p and q with values from V are
m-adjacent if
(i) q is in N4(p), or
(ii) q is in ND(p) and the set N4(p)N4(q) has no pixels whose values
6
are from V.
Mixed adjacency is a modification of 8-adjacency. It is introduced to eliminate the
ambiguities that often arise when 8-adjacency is used.

3.2.3.3 CONNECTIVITY
Connectivity between pixels is a fundamental concept that
simplifies the definition of numerous digital image concepts, such as regions and
boundaries.To establish if two pixels are connected, it must be determined if they are
neighbors and if their gray levels satisfy a specified criterion of similarity.

In a binary image with values 0 and 1, two pixels may be 4-
neighbors, but they are said to be connected only if they have the same value.

A (digital) path (or curve) from pixel p with coordinates (x, y) to
pixel q with coordinates (s, t) is a sequence of distinct pixels with coordinates
(x0,y0),(x1,y1),..(xn,yn)
where pixels (xi,yi) and (xi-1,yi-1) are adjacent for 1in
.
3.2.3.4 CONNECTED COMPONENT
Let S represent a subset of pixels in an image. Two pixels p and
q are said to be connected in S if there exists a path between them consisting entirely
of pixels in S. For any pixel p in S, the set of pixels that are connected to it in S is
called a connected component of S. If it only has one connected component, then set S
is called a connected set.

3.2.3.5 BOUNDARY
Let R be a subset of pixels in an image. We call R a region of the
image if R is a connected set. The boundary (also called border or contour) of a
region R is the set of pixels in the region that have one or more neighbors that are not
in R.

7
3.3 MORPHOLOGICAL IMAGE PROCESSING
3.3.1 INTRODUCTION
In the processing and analysis of images it is important to be able to
extract features, describe shapes and recognize patterns. Such tasks refer to
geometrical concepts such as size, shape, and orientation. Mathematical morphology
uses concepts from set theory, geometry and topology to analyze geometrical
structures in an image.
The word morphology stems from the Greek words morphe and
logos, meaning the study of forms. Mathematical morphology examines the
geometrical structure of an image by probing it with small patterns, called structuring
elements, of varying size and shape.
The above procedure results in nonlinear image operators, which are
well suited to exploring geometrical and topological structures. A succession of such
operators is applied to an image in order to make certain features apparent,
distinguishing meaningful information from irrelevant distortions, by reducing it to a
sort of caricature (skeletonization).

3.3.2 EROSION, DILATION, OPENING, CLOSING
Erosion: It is an operation that shrinks and thins objects in a binary image.
Dilation: It is an operation that grows and thickens objects in a binary image.
Opening: It is defined as erosion followed by dilation.
Closing: It is defined as dilation followed by erosion.

Figure 3.1 (a) shows an example binary image. Figures 3.1 (b) and 3.1 (c) show how
the image is changed by the two most common morphological operations, erosion and
dilation.


8




Figure 3.1 Morphological Operation (erosion, dilation, opening, closing)


In erosion, every object pixel that is touching a background
pixel is changed into a background pixel. In dilation, every background pixel that is
touching an object pixel is changed into an object pixel. Erosion makes the objects
smaller, and can break a single object into multiple objects. Dilation makes the
objects larger, and can merge multiple objects into one.
Figures 3.1 (d) and 3.1 (e) how the image is changed by the two
other morphological operations, opening removes small islands and thin filaments of
object pixels. Likewise, closing removes islands and thin filaments of background
pixels. These techniques are useful for handling noisy images where some pixels have
the wrong binary value.




9
4. MATLAB

4.1 INTRODUCTION

MATLAB is a high-performance language for technical
computing. It integrates computation, visualization, and programming in an easy-to-
use environment where problems and solutions are expressed in familiar mathematical
notation. Typical uses include

Math and computation
Algorithm development
Data acquisition
Modeling, simulation, and prototyping
Data analysis, exploration, and visualization
Scientific and engineering graphics
Application development, including graphical user interface building

MATLAB is an interactive system whose basic data element is an
array that does not require dimensioning. This allows you to solve many technical
computing problems, especially those with matrix and vector formulations. The name
MATLAB stands for matrix laboratory.

MATLAB features a family of add-on application-specific
solutions called toolboxes. MATLAB toolboxes allow us to learn and apply
specialized technology. Toolboxes are comprehensive collections of MATLAB
functions (M-files) that extend the MATLAB environment to solve particular classes
of problems. Areas in which toolboxes are available include image processing, signal
processing, control systems, neural networks, fuzzy logic, wavelets, simulation, and
many others.



10
4.2 MATRICES

4.2.1 ENTERING THE MATRICES

Different ways to enter matrices:
Enter an explicit list of elements.
Load matrices from external data files.
Generate matrices using built-in functions.
Create matrices with your own functions in M-files.
Basic conventions used to enter matrices:
Separate the elements of a row with blanks or commas.
Use a semicolon (;) to indicate the end of each row.
Surround the entire list of elements with square brackets, [ ].

Once we have entered the matrix, it is automatically remembered in the MATLAB
workspace. We can refer to it as the variable itself to which we assigned the matrix.

4.2.2 SUBSCRIPTS
The element in row i and column j of A is denoted by A(i,j). For
example, A(4,2) is the number in the fourth row and second column.
It is also possible to refer to the elements of a matrix with a single
subscript, A(k). In a two-dimensional matrix the array is regarded as one long column
vector formed from the columns of the original matrix.

4.2.3 THE COLON OPERATOR
The colon, :, is one of the most important MATLAB operators. It
occurs in several different forms.
The expression 1:10 is a row vector containing the integers from 1 to 10,
1 2 3 4 5 6 7 8 9 10
To obtain nonunit spacing, specify an increment. For example, 100:-7:50 is
100 93 86 79 72 65 58 51


11

Subscript expressions involving colons refer to portions of a
matrix. A(1:k,j) is the first k elements of the jth column of A. The colon by itself
refers to all the elements in a row or column of a matrix and the keyword end refers to
the last row or column. So sum (A(:,end)) computes the sum of the elements in the
last column of A.

4.2.4 VARIABLES

MATLAB does not require any type declarations or dimension
statements. When MATLAB encounters a new variable name, it automatically creates
the variable and allocates the appropriate amount of storage. If the variable already
exists, MATLAB changes its contents and, if necessary, allocates new storage.
Variable names consist of a letter, followed by any number of
letters, digits, or underscores. MATLAB uses only the first 31 characters of a variable
name. MATLAB is case sensitive; it distinguishes between uppercase and lowercase
letters. A and a are not the same variable. To view the matrix assigned to any variable,
simply enter the variable name.

4.2.5 GENERATING MATRICES

MATLAB provides four functions that generate basic matrices.
Zeros All zeros
Ones All ones
Rand Uniformly distributed random elements
randn Normally distributed random elements

4.2.6 SUPRESSING OUTPUT

If you simply type a statement and press Return or Enter,
MATLAB automatically displays the results on screen. However, if you end the line
with a semicolon, MATLAB performs the computation but does not display any
output.
12
4.3 PROGRAMMING
Programming includes:
Flow Control
Data Structures
Scripts and Functions

4.3.1 FLOW CONTROL
MATLAB has several flow control constructs:
if
switch and case
for
while
continue
break
try - catch
return

IF
The if statement evaluates a logical expression and executes a
group of statements when the expression is true. The optional elseif and else
keywords provide for the execution of alternate groups of statements. An end
keyword, which matches the if, terminates the last group of statements. The groups of
statements are delineated by the four keywords -- no braces or brackets are involved.

SWITCH AND CASE
The switch statement executes groups of statements based on the
value of a variable or expression. The keywords case and otherwise delineate the
groups. Only the first matching case is executed. There must always be an end to
match the switch.
Unlike the C language switch statement, MATLAB switch does not
fall through. If the first case statement is true, the other case statements do not
execute. So, break statements are not required.

13
FOR
The for loop repeats a group of statements a fixed, predetermined
number of times. A matching end delineates the statements

WHILE
The while loop repeats a group of statements an indefinite number
of times under control of a logical condition. A matching end delineates the
statements.

CONTINUE
The continue statement passes control to the next iteration of the
for loop or while loop in which it appears, skipping any remaining statements in the
body of the loop. In nested loops, continue passes control to the next iteration of the
for loop or while loop enclosing it.

BREAK
The break statement lets you exit early from a for loop or while
loop. In nested loops, break exits from the innermost loop only.

TRY-CATCH

The general form of a try-catch statement sequence is
try
statement
...
statement
catch
statement
...
statement
end
In this sequence the statements between try and catch are executed until an error
occurs. The statements between catch and end are then executed.

14

RETURN
Return terminates the current sequence of commands and returns
control to the invoking function or to the keyboard. Return is also used to terminate
keyboard mode. A called function normally transfers control to the function that
invoked it when it reaches the end of the function. We can insert a return statement
within the called function to force an early termination and to transfer control to the
invoking function.

4.3.2 DATA STRUCTURES

The other data structures in MATLAB include
Multidimensional Arrays
Cell Arrays

MULTIDIMENSIONAL ARRAYS
Multidimensional arrays in MATLAB are arrays with more than
two subscripts. One way of creating a multidimensional array is by calling zeros,
ones, rand, or randn with more than two arguments.
A three-dimensional array might represent three-dimensional
physical data, say the temperature in a room, sampled on a rectangular grid. Or it
might represent a sequence of matrices, A(k), or samples of a time-dependent matrix,
A(t). In these latter cases, the (i, j)th element of the kth matrix, or the tkth matrix, is
denoted by A(i,j,k).

CELL ARRAYS
Cell arrays in MATLAB are multidimensional arrays whose
elements are copies of other arrays. A cell array of empty matrices can be created with
the cell function. But, more often, cell arrays are created by enclosing a miscellaneous
collection of things in curly braces, {}. The curly braces are also used with subscripts
to access the contents of various cells.



15
4.3.3 SCRIPTS AND FUNCTIONS

MATLAB is a powerful programming language as well as an
interactive computational environment. Files that contain code in the MATLAB
language are called M-files. You create M-files using a text editor, then use them as
you would any other MATLAB function or command.

There are two kinds of M-files:
Scripts, which do not accept input arguments or return output arguments. They
operate on data in the workspace.
Functions, which can accept input arguments and return output arguments.
Internal variables are local to the function.

SCRIPTS
When we invoke a script, MATLAB simply executes the
commands found in the file. Scripts can operate on existing data in the workspace, or
they can create new data on which to operate. Although scripts do not return output
arguments, any variables that they create remain in the workspace, to be used in
subsequent computations. In addition, scripts can produce graphical output using
functions like plot.

FUNCTIONS
Functions are M-files that can accept input arguments and return
output arguments. The names of the M-file and of the function should be the same.
Functions operate on variables within their own workspace, separate from the
workspace you access at the MATLAB command prompt.

TYPES OF FUNCTIONS
MATLAB offers several different types of functions to use in your programming.

Anonymous Functions
An anonymous function is a simple form of MATLAB function
that does not require an M-file. It consists of a single MATLAB expression and any
number of input and output arguments. You can define an anonymous function right
16
at the MATLAB command line, or within an M-file function or script. This gives you
a quick means of creating simple functions without having to create M-files each
time. The syntax for creating an anonymous function from an expression is f =
@(arglist)expression

Primary and Subfunctions
All functions that are not anonymous must be defined within an
M-file. Each M-file has a required primary function that appears first in the file, and
any number of subfunctions that follow the primary. Primary functions have a wider
scope than subfunctions. That is, primary functions can be invoked from outside of
their M-file (from the MATLAB command line or from functions in other M-files)
while subfunctions cannot. Subfunctions are visible only to the primary function and
other subfunctions within their own M-file.

Private Functions
A private function is a type of primary M-file function. Its unique
characteristic is that it is visible only to a limited group of other functions. This type
of function can be useful if you want to limit access to a function, or when you choose
not to expose the implementation of a function.
Private functions reside in subdirectories with the special name
private. They are visible only to functions in the parent directory.

Nested Functions
You can define functions within the body of any MATLAB M-file
function. These are said to be nested within the outer function. A nested function
contains any or all of the components of any other M-file function
Like other functions, a nested function has its own workspace
where variables used by the function are stored. But it also has access to the
workspaces of all functions in which it is nested.




17
5. IMAGE PROCESSING TOOLBOX

5.1 INTRODUCTION
The Image Processing Toolbox (IPT) is a collection of functions
that extend the capability of the MATLAB numeric computing environment. The
toolbox supports a wide range of image processing operations, including

Spatial image transformations
Morphological operations
Neighborhood and block operations
Linear filtering and filter design
Transforms
Image analysis and enhancement
Image registration
Deblurring
Region of interest operations

5.2 IMAGES IN MATLAB AND IPT

The basic data structure in MATLAB, array is naturally suited to
the representation of images, real-valued ordered sets of color or intensity data.
MATLAB stores most images as two-dimensional arrays (i.e.,
matrices), in which each element of the matrix corresponds to a single pixel in the
displayed image. For example, an image composed of 200 rows and 300 columns of
different colored dots would be stored in MATLAB as a 200-by-300 matrix.
Some images, such as RGB, require a three-dimensional array,
where the first plane in the third dimension represents the red pixel intensities, the
second plane represents the green pixel intensities, and the third plane represents the
blue pixel intensities.




18
5.3 IMAGE TYPES IN TOOLBOX
The Image Processing Toolbox supports four basic types of images:
Indexed images
Intensity images
Binary images
RGB images
INDEXED IMAGES
An indexed image consists of a data matrix, X, and a colormap
matrix, map. The data matrix can be of class uint8, uint16, or double. The colormap
matrix is an m-by-3 array of class double containing floating-point values in the range
[0,1]. Each row of map specifies the red, green, and blue components of a single
color. An indexed image uses direct mapping of pixel values to colormap values. The
color of each image pixel is determined by using the corresponding value of X as an
index into map. The value 1 points to the first row in map, the value 2 points to the
second row, and so on.

INTENSITY IMAGES
An intensity image is a data matrix, I, whose values represent
intensities within some range. MATLAB stores an intensity image as a single matrix,
with each element of the matrix corresponding to one image pixel. The matrix can be
of class double, uint8, or uint16.While intensity images are rarely saved with a
colormap, MATLAB uses a colormap to display them.
The elements in the intensity matrix represent various intensities,
or gray levels, where the intensity 0 usually represents black and the intensity 1, 255,
or 65535 usually represents full intensity, or white.

BINARY IMAGES
In a binary image, each pixel assumes one of only two discrete
values. Essentially, these two values correspond to on and off. A binary image is
stored as a logical array of 0's (off pixels) and 1's (on pixels).

RGB IMAGES
An RGB image, sometimes referred to as a true-color image, is
stored in MATLAB as an m-by-n-by-3 data array that defines red, green, and blue
19
color components for each individual pixel. The color of each pixel is determined by
the combination of the red, green, and blue intensities stored in each color plane at the
pixel's location.

CONVERTING IMAGE TYPES
The following table lists all the image conversion functions in the
Image Processing Toolbox.

Function Description
dither Create a binary image from a grayscale intensity image by dithering;
create an indexed image from an RGB image by dithering
gray2ind Create an indexed image from a grayscale intensity image
grayslice Create an indexed image from a grayscale intensity image by thresholding
im2bw Create a binary image from an intensity image, indexed image, or RGB
image, based on a luminance threshold
ind2gray Create a grayscale intensity image from an indexed image
ind2rgb Create an RGB image from an indexed image
mat2gray Create a grayscale intensity image from data in a matrix, by scaling the
data
rgb2gray Create a grayscale intensity image from an RGB image
rgb2ind Create an indexed image from an RGB image

5.4 READING AN IMAGE
The imread function reads an image from any supported graphics
image file format, in any of the supported bit depths. Most image file formats use 8
bits to store pixel values.
Syntax
A =imread(filename,fmt)
[X,map] =imread(filename,fmt)
MATLAB supports many common graphics file formats, such as
Microsoft Windows Bitmap (BMP), Graphics Interchange Format (GIF), J oint
Photographic Experts Group (J PEG), Portable Network Graphics (PNG), and Tagged
Image File Format (TIFF) formats.
20
5.5 WRITING AN IMAGE
The function imwrite writes an image to a graphics file in one of
the supported formats. The most basic syntax for imwrite takes the image variable
name and a filename. If you include an extension in the filename, MATLAB infers
the desired file format from it.
Syntax
imwrite(A,filename,fmt)
imwrite(X,map,filename,fmt)

5.6 COORDINATE SYSTEMS
The two main coordinate systems used in the Image Processing Toolbox are
Pixel Coordinates
Spatial Coordinates

PIXEL COORDINATES
The most convenient method for expressing locations in an image is
to use pixel coordinates. In this coordinate system, the image is treated as a grid of
discrete elements, ordered from top to bottom and left to right.
For pixel coordinates, the first component r (the row) increases
downward, while the second component c (the column) increases to the right. Pixel
coordinates are integer values and range between 1 and the length of the row or
column. There is a one-to-one correspondence between pixel coordinates and the
coordinates MATLAB uses for matrix subscripting.

SPATIAL COORDINATES
In the spatial coordinate system, locations in an image are positions
on a plane, and they are described in terms of x and y (not r and c as in the pixel
coordinate system). The spatial coordinate system corresponds closely to the pixel
coordinate system in many ways. For example, the spatial coordinates of the center
point of any pixel are identical to the pixel coordinates for that pixel.
In pixel coordinates, the upper left corner of an image is (1,1),
while in spatial coordinates, this location by default is (0.5,0.5). This difference is due
to the pixel coordinate system's being discrete, while the spatial coordinate system is
continuous.
21
5.7 DISPLAYING IMAGES
The Image Processing Toolbox includes two display functions
imview
imshow

5.7.1 IMVIEW
The imview function displays the image in a separate, J ava-based
window called the Image Viewer. The Image Viewer provides tools for flexible
navigation, especially for large images, and for pixel value inspection.

5.7.2 IMSHOW
The imshow function creates a Handle Graphics image object and
displays the image in a MATLAB figure window. imshow automatically sets the
values of certain figure, axes, and image object properties to control how image data
is interpreted.

DISPLAYING DIFFERENT IMAGES

Image Type Syntax
Indexed Images imshow(X,map), imview(X,map)
Intensity Images imshow(I), imview(I)
Binary Images imshow(BW), imview(BW)
RGB Images imshow(RGB), imview(RGB)

5.8 SPATIAL TRANSFORMATIONS

5.8.1 INTERPOLATION
Interpolation is the process used to estimate an image value at a location in between
image pixels. For example, if you resize an image so it contains more pixels than it
did originally, the toolbox uses interpolation to determine the values for the additional
pixels.


22
INTERPOLATION METHODS
The Image Processing Toolbox provides three interpolation methods:
Nearest-neighbor interpolation
Bilinear interpolation
Bicubic interpolation

The interpolation methods all work in a fundamentally similar
way. In each case, to determine the value for an interpolated pixel, they find the point
in the input image that the output pixel corresponds to. They then assign a value to the
output pixel by computing a weighted average of some set of pixels in the vicinity of
the point. The weightings are based on the distance each pixel is from the point.

Nearest-neighbor interpolation: The output pixel is assigned the value of the pixel that
the point falls within. No other pixels are considered.
Bilinear interpolation: The output pixel value is a weighted average of pixels in the
nearest 2-by-2 neighborhood.
Bicubic interpolation: The output pixel value is a weighted average of pixels in the
nearest 4-by-4 neighborhood.

The number of pixels considered affects the complexity of the
computation. Therefore the bilinear method takes longer than nearest-neighbor
interpolation, and the bicubic method takes longer than bilinear. However, the greater
the number of pixels considered, the more accurate the effect is.

5.8.2 IMAGE ROTATION
To rotate an image, use the imrotate function. imrotate accepts
four arguments:
The image to be rotated
The rotation angle(in degrees)
The interpolation method
The size of the output image
Syntax
B =imrotate(A,angle)
B =imrotate(A,angle,method)
23

Specifying the Interpolation Method
This table lists the supported interpolation methods in order of complexity
Argument Value Interpolation Method
'nearest' Nearest-neighbor interpolation (the default)
'bilinear' Bilinear interpolation
'bicubic' Bicubic interpolation

B =imrotate(A,angle,method,bbox)
The bbox argument specifies the bounding box of the returned image. bbox is a string
that can have one of these values.
Argument
Value
Description
'crop' Output image B includes only the central portion of the rotated image
and is the same size as A.
'loose' Output image B includes the whole rotated image and is generally
larger than the input image A. imrotate sets pixels in areas outside the
original image to zero.
(default)

5.9 LINEAR FILTERING
Filtering is a technique for modifying or enhancing an image.
Filtering is a neighborhood operation, in which the value of any given pixel in the
output image is determined by applying some algorithm to the values of the pixels in
the neighborhood of the corresponding input pixel. A pixel's neighborhood is some set
of pixels, defined by their locations relative to that pixel.
Linear filtering is filtering in which the value of an output pixel is
a linear combination of the values of the pixels in the input pixel's neighborhood.

5.9.1 CONVOLUTION
Linear filtering of an image is accomplished through an operation called convolution.
In convolution, the value of an output pixel is computed as a weighted sum of
neighboring pixels. The matrix of weights is called the convolution kernel, also
known as the filter.
24
5.9.2 CORRELATION
The operation called correlation is closely related to convolution. In correlation, the
value of an output pixel is also computed as a weighted sum of neighboring pixels.
The difference is that the matrix of weights, in this case called the correlation kernel,
is not rotated during the computation.

5.9.3 IMFILTER
Filtering of images, either by correlation or convolution can be performed using the
toolbox function imfilter.
Syntax:
B =imfilter(A,H)
Above syntax filters the multidimensional array A with the multidimensional filter H.
In addition optional arguments can also be given to imfilter function.

Correlation and Convolution Options:
Option Description
'corr' imfilter performs multidimensional filtering using correlation, which is the
same way that filter2 performs filtering. When no correlation or convolution
option is specified, imfilter uses correlation
'conv' imfilter performs multidimensional filtering using convolution.


The fspecial function produces several kinds of predefined filters, in the form
of correlation kernels. After creating a filter with fspecial, you can apply it
directly to your image data using imfilter.

5.10 NOISE REMOVAL
Digital images are prone to a variety of types of noise. There are
several ways that noise can be introduced into an image, depending on how the image
is created.
The toolbox provides a number of different ways to remove or
reduce noise in an image. Different methods are better for different kinds of noise.

25
The methods available include:
Using Linear Filtering
Using Median Filtering
Using Adaptive Filtering

LINEAR FILTERING
We can use linear filtering to remove certain types of noise. Certain
filters, such as averaging or Gaussian filters, are appropriate for this purpose. For
example, an averaging filter is useful for removing grain noise from a photograph.
Because each pixel gets set to the average of the pixels in its neighborhood, local
variations caused by grain are reduced.

MEDIAN FILTERING
Median filtering is similar to using an averaging filter, in that each
output pixel is set to an average of the pixel values in the neighborhood of the
corresponding input pixel. However, with median filtering, the value of an output
pixel is determined by the median of the neighborhood pixels, rather than the mean.
The median is much less sensitive than the mean to extreme
values (called outliers). Median filtering is therefore better able to remove these
outliers without reducing the sharpness of the image. The medfilt2 function
implements median filtering.
Syntax:
B =medfilt2(A,[m n])
The above syntax performs median filtering of the matrix A in
two dimensions. Each output pixel contains the median value in the m-by-n
neighborhood around the corresponding pixel in the input image.

ADAPTIVE FILTERING
The Wiener2 function applies a Wiener filter (a type of linear
filter) to an image adaptively, tailoring itself to the local image variance. Where the
variance is large, wiener2 performs little smoothing. Where the variance is small,
wiener2 performs more smoothing.
26
This approach often produces better results than linear filtering.
The adaptive filter is more selective than a comparable linear filter, preserving edges
and other high-frequency parts of an image.

5.11 MORPHOLOGICAL OPERATIONS

Morphology is a technique of image processing based on
shapes. The value of each pixel in the output image is based on a comparison of the
corresponding pixel in the input image with its neighbors.

5.11.1 DILATION AND EROSION
Dilation and erosion are two fundamental morphological
operations. Dilation adds pixels to the boundaries of objects in an image, while
erosion removes pixels on object boundaries. The number of pixels added or removed
from the objects in an image depends on the size and shape of the structuring element
used to process the image.

RULES FOR DILATION AND EROSION
Operation Rule
Dilation The value of the output pixel is the maximum value of all the pixels in the
input pixel's neighborhood. In a binary image, if any of the pixels is set to
the value 1, the output pixel is set to 1.
Erosion The value of the output pixel is the minimum value of all the pixels in the
input pixel's neighborhood. In a binary image, if any of the pixels is set to
0, the output pixel is set to 0.

DILATING AN IMAGE
To dilate an image, use the imdilate function. The imdilate function
accepts two primary arguments: The input image to be processed (grayscale, binary,
or packed binary image) A structuring element object, returned by thestrel function,
or a binary matrix defining the neighborhood of a structuring element.
Syntax:
IM2 =imdilate(IM,SE)
27
ERODING AN IMAGE
To erode an image, use the imerode function. The imerode
function accepts two primary arguments: The input image to be processed (grayscale,
binary, or packed binary image) A structuring element object, returned by the strel
function, or a binary matrix defining the neighborhood of a structuring element.
Syntax
IM2 =imerode(IM,SE)

STREL
The strel function creates a morphological structuring element
Syntax
SE =strel(shape,parameters)

Flat Structuring Elements
Arbitrary Rectangle
Pair Line
Diamond Square
Periodicline octagon
Disk

Nonflat Structuring Elements
Arbitrary
ball

5.11.2 MORPHOLOGICAL OPENING
Morphological opening of an image is an erosion followed by
a dilation, using the same structuring element for both operations.
Syntax
IM2 =imopen(IM,SE)
IM2 =imopen(IM,SE) performs morphological opening on
the grayscale or binary image IM with the structuring element SE. The argument SE
must be a single structuring element object, as opposed to an array of objects.


28
5.11.3 MORPHOLOGICAL CLOSING
Morphological closing of an image is an dilation followed by
a erosion, using the same structuring element for both operations.
Syntax
IM2 =imclose(IM,SE)
IM2 =imclose(IM,SE) performs morphological closing on
the grayscale or binary image IM, returning the closed image, IM2.

5.11.4 IMFILL
Theimfill function performs a flood-fill operation on binary
and grayscale images. For binary images, imfill changes connected background pixels
(0's) to foreground pixels (1's), stopping when it reaches object boundaries. For
grayscale images, imfill brings the intensity values of dark areas that are surrounded
by lighter areas up to the same intensity level as surrounding pixels.
Syntax
BW2 =imfill(BW,locations)
BW2 =imfill(BW,'holes')
BW2 =imfill(BW,'holes') fills holes in the binary image BW. A
hole is a set of background pixels that cannot be reached by filling in the background
from the edge of the image.
Imfill differs from the other object-based operations in that it
operates on background pixels. When you specify connectivity with imfill, you are
specifying the connectivity of the background, not the foreground.










29
6. PROJECT DESCRIPTION

PROJECT TITLE: MANGO GRADING BASED ON SIZE USING IMAGE
PROCESSING TECHNIQUES

6.1 INTRODUCTION
Our project is a part of the ongoing project MANGO SORTER.

The objective of the project Mango Sorter is to develop machine
vision & image analysis methodologies for fast sorting & grading of mangoes as per
the required grading standards.

The following diagram shows the various parameters considered
for grading mangoes:



PARAMET
ERS FOR
GRADING
MANGOES

COLOR

SIZE

WEIGHT
SHAPE

EXTERNAL
DEFECTS

INTERNAL
DEFECTS

AREA

MAJ OR
AXIS

MINOR
AXIS






30
6.2 EXPLANATION
Mangoes are graded mainly based on the following parameters:
Color
Size
Shape
Weight
External defects
Internal defects

For the mangoes of export quality:
Color, size, shape & weight are the major parameters.
Grading is required before processing the fruit.
Fruits must be free from black spots on the surface and internal defects are
also important.
Ripening and maturity are the other factors to be considered.

HARDWARE SETUP
Firewire output cameras are being used because they do not
require additional frame grabber and are also cheaper relative to camera link based
cameras. TMS32DM642 DSP based EVM is being used for implementation of the
algorithms under DSP environment. Firewire camera SONY Model XCD-X710CR
has been interfaced with our application program to capture the images in real time.

COLOR COMPUTATION
It can be done by the following method, background removal,
conversion of RGB components to HSI components, median value of the Hue
histogram to be computed and this value to be used for grading.

SHAPE ANALYSIS
It can be represented in several ways with a combination of the size
measurements. It can be found by comparing major and minor axes.



31

SIZE COMPUTATION
The following diagram shows the commonly used features for size measurement:




SIZE


AREA


PERIMETER


MAJ OR AXIS


MINOR AXIS

In the food industry the minor axis (width) is some times referred
to the size of the fruit. Most convenient is to measure the area. It includes the
measurement of number of white pixels in the area of interest of binary image of the
mango.
The project we are assigned is to develop an algorithm to find the
minor axis, which is one of the criterion to determine the size of the mangoes.

6.3 ALGORITHM FOR SIZE COMPUTATION

Following steps are used to compute the minor axis
The first step is to read the image from the directory in which images are
stored after acquisition.
Converting the acquired image (with blue background) to binary image by
converting all the background (blue) pixels to logical value 0 and remaining
pixels to logical 1.
Performing morphological operations (closing, filling holes in binary image)
to smoothen the image and fill all the holes inside the binary image.
Using median filter to filter the image for noise removal.
The next step is to find the length of longest intercept (major axis) which is
horizontal.
32
Final step is o find the minor axis which is perpendicular to the major axis
determined above.

6.4FLOWCHART:

START
READ THE
RGB IMAGE
CONVERT
TO BINARY
FIND MAJ OR
AXIS
FIND MINOR
AXIS
MAJ OR &
MINOR AXES
STOP

6.5 EXPLANATION OF OUR PROJECT:

Converting the image from RGB to Binary:
To read the image, the predefined function imread is used. We used
the images with the blue background (as assigned) for our project. Naturally the
mango has relatively low blue content than the red and green component. This is
taken as the criteria for converting the image to binary. The pixels having low blue
33
value than the red or green are assigned logical value 1. The other pixels are assigned
logical 0.

Processing of binary image
To fill any holes present in the binary image the function imfill is
used. To remove noise, filtering is done using median filter and the function used is
medfilt2. We also used imclose to smoothen the boundary of binary image and also to
fill any remaining holes.

Finding the major axis
The orientation of the major axis is found to use it as a reference
for minor axis. The longest intercept in the image is taken as the major axis. The
image is rotated every one degree through 180 degrees and the length of the longest
horizontal intercept is found. All the lengths are compared and the maximum length is
considered as the major axis. The orientation of the major axis is found and the image
is rotated to make the major axis horizontal.

Finding the minor axis
The axis perpendicular to the major axis is taken as the minor axis.
All the vertical intercepts are calculated for the image with the horizontal major axis.
They are compared and the longest one taken as the minor axis. The length of minor
axis (no. of pixels) is displayed.














34
7. CONCLUSION AND RECOMMENDATIONS


The minor axis of the mango which is one of the criterions to grade
mangoes based on the size was successfully measured using Image processing
toolbox in MATLAB. Both major and minor axes were measured. Minor axis was
measured with reference to the major axis. The result was displayed immediately on
the Matlab Command Window.
Images with blue background are only considered in developing
the algorithm because of the simplicity in removing the blue background and also
because of the availability of algorithm to remove any type of background which was
developed previously.
Efficiency of our program depends on how much angle do we
rotate each time to find the length of horizontal intercepts. The loop which we had
written executes 180 times if we rotate the image one degree each time and hence the
efficiency of the program decreases ,but the value of the minor axis can be determined
correctly. We also checked by rotating the image for 5 degrees each time and the error
came out to be 1 pixel on an average for both major and minor axis.
Modifications can be made for the program to reduce the
execution time by rotating to angle more than 1 degree and also by knowing the
approximate value the image may deviate from the supposed angle .If the image does
not go beyond 45 degrees on both sides of the almost horizontal major axis image
then the program can be modified and can be written to rotate the image through 45
degrees on both sides in steps of 1 or 5 degrees. This can be done by only examining
the deviation the image undergoes by human inspection of images.

8. REFERNCES

Digital Image Processing using MatLab by Rafael C. Gonzalez
Digital Image Processing by Rafeal C. Gonzalez
Matlab 7 Documentation


35
9. APPENDIX


Program for finding the minor axis

function minoraxis=pro(image)
clear
imview close all

%......................converting image to binary.%

a=im2double(image);
r=a(:,:,1);
g=a(:,:,2);
b=a(:,:,3);
dim=size(a);
row=dim(1);
col=dim(2);
i=1;
j=1;
q=zeros(row,col);
while i<=row
while j<=col
if (r(i,j) >b(i,j)) | (g(i,j) >b(i,j))
q(i,j)=1;
end
j=j+1;
end
j=1;
i=i+1;
end

s=imfill(q,'holes');
l =medfilt2(s,[6 6]);
imview(l)


%............finding lengths of major axes%

j=1;
major=zeros(1,36);

for i=0:5:179
im=imrotate(l,i,'bilinear','crop');


%........................leftmost pixel..%

u=1;
v=1;
36
h=0;
while u<=col
while v<=row
if im(v,u)==1
lmp=u;
h=1;
break
end
v=v+1;
end
if h==1
break
end
v=1;
u=u+1;
end

%......................rightmost pixel.%

u=col;
v=1;
h=0;

while u>=1
while v<=row
if im(v,u)==1
rmp=u;
h=1;
break
end
v=v+1;
end
if h==1
break
end
v=1;
u=u-1;
end




%.........................length of major axis%

major(j)=(rmp-lmp);
j=j+1;
end

%..........................finding longest majoraxis%

37
majormax=major(1);
for j=1:36
if major(j)>=majormax
majormax=major(j);
end
end
majoraxis=majormax

%.............finding the required orientation..%

for j=1:36
if major(j)==majormax
jmax=j;
break
end
end

ro=jmax*5;
fim=imrotate(l,ro,'bilinear','crop');
imview(fim)

%..............finding minor axis..%

i=1;
j=1;
count=0;
while j<=col
while i<=row
if fim(i,j)==1
count=count+1;
end
i=i+1;
end
minor(j)=count;
count=0;
i=1;
j=j+1;
end

%..............finding the longest minor axis.%

j=1;
max=0;
while j<=col
if minor(j)>=max
max=minor(j);
end
j=j+1;
end
minoraxis=max;
38
39

Вам также может понравиться