Last active
July 12, 2017 09:38
-
-
Save whichlight/6445704 to your computer and use it in GitHub Desktop.
pixel smoothing and moving average in javascript, for Kinect depth frame buffers in https://github.com/whichlight/figure-sketching-kinect/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from http://www.codeproject.com/Articles/317974/KinectDepthSmoothing | |
function smoothDepth(arr){ | |
var res = createArray(width,height); | |
for(var i=2; i< width-2; i++){ | |
for(var j=2; j < height-2; j++){ | |
if(arr[i][j]===0){ | |
var innerBandCount = 0; | |
var outerBandCount = 0; | |
var filterCollection = []; | |
for (var y = -2; y < 3; y++) | |
{ | |
for (var x = -2; x < 3; x++) | |
{ | |
if (x != 0 || y != 0) | |
{ | |
var i2 = i + x; | |
var j2 = j + y; | |
if(arr[i2][j2]!=0){ | |
filterCollection.push(arr[i2][j2]); | |
if (y != 2 && y != -2 && x != 2 && x != -2){ | |
innerBandCount++; | |
} | |
else{ | |
outerBandCount++; | |
} | |
} | |
} | |
} | |
} | |
if (innerBandCount >= innerBandThreshold || outerBandCount >= outerBandThreshold) | |
{ | |
res[i][j] = mode(filterCollection); | |
} | |
else{ | |
res[i][j] = arr[i][j]; | |
} | |
} | |
else{ | |
res[i][j] = arr[i][j]; | |
} | |
} | |
} | |
return res; | |
} | |
function movingAverage(arr, movingAverageWindow){ | |
depthQueue.push(arr); | |
if (depthQueue.length > movingAverageWindow){ | |
depthQueue.shift(); | |
} | |
var denominator = 0; | |
var count = 1; | |
var sumDepthArray = createArray(width,height); | |
var averagedDepthArray = createArray(width,height); | |
for(var i=0; i<width; i++){ | |
for( var j=0; j<height; j++){ | |
sumDepthArray[i][j] =0; | |
} | |
} | |
for(var layer=depthQueue.length-1; layer >=0 ; layer--){ | |
for(var i=0; i<width; i++){ | |
for( var j=0; j<height; j++){ | |
sumDepthArray[i][j] += depthQueue[layer][i][j] * count; | |
} | |
} | |
denominator+=count; | |
count++; | |
} | |
for(var i=0; i<width; i++){ | |
for( var j=0; j<height; j++){ | |
averagedDepthArray[i][j] = sumDepthArray[i][j]/denominator; | |
} | |
} | |
return averagedDepthArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment