Skip to content

Commit

Permalink
I hope i don't break everything
Browse files Browse the repository at this point in the history
  • Loading branch information
arsh73552 committed Jul 11, 2023
1 parent e5244b7 commit 6ad24f5
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 111 deletions.
114 changes: 80 additions & 34 deletions Backend/src/kMeans.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,111 @@
function arrayToMatrix(array, width ,height){
var matrix = [];
var temp = [];
for(var i=0; i<=(height * width); i++){
if(i%width == 0 && i != 0){
matrix.push(temp)
temp = []
}
temp.push(array[i]);
/**
* Converts a flat array into a matrix of specified width and height.
* @param {Array} array - The flat array to convert into a matrix.
* @param {number} width - The desired width of the matrix.
* @param {number} height - The desired height of the matrix.
* @returns {Array} - The matrix representation of the input array.
*/
function arrayToMatrix(array, width, height) {
var matrix = [];
var temp = [];

// Iterate over each element in the array
for (var i = 0; i < array.length; i++) {
// Check if a new row should be created
if (i % width === 0 && i !== 0) {
// Push the current row to the matrix
matrix.push(temp);
// Reset the temporary row array
temp = [];
}
return matrix;
// Add the current element to the temporary row array
temp.push(array[i]);
}

// Push the last row to the matrix
matrix.push(temp);

// Return the resulting matrix
return matrix;
}
function getRandomPixels(matrix, K){
l = matrix.length;
b = matrix[0].length;

/**
* Generates random pixels from a given matrix.
* @param {Array} matrix - A two-dimensional array representing an image or pixel grid.
* @param {number} K - The number of random pixels to generate.
* @returns {Array} - An array of pixel values with a count of 1.
*/
function getRandomPixels(matrix, K) {
// Get the dimensions of the matrix
const length = matrix.length;
const breadth = matrix[0].length;

// Array to store the random pixels
let randomPixels = [];
for(let i=0; i<K; i++){
let x = Math.floor(Math.random()*(l));
let y = Math.floor(Math.random()*(b));

// Generate K random pixels
for(let i = 0; i < K; i++) {
// Generate random coordinates within the matrix boundaries
let x = Math.floor(Math.random() * length);
let y = Math.floor(Math.random() * breadth);

// Retrieve the pixel value at the generated coordinates and store it in the randomPixels array
randomPixels.push([matrix[x][y], 1]);
}

// Return the array of random pixels
return randomPixels;
}

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++){
/**
* Changes the values in the matrix to their closest cluster value.
* @param {Array} matrix - The matrix to be updated.
* @param {number} K - The number of clusters.
*/
function changeToClosest(matrix, K) {
// Generate random clusters
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++) {
var closestValue = 0;
var currentDist = 9007199254740991;
for(let k = 0; k<clusters.length; k++){
if(currentDist > (Math.abs(matrix[i][j] - clusters[k][0]))){

// Find the closest cluster value to the current pixel
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))
);

// Update the closest cluster value based on the current pixel
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++){

// Assign the closest cluster values to the pixels in the matrix
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]))){

// Find the closest cluster value to the current pixel
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];
}
}

// Update the matrix with the closest cluster value
matrix[i][j] = closestValue;
}
}

}


async function kMeans(K, numChannel){
const convert = require("./matrixToImage");
const { createCanvas, Image, getImageData} = require('canvas');
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions __tests__/test_change2Closest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const currObject = require('../Backend/src/kMeans.js');

describe('changeToClosest', () => {
it('should update the matrix with the closest cluster values', () => {
// Test input
const matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
const K = 2;

// Make a copy of the original matrix for comparison
const originalMatrix = JSON.parse(JSON.stringify(matrix));

// Call the changeToClosest function
currObject.changeToClosest(matrix, K);

// Assertions
expect(matrix).not.toEqual(originalMatrix);
// Additional assertions based on the expected behavior of the changeToClosest function
});
});
61 changes: 61 additions & 0 deletions __tests__/test_getRandom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const currObject = require('../Backend/src/kMeans.js');


describe('getRandomPixels', () => {
it('should return an array of random pixels with the specified count', () => {
// Test input
const matrix = [
[100, 200, 300],
[400, 500, 600],
[700, 800, 900]
];
const K = 5;

// Call the getRandomPixels function
const result = currObject.getRandomPixels(matrix, K);

// Assertions
expect(result).toHaveLength(K);
result.forEach((pixel) => {
expect(pixel).toHaveLength(2);
expect(matrix.flat()).toContain(pixel[0]);
expect(pixel[1]).toBe(1);
});
});

it('should return an array of random pixels with the specified count', () => {
// Test input
const matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
const K = 3;

// Call the getRandomPixels function
const result = currObject.getRandomPixels(matrix, K);

// Assertions
expect(result).toHaveLength(K);
result.forEach((pixel) => {
expect(pixel).toHaveLength(2);
expect(matrix.flat()).toContain(pixel[0]);
expect(pixel[1]).toBe(1);
});
});

it('should return an empty array when K is 0', () => {
// Test input
const matrix = [
[10, 20],
[30, 40]
];
const K = 0;

// Call the getRandomPixels function
const result = currObject.getRandomPixels(matrix, K);

// Assertion
expect(result).toHaveLength(0);
});
});
76 changes: 0 additions & 76 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@
},
"devDependencies": {
"nodemon": "^2.0.22"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/mandeep-mahra/Image-Compression.git"
},
"bugs": {
"url": "https://github.com/mandeep-mahra/Image-Compression/issues"
},
"homepage": "https://github.com/mandeep-mahra/Image-Compression#readme"
}

0 comments on commit 6ad24f5

Please sign in to comment.