Skip to content

Commit

Permalink
feat(compare-images): add spatial tolerance
Browse files Browse the repository at this point in the history
Add an option to pass spatial tolerance when comparing image
direction matrix and origin for spatial overlap.

Also export `toScalarDouble` js function for external use.
  • Loading branch information
jadh4v committed Jul 18, 2024
1 parent 55dc91e commit 75f2db4
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/compare-images/compare-double-images.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ CompareImages(itk::wasm::Pipeline & pipeline, const TImage * testImage)
double differenceThreshold = 0.0;
pipeline.add_option("-d,--difference-threshold", differenceThreshold, "Intensity difference for pixels to be considered different.");

double spatialTolerance = 1e-8;
pipeline.add_option("-s,--spatial-tolerance", spatialTolerance, "Tolerance for comparing spatial overlap (origin and direction matrix).");

unsigned int radiusTolerance = 0;
pipeline.add_option("-r,--radius-tolerance", radiusTolerance, "Radius of the neighborhood around a pixel to search for similar intensity values.");

Expand All @@ -72,6 +75,9 @@ CompareImages(itk::wasm::Pipeline & pipeline, const TImage * testImage)

ITK_WASM_PARSE(pipeline);

itk::ImageToImageFilterCommon::SetGlobalDefaultDirectionTolerance(spatialTolerance);
itk::ImageToImageFilterCommon::SetGlobalDefaultCoordinateTolerance(spatialTolerance);

using DiffType = itk::Testing::ComparisonImageFilter<ImageType, ImageType>;
auto diff = DiffType::New();
diff->SetValidInput(testImage);
Expand Down Expand Up @@ -222,4 +228,4 @@ int main(int argc, char * argv[])

double>
::Dimensions<2U,3U,4U,5U,6U>("test-image", pipeline);
}
}

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def compare_double_images(
test_image: Image,
baseline_images: List[Image] = [],
difference_threshold: float = 0,
spatial_tolerance: float = 1e-08,
radius_tolerance: int = 0,
number_of_pixels_tolerance: int = 0,
ignore_boundary_pixels: bool = False,
Expand All @@ -27,6 +28,9 @@ def compare_double_images(
:param difference_threshold: Intensity difference for pixels to be considered different.
:type difference_threshold: float
:param spatial_tolerance: Tolerance for comparing spatial overlap (origin and direction matrix).
:type spatial_tolerance: float
:param radius_tolerance: Radius of the neighborhood around a pixel to search for similar intensity values.
:type radius_tolerance: int
Expand All @@ -46,5 +50,5 @@ def compare_double_images(
:rtype: Image
"""
func = environment_dispatch("itkwasm_compare_images", "compare_double_images")
output = func(test_image, baseline_images=baseline_images, difference_threshold=difference_threshold, radius_tolerance=radius_tolerance, number_of_pixels_tolerance=number_of_pixels_tolerance, ignore_boundary_pixels=ignore_boundary_pixels)
output = func(test_image, baseline_images=baseline_images, difference_threshold=difference_threshold, spatial_tolerance=spatial_tolerance, radius_tolerance=radius_tolerance, number_of_pixels_tolerance=number_of_pixels_tolerance, ignore_boundary_pixels=ignore_boundary_pixels)
return output
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async def compare_double_images_async(
test_image: Image,
baseline_images: List[Image] = [],
difference_threshold: float = 0,
spatial_tolerance: float = 1e-08,
radius_tolerance: int = 0,
number_of_pixels_tolerance: int = 0,
ignore_boundary_pixels: bool = False,
Expand All @@ -27,6 +28,9 @@ async def compare_double_images_async(
:param difference_threshold: Intensity difference for pixels to be considered different.
:type difference_threshold: float
:param spatial_tolerance: Tolerance for comparing spatial overlap (origin and direction matrix).
:type spatial_tolerance: float
:param radius_tolerance: Radius of the neighborhood around a pixel to search for similar intensity values.
:type radius_tolerance: int
Expand All @@ -46,5 +50,5 @@ async def compare_double_images_async(
:rtype: Image
"""
func = environment_dispatch("itkwasm_compare_images", "compare_double_images_async")
output = await func(test_image, baseline_images=baseline_images, difference_threshold=difference_threshold, radius_tolerance=radius_tolerance, number_of_pixels_tolerance=number_of_pixels_tolerance, ignore_boundary_pixels=ignore_boundary_pixels)
output = await func(test_image, baseline_images=baseline_images, difference_threshold=difference_threshold, spatial_tolerance=spatial_tolerance, radius_tolerance=radius_tolerance, number_of_pixels_tolerance=number_of_pixels_tolerance, ignore_boundary_pixels=ignore_boundary_pixels)
return output
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ interface CompareDoubleImagesNodeOptions {
/** Intensity difference for pixels to be considered different. */
differenceThreshold?: number

/** Tolerance for comparing spatial overlap (origin and direction matrix). */
spatialTolerance?: number

/** Radius of the neighborhood around a pixel to search for similar intensity values. */
radiusTolerance?: number

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ async function compareDoubleImagesNode(
if (typeof options.radiusTolerance !== "undefined") {
args.push('--radius-tolerance', options.radiusTolerance.toString())

}
if (typeof options.spatialTolerance !== "undefined") {
args.push('--spatial-tolerance', options.spatialTolerance.toString())

}
if (typeof options.numberOfPixelsTolerance !== "undefined") {
args.push('--number-of-pixels-tolerance', options.numberOfPixelsTolerance.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ interface CompareDoubleImagesOptions extends WorkerPoolFunctionOption {
/** Intensity difference for pixels to be considered different. */
differenceThreshold?: number

/** Tolerance for comparing spatial overlap (origin and direction matrix). */
spatialTolerance?: number

/** Radius of the neighborhood around a pixel to search for similar intensity values. */
radiusTolerance?: number

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ async function compareDoubleImages(
if (typeof options.radiusTolerance !== "undefined") {
args.push('--radius-tolerance', options.radiusTolerance.toString())

}
if (typeof options.spatialTolerance !== "undefined") {
args.push('--spatial-tolerance', options.spatialTolerance.toString())

}
if (typeof options.numberOfPixelsTolerance !== "undefined") {
args.push('--number-of-pixels-tolerance', options.numberOfPixelsTolerance.toString())
Expand Down
3 changes: 3 additions & 0 deletions packages/compare-images/typescript/src/index-node-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ export type { VectorMagnitudeNodeResult }

import vectorMagnitudeNode from './vector-magnitude-node.js'
export { vectorMagnitudeNode }

import toScalarDouble from './to-scalar-double.js'
export { toScalarDouble }
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class CompareDoubleImagesController {
model.options.set("differenceThreshold", parseFloat(differenceThresholdElement.value))
})

const spatialToleranceElement = document.querySelector('#compareDoubleImagesInputs sl-input[name=spatial-tolerance]')
spatialToleranceElement.addEventListener('sl-change', (event) => {
model.options.set("spatialTolerance", parseFloat(spatialToleranceElement.value))
})

const radiusToleranceElement = document.querySelector('#compareDoubleImagesInputs sl-input[name=radius-tolerance]')
radiusToleranceElement.addEventListener('sl-change', (event) => {
model.options.set("radiusTolerance", parseInt(radiusToleranceElement.value))
Expand Down

0 comments on commit 75f2db4

Please sign in to comment.