var scriptPath = VQ.getScriptPath().replace(/[/\\][^/\\]+$/, "");
var outputPath = "";
//---------------------------------------------------------
// Start - Only for internal testing (users can disregard or remove)
//---------------------------------------------------------
var isTestModule = scriptPath.indexOf("extern/t/TestModules") !== -1;
VQ.setTestMode(isTestModule);
function getArtifactOutputFolder() {
const cachePath = VQ.getCache();
return cachePath.substring(0, cachePath.lastIndexOf("/") + 1) + "artifacts";
}
if (VQ.isTestMode()) {
outputPath = getArtifactOutputFolder();
}
//---------------------------------------------------------
// End - Only for internal testing (users can disregard or remove)
//---------------------------------------------------------
//---------------------------------------------------------
// Histogram Tool example
//
// The Histogram tool (Tools > Histogram) plots how often each intensity occurs in an
// image. A script can drive it without opening it: choose the data and the bins,
// optionally restrict it to one ROI with the ROI filter, then read the counts or save
// them to a CSV file.
//---------------------------------------------------------
VQ.setConfig('Data/disableMIPViewer', true, true);
var dm = VQ.dataManager();
var mw = VQ.mainWin();
dm.openDat(0, [scriptPath + "/data/CTSynthetic.dcm"]);
if (outputPath == "") {
outputPath = scriptPath;
}
// Draw a spherical ROI in the middle of the image with the 3D ROI Tool
mw.setViewMode('Slice View', '3D ROI Tool');
var op = VQ.currentOp();
var roiID = op.addROI("Sphere", "red");
op.drawSphere(dm.getDimX()/2, dm.getDimY()/2, dm.getDimZ()/2, 10, roiID);
// Set up the histogram: data set 0, 64 bins over the full intensity range
var hist = VQ.histogramTool();
hist.setDataset(VQ.index(0));
hist.setNumBins(64);
hist.resetRange();
// Restrict it to the ROI. This turns the ROI Filter on; hist.setROI(-1) turns it off again.
hist.setROIByName("Sphere");
// Read the result: one count per bin, plus where each bin starts
var h = hist.getHistogram();
var total = 0, peak = 0;
for (var i = 0; i < h.counts.length; i++) {
total += h.counts[i];
if (h.counts[i] > h.counts[peak]) peak = i;
}
VQ.debug("Histogram summary: " + JSON.stringify({
bins: h.counts.length,
voxels: total,
mostCommonFrom: h.binStarts[peak],
mostCommonTo: h.binStarts[peak] + h.binStep
}));
// Save the counts (bin start, count per line) and a picture of the plot
hist.saveCSVFile(outputPath + "/HistogramTool-Example.csv");
hist.savePDFFile(outputPath + "/HistogramTool-Example.pdf");
// Show the tool so the result can be inspected
hist.show();
if (VQ.isTestMode()) {
VQ.debug("ROI voxels: " + VQ.getROIStats(0, roiID).voxels);
VQ.debug("CSV: " + outputPath + "/HistogramTool-Example.csv");
VQ.quit();
}