image Segmentation using K-means Clustering Algorithm using matlab

 Image segmentation is a fundamental technique in image processing, which is used to partition an image into multiple segments or regions. Segmentation helps in separating the foreground from the background, and also to identify different objects in an image. One of the popular techniques for image segmentation is clustering, and K-means is one of the most widely used clustering algorithms. In this article, we will discuss how to perform image segmentation using the K-means clustering algorithm in MATLAB.

K-means clustering is an unsupervised learning algorithm that partitions a given set of data into K clusters, where K is a pre-defined number of clusters. The K-means algorithm tries to minimize the within-cluster variance by finding the centroids of the clusters. The algorithm proceeds as follows:

  1. Initialize K cluster centroids randomly
  2. Assign each data point to the nearest centroid
  3. Recalculate the centroids for each cluster
  4. Repeat steps 2 and 3 until convergence

The K-means algorithm can be used for image segmentation by treating the pixels in an image as data points. Each pixel can be represented as a vector of its color values, and the algorithm can be used to group similar pixels together into clusters.

Let's see how we can implement image segmentation using the K-means clustering algorithm in MATLAB. First, we need to load an image into MATLAB. We can use the "imread" function to read an image file into a matrix.

img = imread('image.jpg');

Next, we need to reshape the image into a 2D array, where each row represents a pixel and the columns represent the color values.

img_vec = reshape(img, [], 3);

Now we can apply the K-means algorithm to the pixel data. We can use the "kmeans" function in MATLAB to perform the clustering.

K = 4; % number of clusters

[idx, centroids] = kmeans(img_vec, K);

The "kmeans" function returns the cluster indices for each data point and the centroid positions for each cluster. We can reshape the cluster indices into an image format using the "reshape" function.

idx_img = reshape(idx, size(img, 1), size(img, 2));

Now we can create a segmented image by replacing each pixel with its corresponding cluster centroid.

seg_img = zeros(size(img));

for i = 1:K

    seg_img(idx_img == i, :) = repmat(centroids(i, :), sum(idx_img == i), 1);

end

Finally, we can display the original image and the segmented image side by side using the "subplot" function.

figure;

subplot(1, 2, 1);

imshow(img);

title('Original Image');

subplot(1, 2, 2);

imshow(uint8(seg_img));

title('Segmented Image');

The above code will display the original image and the segmented image side by side in a MATLAB figure window.

here is the full MATLAB code for image segmentation using the K-means clustering algorithm:

% Load image

img = imread('image.jpg');

 

% Reshape image into 2D array

img_vec = reshape(img, [], 3);

 

% Perform K-means clustering

K = 4; % number of clusters

[idx, centroids] = kmeans(img_vec, K);

 

% Reshape cluster indices into image format

idx_img = reshape(idx, size(img, 1), size(img, 2));

 

% Create segmented image

seg_img = zeros(size(img));

for i = 1:K

    seg_img(idx_img == i, :) = repmat(centroids(i, :), sum(idx_img == i), 1);

end

 

% Display original and segmented image side by side

figure;

subplot(1, 2, 1);

imshow(img);

title('Original Image');

subplot(1, 2, 2);

imshow(uint8(seg_img));

title('Segmented Image');

Note that you need to replace "image.jpg" with the filename of your own image. Also, you can experiment with different values of K to obtain different segmentations of the image.

In conclusion, the K-means clustering algorithm is a powerful technique for image segmentation. In MATLAB, we can use the "kmeans" function to perform clustering on the pixel data and create a segmented image. By experimenting with different values of K, we can obtain different segmentations of the image.


Post a Comment

Previous Post Next Post