Skip to content

Commit

Permalink
changes k-means
Browse files Browse the repository at this point in the history
  • Loading branch information
mandeep-mahra committed Jul 10, 2023
1 parent 57cdeb4 commit b8bbbec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
Binary file modified Backend/input/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Backend/output/chromaSS.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Backend/output/kMeans.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 46 additions & 45 deletions Backend/src/kMeans.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,61 @@ function arrayToMatrix(array, width ,height){
}
return matrix;
}

async function kMeans(K, numChannel){
const convert = require("./matrixToImage");
const { createCanvas, Image, getImageData} = require('canvas');

function getRandomPixels(matrix){
l = matrix.length;
b = matrix[0].length;
let randomPixels = [];
for(let i=0; i<K; i++){
let x = Math.floor(Math.random()*(l));
let y = Math.floor(Math.random()*(b));
randomPixels.push([matrix[x][y], 1]);
}
return randomPixels;
function getRandomPixels(matrix){
l = matrix.length;
b = matrix[0].length;
let randomPixels = [];
for(let i=0; i<K; i++){
let x = Math.floor(Math.random()*(l));
let y = Math.floor(Math.random()*(b));
randomPixels.push([matrix[x][y], 1]);
}
return randomPixels;
}

function changeToClosest(matrix){
let clusters = getRandomPixels(matrix);
//Moving the clusters
for(let i =0; i<matrix.length; i++){
for(let j=0; j<matrix[0].length; j++){
var closestValue = 0;
var currentDist = 9007199254740991;
for(let k = 0; k<clusters.length; k++){
if(currentDist > (Math.abs(matrix[i][j] - clusters[k][0]))){
currentDist = Math.abs(matrix[i][j] - clusters[k][0]);
closestValue = k;
}
function changeToClosest(matrix){
let clusters = getRandomPixels(matrix);
//Moving the clusters
for(let i =0; i<matrix.length; i++){
for(let j=0; j<matrix[0].length; j++){
var closestValue = 0;
var currentDist = 9007199254740991;
for(let k = 0; k<clusters.length; k++){
if(currentDist > (Math.abs(matrix[i][j] - clusters[k][0]))){
currentDist = Math.abs(matrix[i][j] - clusters[k][0]);
closestValue = k;
}

clusters[closestValue][0] = (
Math.round(((clusters[closestValue][0] * clusters[closestValue][1])+matrix[i][j])/(clusters[closestValue][1] + 1))
);
clusters[closestValue][1]++;
}

clusters[closestValue][0] = (
Math.round(((clusters[closestValue][0] * clusters[closestValue][1])+matrix[i][j])/(clusters[closestValue][1] + 1))
);
clusters[closestValue][1]++;
}
// Assigning the values to pixels
for(let i =0; i<matrix.length; i++){
for(let j=0; j<matrix[0].length; j++){
var closestValue = -1;
var currentDist = 9007199254740991;
for(let k = 0; k<clusters.length; k++){
if(currentDist > (Math.abs(matrix[i][j] - clusters[k][0]))){
currentDist = Math.abs(matrix[i][j] - clusters[k][0]);
closestValue = clusters[k][0];
}
}
// Assigning the values to pixels
for(let i =0; i<matrix.length; i++){
for(let j=0; j<matrix[0].length; j++){
var closestValue = -1;
var currentDist = 9007199254740991;
for(let k = 0; k<clusters.length; k++){
if(currentDist > (Math.abs(matrix[i][j] - clusters[k][0]))){
currentDist = Math.abs(matrix[i][j] - clusters[k][0]);
closestValue = clusters[k][0];
}
matrix[i][j] = closestValue;
}
matrix[i][j] = closestValue;
}

}

}


async function kMeans(K, numChannel){
const convert = require("./matrixToImage");
const { createCanvas, Image, getImageData} = require('canvas');



const img = new Image();
img.src = "../input/image.jpg";
Expand Down Expand Up @@ -101,4 +102,4 @@ async function kMeans(K, numChannel){
}


module.exports = {kMeans, arrayToMatrix};
module.exports = {kMeans, arrayToMatrix, changeToClosest, getRandomPixels};

0 comments on commit b8bbbec

Please sign in to comment.