Skip to content

Commit

Permalink
feat(compare-images): add compare-images.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Aug 21, 2023
1 parent 3fcb0f7 commit 9fdf36b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 29 deletions.
33 changes: 4 additions & 29 deletions packages/compare-images/typescript/src/compare-images-node.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
import {
Image,
FloatTypes,
PixelTypes,
castImage,
} from 'itk-wasm'

import CompareDoubleImagesOptions from './compare-double-images-options.js'
import CompareDoubleImagesNodeResult from './compare-double-images-node-result.js'
import compareDoubleImagesNode from './compare-double-images-node.js'
import vectorMagnitudeNode from './vector-magnitude-node.js'

async function _toScalarDouble(image: Image): Promise<Image> {
let scalarDouble = image

if (scalarDouble.imageType.componentType !== FloatTypes.Float64) {
let pixelType = undefined
if (image.imageType.pixelType !== PixelTypes.Scalar && image.imageType.pixelType !== PixelTypes.VariableLengthVector) {
pixelType = PixelTypes.VariableLengthVector
}
scalarDouble = castImage(image, { componentType: FloatTypes.Float64, pixelType })
} else {
if (image.imageType.pixelType !== PixelTypes.Scalar && image.imageType.pixelType !== PixelTypes.VariableLengthVector) {
const pixelType = PixelTypes.VariableLengthVector
scalarDouble = castImage(image, { pixelType })
}
}

if (scalarDouble.imageType.pixelType === PixelTypes.VariableLengthVector) {
const magnitude = await vectorMagnitudeNode(scalarDouble)
scalarDouble = magnitude.magnitudeImage
}

return scalarDouble
}
import toScalarDouble from './to-scalar-double.js'
import vectorMagnitudeNode from './vector-magnitude-node.js'

/**
* Compare images with a tolerance for regression testing.
Expand All @@ -50,9 +25,9 @@ async function compareImagesNode(
options: CompareDoubleImagesOptions = { baselineImages: [] as Image[], }
) : Promise<CompareDoubleImagesNodeResult> {

const testImageDouble = await _toScalarDouble(testImage)
const testImageDouble = await toScalarDouble(vectorMagnitudeNode, testImage)
const baselineImagesDouble = await Promise.all(options.baselineImages.map(async (image) => {
return await _toScalarDouble(image)
return await toScalarDouble(vectorMagnitudeNode, image)
}))

const otherOptions = { ...options }
Expand Down
46 changes: 46 additions & 0 deletions packages/compare-images/typescript/src/compare-images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
Image,
} from 'itk-wasm'

import CompareDoubleImagesOptions from './compare-double-images-options.js'
import CompareDoubleImagesResult from './compare-double-images-result.js'
import compareDoubleImages from './compare-double-images.js'

import toScalarDouble from './to-scalar-double.js'
import vectorMagnitude from './vector-magnitude.js'

/**
* Compare images with a tolerance for regression testing.
*
* For multi-component images, the intensity difference threshold
* is based on the pixel vector magnitude.
*
* @param {Image} testImage - The input test image
* @param {CompareDoubleImagesOptions} options - options object
*
* @returns {Promise<CompareDoubleImagesResult>} - result object
*/
async function compareImages(
webWorker: null | Worker,
testImage: Image,
options: CompareDoubleImagesOptions = { baselineImages: [] as Image[], }
) : Promise<CompareDoubleImagesResult> {

async function vectorMagnitudeWorker(image: Image) {
const { webWorker: usedWebWorker, magnitudeImage } = await vectorMagnitude(null, image)
usedWebWorker?.terminate()
return { magnitudeImage }
}

const testImageDouble = await toScalarDouble(vectorMagnitudeWorker, testImage)
const baselineImagesDouble = await Promise.all(options.baselineImages.map(async (image) => {
return await toScalarDouble(vectorMagnitudeWorker, image)
}))

const otherOptions = { ...options }
otherOptions.baselineImages = baselineImagesDouble

return compareDoubleImages(webWorker, testImageDouble, otherOptions)
}

export default compareImages
3 changes: 3 additions & 0 deletions packages/compare-images/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ export type { CompareDoubleImagesOptions }

import compareDoubleImages from './compare-double-images.js'
export { compareDoubleImages }

import compareImages from './compare-images.js'
export { compareImages }
34 changes: 34 additions & 0 deletions packages/compare-images/typescript/src/to-scalar-double.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
Image,
FloatTypes,
PixelTypes,
castImage,
} from 'itk-wasm'

import VectorMagnitudeNodeResult from './vector-magnitude-node-result'

async function toScalarDouble(vectorMagnitudeFn: (img: Image) => Promise<VectorMagnitudeNodeResult>, image: Image): Promise<Image> {
let scalarDouble = image

if (scalarDouble.imageType.componentType !== FloatTypes.Float64) {
let pixelType = undefined
if (image.imageType.pixelType !== PixelTypes.Scalar && image.imageType.pixelType !== PixelTypes.VariableLengthVector) {
pixelType = PixelTypes.VariableLengthVector
}
scalarDouble = castImage(image, { componentType: FloatTypes.Float64, pixelType })
} else {
if (image.imageType.pixelType !== PixelTypes.Scalar && image.imageType.pixelType !== PixelTypes.VariableLengthVector) {
const pixelType = PixelTypes.VariableLengthVector
scalarDouble = castImage(image, { pixelType })
}
}

if (scalarDouble.imageType.pixelType === PixelTypes.VariableLengthVector) {
const magnitude = await vectorMagnitudeFn(scalarDouble)
scalarDouble = magnitude.magnitudeImage
}

return scalarDouble
}

export default toScalarDouble

0 comments on commit 9fdf36b

Please sign in to comment.