661. Image Smoother Flashcards
can you set up variables for img.length, img[0].length?
let m = img.length, n = img[0].length;
can you set up variable res for empty two dimensional array?
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
can you set up variable dir for adjacent cells of current cell in 8 directions?
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
can you set up a function smooth that takes coordinates (i, j)?
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
}
can you set up variable sum with initial value current img cell, variable count with initial value 1 in smooth function?
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
}
can you loop through directions in dir in smooth function?
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {
} }
can you check in the dir loop, if adjacent cell is undefined, if not, update sum with the adjacent cell value, increment count
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {
if (img[i + r] !== undefined && img[i + r][j + c] !== undefined) {
sum += img[i + r][j + c];
count++;
}
}
}
can you return average of the sum in the smooth function?
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {
if (img[i + r] !== undefined && img[i + r][j + c] !== undefined) {
sum += img[i + r][j + c];
count++;
}
}
return sum / count | 0;
}
can you loop through each cell as input in smooth function, assign smooth function return value to empty two dimensional array?
let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {
if (img[i + r] !== undefined && img[i + r][j + c] !== undefined) {
sum += img[i + r][j + c];
count++;
}
}
return sum / count | 0;
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
res[i][j] = smooth(i, j);
}
}
can you return the two dimensional array?
return res;