Skip to content

Commit

Permalink
kmeans fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mandeep-mahra committed Jul 11, 2023
1 parent b8bbbec commit 3a2f577
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 7 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.
11 changes: 4 additions & 7 deletions Backend/src/kMeans.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function arrayToMatrix(array, width ,height){
}
return matrix;
}
function getRandomPixels(matrix){
function getRandomPixels(matrix, K){
l = matrix.length;
b = matrix[0].length;
let randomPixels = [];
Expand All @@ -22,8 +22,8 @@ function getRandomPixels(matrix){
return randomPixels;
}

function changeToClosest(matrix){
let clusters = getRandomPixels(matrix);
function changeToClosest(matrix,K){
let clusters = getRandomPixels(matrix,K);
//Moving the clusters
for(let i =0; i<matrix.length; i++){
for(let j=0; j<matrix[0].length; j++){
Expand Down Expand Up @@ -64,8 +64,6 @@ async function kMeans(K, numChannel){
const convert = require("./matrixToImage");
const { createCanvas, Image, getImageData} = require('canvas');



const img = new Image();
img.src = "../input/image.jpg";
var canvas = createCanvas(img.width, img.height);
Expand All @@ -88,14 +86,13 @@ async function kMeans(K, numChannel){
}
}


var rMatrix = arrayToMatrix(red, width, height);
var gMatrix = arrayToMatrix(green, width, height);
var bMatrix = arrayToMatrix(blue, width, height);

var matrices = [rMatrix, gMatrix, bMatrix];
for(var i =0; i<numChannel ; i++)
changeToClosest(matrices[i]);
changeToClosest(matrices[i], K);

const path = "../output/kMeans.jpg";
convert.toImage(rMatrix, gMatrix, bMatrix, path);
Expand Down

0 comments on commit 3a2f577

Please sign in to comment.