Improve Code Quality and Optimize (#763)

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>
This commit is contained in:
Varun Gupta
2019-02-16 11:05:32 +05:30
committed by GitHub
parent f42aa2fd67
commit 75dc23fb55
26 changed files with 1332 additions and 921 deletions

View File

@@ -1,9 +1,9 @@
var _ = require('lodash');
module.exports = exports = function(pixels, constantFactor, kernelValues){
let kernel = kernelGenerator(constantFactor, kernelValues), oldpix = _.cloneDeep(pixels);
kernel = flipKernel(kernel);
module.exports = exports = function(pixels, constantFactor, kernelValues) {
let kernel = kernelGenerator(constantFactor, kernelValues), oldpix = _.cloneDeep(pixels);
kernel = flipKernel(kernel);
for (let i = 0; i < pixels.shape[0]; i++) {
for (let i = 0; i < pixels.shape[0]; i++) {
for (let j = 0; j < pixels.shape[1]; j++) {
let neighboutPos = getNeighbouringPixelPositions([i, j]);
let acc = [0.0, 0.0, 0.0, 0.0];
@@ -15,9 +15,9 @@ module.exports = exports = function(pixels, constantFactor, kernelValues){
acc[3] += (oldpix.get(neighboutPos[a][b][0], neighboutPos[a][b][1], 3) * kernel[a][b]);
}
}
acc[0] = acc[0]%255;
acc[1] = acc[1]%255;
acc[2] = acc[2]%255;
acc[0] = Math.min(acc[0], 255);
acc[1] = Math.min(acc[1], 255);
acc[2] = Math.min(acc[2], 255);
pixels.set(i, j, 0, acc[0]);
pixels.set(i, j, 1, acc[1]);
pixels.set(i, j, 2, acc[2]);
@@ -26,25 +26,25 @@ module.exports = exports = function(pixels, constantFactor, kernelValues){
return pixels;
function kernelGenerator(constantFactor, kernelValues){
kernelValues = kernelValues.split(" ");
for(i = 0 ; i < 9; i++){
function kernelGenerator(constantFactor, kernelValues) {
kernelValues = kernelValues.split(" ");
for (i = 0; i < 9; i++) {
kernelValues[i] = Number(kernelValues[i]) * constantFactor;
}
let k = 0;
let arr = [];
for(i = 0; i < 3; i++){
let columns = [];
for(j = 0; j < 3; j++){
columns.push(kernelValues[k]);
k += 1;
}
arr.push(columns);
}
return arr;
}
let arr = [];
for (i = 0; i < 3; i++) {
let columns = [];
for (j = 0; j < 3; j++) {
columns.push(kernelValues[k]);
k += 1;
}
arr.push(columns);
}
return arr;
}
function getNeighbouringPixelPositions(pixelPosition) {
function getNeighbouringPixelPositions(pixelPosition) {
let x = pixelPosition[0], y = pixelPosition[1], result = [];
for (let i = -1; i <= 1; i++) {
@@ -57,7 +57,7 @@ module.exports = exports = function(pixels, constantFactor, kernelValues){
return result;
}
function flipKernel(kernel) {
function flipKernel(kernel) {
let result = [];
for (let i = kernel.length - 1; i >= 0; i--) {
let arr = [];