-
-
Save freegroup/380adf00adf87432cc3e627f3904d205 to your computer and use it in GitHub Desktop.
Revisions
-
zz85 revised this gist
Nov 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ // Read https://en.wikipedia.org/wiki/Otsu%27s_method (added this since JS examples in wikipedia didn't work) function otsu(histData /* Array of 256 greyscale values */, total /* Total number of pixels */) { let sum = 0; -
zz85 created this gist
Nov 7, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ // Read https://en.wikipedia.org/wiki/Otsu%27s_method function otsu(histData /* Array of 256 greyscale values */, total /* Total number of pixels */) { let sum = 0; for (let t=0 ; t<256 ; t++) sum += t * histData[t]; let sumB = 0; let wB = 0; let wF = 0; let varMax = 0; let threshold = 0; for (let t=0 ; t<256 ; t++) { wB += histData[t]; // Weight Background if (wB == 0) continue; wF = total - wB; // Weight Foreground if (wF == 0) break; sumB += t * histData[t]; let mB = sumB / wB; // Mean Background let mF = (sum - sumB) / wF; // Mean Foreground // Calculate Between Class Variance let varBetween = wB * wF * (mB - mF) * (mB - mF); // Check if new maximum found if (varBetween > varMax) { varMax = varBetween; threshold = t; } } return threshold; }