Signed-off-by: Varun Gupta <varun.gupta1798@gmail.com>
This commit is contained in:
Varun Gupta
2018-10-06 22:36:15 +05:30
committed by Jeffrey Warren
parent 2e7e042baa
commit 768117a078
4 changed files with 39 additions and 43 deletions

View File

@@ -4,9 +4,8 @@ const _ = require('lodash')
const kernelx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
kernely = [[-1, -2, -1], [0, 0, 0], [1, 2, 1]];
let angles = [], mags = [], strongEdgePixels = [], weakEdgePixels = [], notInUI;
module.exports = function(pixels, highThresholdRatio, lowThresholdRatio, inBrowser) {
notInUI = !inBrowser;
let angles = [], mags = [], strongEdgePixels = [], weakEdgePixels = [], notInUI = !inBrowser;
for (var x = 0; x < pixels.shape[0]; x++) {
angles.push([]);
mags.push([]);
@@ -29,8 +28,9 @@ module.exports = function(pixels, highThresholdRatio, lowThresholdRatio, inBrows
angles.slice(-1)[0].push(result.angle);
}
}
return doubleThreshold(nonMaxSupress(pixels), highThresholdRatio, lowThresholdRatio);
nonMaxSupress(pixels, mags, angles);
doubleThreshold(pixels, highThresholdRatio, lowThresholdRatio, mags, strongEdgePixels, weakEdgePixels);
return pixels;
}
//changepixel function that convolutes every pixel (sobel filter)
@@ -65,7 +65,7 @@ function changePixel(pixels, val, a, x, y) {
}
//Non Maximum Supression without interpolation
function nonMaxSupress(pixels) {
function nonMaxSupress(pixels, mags, angles) {
angles = angles.map((arr) => arr.map(convertToDegrees));
@@ -113,7 +113,6 @@ function nonMaxSupress(pixels) {
}
}
return pixels;
}
//Converts radians to degrees
var convertToDegrees = radians => (radians * 180) / Math.PI;
@@ -122,9 +121,9 @@ var convertToDegrees = radians => (radians * 180) / Math.PI;
var findMaxInMatrix = arr => Math.max(...arr.map(el => el.map(val => !!val ? val : 0)).map(el => Math.max(...el)));
//Applies the double threshold to the image
function doubleThreshold(pixels, highThresholdRatio, lowThresholdRatio) {
function doubleThreshold(pixels, highThresholdRatio, lowThresholdRatio, mags, strongEdgePixels, weakEdgePixels) {
const highThreshold = findMaxInMatrix(mags) * 0.2;
const highThreshold = findMaxInMatrix(mags) * highThresholdRatio;
const lowThreshold = highThreshold * lowThresholdRatio;
for (let i = 0; i < pixels.shape[0]; i++) {
@@ -140,8 +139,6 @@ function doubleThreshold(pixels, highThresholdRatio, lowThresholdRatio) {
}
strongEdgePixels.forEach(pix => pixels.set(pix[0], pix[1], 3, 255));
return pixels;
}
// hysteresis edge tracking algorithm -- not working as of now