-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compare-images): add compare-images-node.ts
- Loading branch information
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 characters
64 changes: 64 additions & 0 deletions
64
packages/compare-images/typescript/src/compare-images-node.ts
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 | ||
} | ||
|
||
/** | ||
* 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<CompareDoubleImagesNodeResult>} - result object | ||
*/ | ||
async function compareImagesNode( | ||
testImage: Image, | ||
options: CompareDoubleImagesOptions = { baselineImages: [] as Image[], } | ||
) : Promise<CompareDoubleImagesNodeResult> { | ||
|
||
const testImageDouble = await _toScalarDouble(testImage) | ||
const baselineImagesDouble = await Promise.all(options.baselineImages.map(async (image) => { | ||
return await _toScalarDouble(image) | ||
})) | ||
|
||
const otherOptions = { ...options } | ||
otherOptions.baselineImages = baselineImagesDouble | ||
|
||
return compareDoubleImagesNode(testImageDouble, otherOptions) | ||
} | ||
|
||
export default compareImagesNode |
This file contains 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 characters
51 changes: 51 additions & 0 deletions
51
packages/compare-images/typescript/test/node/compare-images-test.js
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import test from 'ava' | ||
import path from 'path' | ||
|
||
import { compareImagesNode } from '../../dist/bundles/compare-images-node.js' | ||
import { readImageLocalFile } from 'itk-wasm' | ||
|
||
const inputPathPrefix = '../test/data/input/' | ||
|
||
test('compareImagesNode produces the expected metrics and difference images for uint8 inputs', async t => { | ||
const testImageFile = 'cake_easy.png' | ||
const testImagePath = path.join(inputPathPrefix, testImageFile) | ||
const testImage = await readImageLocalFile(testImagePath) | ||
|
||
const baselineImageFile = 'cake_hard.png' | ||
const baselineImagePath = path.join(inputPathPrefix, baselineImageFile) | ||
const baselineImage = await readImageLocalFile(baselineImagePath) | ||
|
||
const { metrics, differenceImage, differenceUchar2dImage } = await compareImagesNode(testImage, { baselineImages: [baselineImage,] }) | ||
|
||
t.is(metrics.almostEqual, false) | ||
t.is(metrics.numberOfPixelsWithDifferences, 9915) | ||
t.is(metrics.minimumDifference, 1.0) | ||
t.is(metrics.maximumDifference, 107.0) | ||
t.is(metrics.totalDifference, 337334.0) | ||
t.is(metrics.meanDifference, 34.02259203227433) | ||
|
||
t.is(differenceImage.imageType.componentType, 'float64') | ||
t.is(differenceUchar2dImage.imageType.componentType, 'uint8') | ||
}) | ||
|
||
test('compareImagesNode produces the expected metrics and difference images for rgb inputs', async t => { | ||
const testImageFile = 'apple.jpg' | ||
const testImagePath = path.join(inputPathPrefix, testImageFile) | ||
const testImage = await readImageLocalFile(testImagePath) | ||
|
||
const baselineImageFile = 'orange.jpg' | ||
const baselineImagePath = path.join(inputPathPrefix, baselineImageFile) | ||
const baselineImage = await readImageLocalFile(baselineImagePath) | ||
|
||
const { metrics, differenceImage, differenceUchar2dImage } = await compareImagesNode(testImage, { baselineImages: [baselineImage,] }) | ||
|
||
t.is(metrics.almostEqual, false) | ||
t.is(metrics.numberOfPixelsWithDifferences, 26477) | ||
t.is(metrics.minimumDifference, 0.002273026683894841) | ||
t.is(metrics.maximumDifference, 312.2511648746159) | ||
t.is(metrics.totalDifference, 3121703.1639738297) | ||
t.is(metrics.meanDifference, 117.90244982338746) | ||
|
||
t.is(differenceImage.imageType.componentType, 'float64') | ||
t.is(differenceUchar2dImage.imageType.componentType, 'uint8') | ||
}) |