From 9eac21897a10cd5dbfc94fe03560d5414b28afc2 Mon Sep 17 00:00:00 2001 From: aashna27 Date: Thu, 13 Jun 2019 22:39:13 +0530 Subject: [PATCH] Added noise reduction module (#1105) * Added Noise Removal Module * Remove ChangePixel Function --- src/Modules.js | 1 + src/modules/NoiseReduction/Module.js | 38 +++++++++ src/modules/NoiseReduction/NoiseReduction.js | 81 ++++++++++++++++++++ src/modules/NoiseReduction/index.js | 4 + src/modules/NoiseReduction/info.json | 13 ++++ test/core/modules/noise-reduction.js | 4 + 6 files changed, 141 insertions(+) create mode 100644 src/modules/NoiseReduction/Module.js create mode 100644 src/modules/NoiseReduction/NoiseReduction.js create mode 100644 src/modules/NoiseReduction/index.js create mode 100644 src/modules/NoiseReduction/info.json create mode 100644 test/core/modules/noise-reduction.js diff --git a/src/Modules.js b/src/Modules.js index ef56d4f5..b1193d6b 100644 --- a/src/Modules.js +++ b/src/Modules.js @@ -31,6 +31,7 @@ module.exports = { 'invert': require('image-sequencer-invert'), 'ndvi': require('./modules/Ndvi'), 'ndvi-colormap': require('./modules/NdviColormap'), + 'noise-reduction': require('./modules/NoiseReduction'), 'paint-bucket': require('./modules/PaintBucket'), 'overlay': require('./modules/Overlay'), 'replace-color': require('./modules/ReplaceColor'), diff --git a/src/modules/NoiseReduction/Module.js b/src/modules/NoiseReduction/Module.js new file mode 100644 index 00000000..54a9abb5 --- /dev/null +++ b/src/modules/NoiseReduction/Module.js @@ -0,0 +1,38 @@ +module.exports = function NoiseReduction(options, UI){ + var defaults = require('./../../util/getDefaults.js')(require('./info.json')); + var output; + + function draw(input,callback,progressObj){ + + progressObj.stop(true); + progressObj.overrideFlag = true; + + var step = this; + options.method = options.method || defaults.method; + + function extraManipulation(pixels) { + pixels = require('./NoiseReduction.js')(pixels, options.method); + return pixels; + } + + function output(image, datauri, mimetype){ + // This output is accessible by Image Sequencer + step.output = { src: datauri, format: mimetype }; + + } + + return require('../_nomodule/PixelManipulation.js')(input, { + output: output, + extraManipulation: extraManipulation, + format: input.format, + image: options.image, + callback: callback + }); + } + return { + options: options, + draw: draw, + output: output, + UI: UI + }; +}; \ No newline at end of file diff --git a/src/modules/NoiseReduction/NoiseReduction.js b/src/modules/NoiseReduction/NoiseReduction.js new file mode 100644 index 00000000..82f037e1 --- /dev/null +++ b/src/modules/NoiseReduction/NoiseReduction.js @@ -0,0 +1,81 @@ +module.exports = function Noise(pixels, method) { + let neighbourX = [-1, -1, -1, 0, 0, 0, 1, 1, 1]; + let neighbourY = [-1, 0, 1, -1, 0, 1, -1, 0, 1]; + + if(method == 'Median Filtering'){ + + for(let y = 0; y < pixels.shape[1]; y++){ + for(let x = 0; x < pixels.shape[0]; x++){ + let i = 0, k = 0, windowr = [],windowg = [], windowb = []; + + while( k <= 8){ + let newX = x + neighbourX[k], newY = y + neighbourY[k]; + + if( newX >= 0 && newX < pixels.shape[0]) + { + if( newY >= 0 && newY < pixels.shape[1]){ + windowr[i] = pixels.get((newX),(newY),0); + windowg[i] = pixels.get((newX),(newY),1); + windowb[i] = pixels.get((newX),(newY),2); + i++; + } + } + k++; + } + + windowr.sort(); + windowg.sort(); + windowb.sort(); + + if(i%2 == 0) + { + let value = windowr[i/2] + windowr[(i/2)-1]; + pixels.set(x,y,0,value); + + value = windowg[i/2] + windowg[(i/2)-1]; + pixels.set(x,y,1,value); + + value = windowb[i/2] + windowb[(i/2)-1]; + pixels.set(x,y,2,value); + + } + else { + pixels.set(x, y, 0, windowr[Math.floor(i/2)]); + pixels.set(x, y, 1, windowg[Math.floor(i/2)]); + pixels.set(x, y, 2, windowb[Math.floor(i/2)]); + + } + } + } + } + else if(method == 'Mean Filtering'){ + + for(let y = 0; y < pixels.shape[1]; y++){ + for(let x = 0; x < pixels.shape[0]; x++){ + let i = 0, k = 0, sumR=0, sumG = 0, sumB =0; + + while( k <= 8){ + let newX = x + neighbourX[k], newY = y + neighbourY[k]; + + if( newX >= 0 && newX < pixels.shape[0]) + { + if( newY >= 0 && newY < pixels.shape[1]){ + sumR += pixels.get(newX,newY,0); + sumG += pixels.get(newX,newY,1); + sumB += pixels.get(newX,newY,2); + i++; + } + } + k++; + } + + pixels.set(x, y, 0, sumR/i); + pixels.set(x, y, 1, sumG/i); + pixels.set(x, y, 2, sumB/i); + + } + } + } + + return pixels; +}; \ No newline at end of file diff --git a/src/modules/NoiseReduction/index.js b/src/modules/NoiseReduction/index.js new file mode 100644 index 00000000..71549002 --- /dev/null +++ b/src/modules/NoiseReduction/index.js @@ -0,0 +1,4 @@ +module.exports = [ + require('./Module'), + require('./info.json') +]; \ No newline at end of file diff --git a/src/modules/NoiseReduction/info.json b/src/modules/NoiseReduction/info.json new file mode 100644 index 00000000..03779a44 --- /dev/null +++ b/src/modules/NoiseReduction/info.json @@ -0,0 +1,13 @@ +{ + "name": "Noise Reduction", + "description": "Reduces noise from Image", + "inputs": { + "method": { + "type": "select", + "desc": "Select the noise filtering method", + "default": "Median Filtering", + "values": ["Mean Filtering","Median Filtering"] + } + }, + "docs-link":"" +} diff --git a/test/core/modules/noise-reduction.js b/test/core/modules/noise-reduction.js new file mode 100644 index 00000000..2202ae1c --- /dev/null +++ b/test/core/modules/noise-reduction.js @@ -0,0 +1,4 @@ +const testModule = require('../templates/module-test'), + benchmark = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAklEQVR4AewaftIAAAApSURBVKXBAQEAAAiDMKR/5xuC7QYjkEgiiSSSSCKJJJJIIokkkkgiiR5YbQIegx78CAAAAABJRU5ErkJggg=='; + +testModule('noise-reduction', {method: 'Median Filtering'}, benchmark);