Dip Lab - 9
Dip Lab - 9
Dip Lab - 9
Enrollment #: _________________________________
Class: _________________________________
Objective
Today’s lab covers segmentation of images into regions. We will be specifically focusing on
segmentation based on clustering using the classical k-means clustering algorithm. By the end of
this lab you should be able to implement cluster based segmentation of an image.
Submission Requirements
You are expected to complete the assigned tasks within the lab session and show them to the
lab engineer/instructor. Get the lab journal signed by your instructor and submit it by the end of
lab session.
Tasks
Segmentation divides an image into disjoint regions such that pixels within a region share some
common attributes. Segmentation algorithms are broadly categorized into similarity and
discontinuity based methods. Similarity based segmentation exploits the key idea that pixels
within a region are ‘similar’ to one another. Discontinuity based methods, on the other hand,
rely on finding object boundaries and edges to identify different regions in an image.
A common approach in similarity based segmentation is to cluster similar pixels into groups
using standard clustering algorithms. The most commonly used of these clustering algorithms is
the k-mean clustering where given the number of clusters k, the algorithm iteratively partitions
the given data into k groups or clusters. In case of images, each pixel can be represented as a
point in a three dimensional space. The clustering algorithm can then be applied to identify
The statistics tool box in Matlab provides the kmeans function to cluster data into k groups. The
function requires the data matrix and number of clusters as input and returns the index (cluster
number) of each data member. In case of colored images, we can extract the individual
channels of the image and create the data matrix as follows.
dataMatrix=[R;G;B];
dataMatrix =double( dataMatrix');
Where R, G and B represent the red, green and blue channels of the image each represented as
a vector. The data matrix fed to the k-means functions requires each row to be one data point.
Hence, the size of data matrix is M x 3 where M is the total number of pixels in the image. The
function returns an Mx1 vector giving the cluster number to which each pixel belongs. Each
cluster can then be represented by a different color to represent a region/cluster.
Exercise 1
Load the image ‘onion.png’ and perform color based segmentation using k-means
clustering. Provide k = 5 and represent each cluster by a different color.
In addition to similarity based segmentaiton, discontinuities in the image can also be exploited
to segment objects. Convolution with filters based on image derivates can be used for this
purpose. These filters can be employed to detect points, edges and lines in an image. In the
following exercise, you will detect lines in a givn image using line detection masks.
Exercise 2
Load the provided image ‘field.png’ and perform convolution with the four line detection
masks. Display the four output images to see the response of each filter on the image.
Do Exercises 1 and 2 and get them checked by your instructor. If you are unable to complete the
tasks in the lab session, deposit this sheet alongwith your programs (printed or handwritten)
before the start of the next lab session.
+++++++++++++++++++++++++++