This tutorial extends the previous functionality by median filtering, background subtraction and intensity normalization, simply by adding more sentence definitions similar to the existing one for Gaussian blurring.
The new sentences also re-use the 'filter-size' type.
<!doctype html>
<html>
<body>
<!-- The HTML element that will hold the editor -->
<div id="nls-container"></div>
<!-- The only javascript file needed for nlScript -->
<script src="https://cdn.jsdelivr.net/npm/@nlscript/nlscript@0.3.0/dist/umd/nlScript.js"></script>
<!-- Load the library for the actual processing -->
<script src="preprocessing.js"></script>
<script>
let preprocessing = new Preprocessing("output");
preprocessing.setPixelWidth(0.25, "mm");
let parser = new nlScript.Parser();
let imageUnits = "";
parser.addParseStartListener(() => {
imageUnits = preprocessing.getUnits();
// At the start of parsing (remember this is done whenever auto-completion
// needs to be performed), the 'units' type is undefined and then
// re-defined, according to the pixel calibration unit string of the current image:
parser.undefineType("units");
parser.defineType("units", "pixel(s)", pn => false);
parser.defineType("units", imageUnits, pn => true);
});
parser.defineType("units", "pixel(s)", pn => false);
parser.defineType("filter-size", "{stddev:float} {units:units}", pn => {
let stddev = pn.evaluate("stddev");
let units = pn.evaluate("units");
if(units)
stddev /= preprocessing.getPixelWidth();
return stddev;
}, true);
// Gaussian Blurring
parser.defineSentence(
"Apply Gaussian blurring with a standard deviation of {stddev:filter-size}.",
pn => {
let stdDev = pn.evaluate("stddev");
preprocessing.gaussianBlur(stdDev);
preprocessing.show("output");
return undefined;
});
// Median filtering
parser.defineSentence(
"Apply Median filtering with a window of radius {window-size:filter-size}.",
pn => {
let windowSize = pn.evaluate("window-size");
preprocessing.medianFilter(Math.round(windowSize));
preprocessing.show("output");
return undefined;
});
// Intensity normalization
parser.defineSentence(
"Normalize intensities.",
pn => {
preprocessing.intensityNormalization();
preprocessing.show("output");
return undefined;
});
// Background subtraction
parser.defineSentence(
"Subtract the background with a standard deviation of {window-size:filter-size}.",
pn => {
let windowSize = pn.evaluate("window-size");
preprocessing.subtractBackground(Math.round(windowSize));
preprocessing.show("output");
return undefined;
});
new nlScript.ACEditor(parser, document.getElementById("nls-container"));
</script>
</body>
</html>