/k-means-image-contour-detector

This project implements a system for segmenting objects in images using the K-means clustering algorithm made with Matlab. Allowing users to manually indicate the object to segment with a rectangular box, and then segments it, marking its outline in red.

MIT LicenseMIT

K-means Image contour detector

This project was developed as part of the subject VC (Computer Vision) at FIB - UPC Barcelona, using Matlab. It implements a system for segmenting objects in images using the K-means clustering algorithm. The system allows users to manually indicate the object to segment with a rectangular box, and then segments it, marking its outline in red.

Objective

Implement a system that detects the contourn of objects in color images where the object is distinguishable from the background by its color.

Important

Dependencies: MATLAB with Image Processing Toolbox

You can find the detailed project process in the document titled project_structure_and_results.pdf   (and also the code)


Procedure

User Input for Object Selection:

  • The user indicates the object to segment by drawing a rectangular box around it.

Convert Image to HSV:

  • Convert the image from RGB to HSV format to handle the cyclic nature of the hue component.

K-means Clustering:

  • Apply K-means clustering on the HSV components to group colors into k clusters.
  • Experiment with different values of k (between 4 and 10).

Determine Object Colors:

  • Identify which color clusters primarily fall within the user-defined rectangle.
  • Create a mask indicating the object region.

Segmentation Decision:

  • Compare the frequency of each color inside and outside the rectangle to decide which colors belong to the object.
  • Use these decisions to segment the object and display the result.

Results

The method works well when the object is clearly distinguishable from the background in terms of color and when the object is centered in the image. However, the method may struggle with images where the object is not centered or the background has similar colors to the object.

How to Use

  • Launch Matlab application.
  • Load your image.
  • Run the script and manually select the object using a rectangular box.
  • The segmented object will be displayed with its outline marked in red.




Screenshots

Project step by step:

Screenshot from 2024-07-10 21-14-33

Screenshot from 2024-07-10 21-14-44

Screenshot from 2024-07-10 21-15-01

Screenshot from 2024-07-10 21-15-32

Screenshot from 2024-07-10 21-15-43

Another result:

resultat1



K-means color clustering results I love:

cheetah

cheetah7

lion

tiger1