Image segmentation is a fundamental process in the field of image processing and computer vision. It refers to the technique of dividing an image into several regions or segments, where each segment represents a distinct area or object in the image. The main purpose of image segmentation is to isolate and identify specific objects or features in an image for analysis, interpretation, or recognition.
The K-Means algorithm is often used for image segmentation, where its objective is to group similar pixels into distinct regions or clusters. The basic idea behind K-Means is to find groups of points (in this case, pixels) that are similar to each other, assigning each pixel to a cluster represented by its centroid.
K-Means is sensitive to the initialization of centroids and can converge to local solutions. Therefore, it is common to run the algorithm several times with different initializations and choose the best solution.
Coloured PPM images have been used as inputs and image segmentation has been implemented using K-means technique. A detailed report of the original code can be found in report.
As the application involves segmenting an image, it is necessary to iterate through each pixel of the image. According to the repetition loops below, these iterations across pixels are carried out and are a good opportunity for parallelization, as currently the resolution and quality of images are increasing, making these repetition loops with many iterations.
Changing the original code, which can be found on github, to a version that contains parallelism to obtain better performance in time, was a work proposal from the Parallel Computing discipline. Guided by professor Henrique Cota de Freitas in the 6th period of the Computer Science course at the Pontifical Catholic University of Minas Gerais. This work was carried out by the students:
- Caio Eduardo Ramos Arães
- Danielle Dias Vieira
- Guilherme Quadros Werner
- João Augusto dos Santos Silva
- Thiago de Campos Ribeiro Nolasco
Frog | The Night King | Lakes with mountains |
![]() |
![]() |
![]() |
Frog (3 clusters) | The Night King (3 clusters) | Lakes with mountains (3 clusters) |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
C (Implementation)
The following tools are required in order to start the installation.
- OpenMP
-
To compile and run the code:
- Clone this repository:
git clone https://github.com/Theeeccoo/Implementation_comparison_ImageSegmentation.git
- To compile and execute the code, in the folder where the makefile is located, do:
make run
- Clone this repository:
5 parameters are required to execute the code, which are:
- The executable file:
./kmeans
- Image that will be segmented in the ppm extension:
./pix/frog.ppm
- Number of clusters in the K-mens algorithm, which means the number of regions in the final segmented image:
3
- Number of iterations in K-means algorithm:
7
- Number of threads:
4
Example: ./kmeans ./pix/frog.ppm 3 7 4
Note: To change the image that will be segmented, the number of clusters, the number of iterations and the number of threads can be changed in the makefile execution rule, which is where the command line for execution is. Or it can be placed directly in the terminal to run as per usage.