({{, (RefLink[ImageDistance, paclet:ref/ImageDistance])[(Subscript[image, 1]),(Subscript[image, 2])] 
returns a distance measure between (Subscript[image, 1]) and (Subscript[image, 2]). }, {, (RefLink[ImageDistance, paclet:ref/ImageDistance])[(Subscript[image, 1]),(Subscript[image, 2]),pos]
places the center of (Subscript[image, 2]) at position pos in (Subscript[image, 1]).}, {, (RefLink[ImageDistance, paclet:ref/ImageDistance])[(Subscript[image, 1]),(Subscript[image, 2]),(Subscript[pos, 1]),(Subscript[pos, 2])]
places the point (Subscript[pos, 2]) of (Subscript[image, 2]) at position (Subscript[pos, 1]) in (Subscript[image, 1]).}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[ImageDistance, paclet:ref/ImageDistance])[(Subscript[image, 1]),(Subscript[image, 2])] centers (Subscript[image, 2]) in (Subscript[image, 1]) and returns the distance between the overlapping regions in the two images.

(RefLink[ImageDistance, paclet:ref/ImageDistance]) works with arbitrary 2D and 3D images.

Images should either have the same number of channels or one should be a single-channel image. If either of (Subscript[image, 1]) or (Subscript[image, 2]) is a single-channel image, the channel is replicated to match the number of channels in the other image.

Position specification pos can be of the form:

If alignment along each axis is not given, it is assumed to be (RefLink[Center, paclet:ref/Center]).

The following options can be given:

Some typical distance function settings include:

The following special distance functions are also supported:

Using (RefLink[Masking, paclet:ref/Masking])->roi, a region of interest in (Subscript[image, 1]) is specified. With (RefLink[Masking, paclet:ref/Masking])->{(Subscript[roi, 1]),(Subscript[roi, 2])}, the intersection of (Subscript[roi, 1]) and (Subscript[roi, 2]) on the overlapped images is used.

Predefined (RefLink[ImageDistance, paclet:ref/ImageDistance]) metrics are symmetric and non-negative. However, some distances may not satisfy the triangle inequality. The distance between two images can be 0 with some methods, even if they are not identical. User-defined functions might break these properties.

If there are no overlapping regions or the measure cannot be determined, (RefLink[Indeterminate, paclet:ref/Indeterminate]) is returned. »

OpenersPrimaryExamplesSection(Spacer1)Examples  (22)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Euclidean distance between two images:

ImageDistance[,]

Euclidean distance between 3D images:

ImageDistance[,]

OpenersExampleSection1(Spacer1)Scope  (2)

By default, images are center aligned:

{i1,i2}={,};

ImageDistance[i1,i2]

Align the top-left corner of the first image with the center of the second image:

ImageDistance[i1,i2,{Left,Top}]

Align the center top of the first image and right center of the second image:

ImageDistance[i1,i2,Top,Right]

Specify positions using the standard coordinate system:

ImageDistance[i1,i2,{66.5,6},{21.5,2}]

Specify positions using scaled coordinates:

ImageDistance[i1,i2,Scaled[{0,1/2}],Scaled[{.2,1/2}]]

Specify the alignment position for 3D images:

ImageDistance[,,{Right,Front,Top}]

OpenersExampleSection2(Spacer1)Options  (8)

OpenersExampleSubsection0(Spacer1)DistanceFunction  (5)

Use a custom distance function:

f[i1_,i2_]:=Abs[Mean@Flatten@ImageData[i1]-Mean@Flatten@ImageData@i2];
ImageDistance[,,DistanceFunction->f]

Use a custom distance function with masks:

f[i1_,i2_,mask_]:=With[{pos=PixelValuePositions[mask,1]},Abs[Mean@Flatten@PixelValue[i1,pos]-Mean@Flatten@PixelValue[i2,pos]]
];
ImageDistance[,,DistanceFunction->f,Masking->{,}]

Earth mover distance:

ImageDistance[,,DistanceFunction->"EarthMoverDistance"]

Use histograms with four bins per channel:

ImageDistance[,,DistanceFunction->{"EarthMoverDistance",4}]

Compare different distance measures:

flist={EuclideanDistance,SquaredEuclideanDistance,NormalizedSquaredEuclideanDistance,ManhattanDistance,CosineDistance,CorrelationDistance,"MeanEuclideanDistance","MeanSquaredEuclideanDistance","RootMeanSquare","MeanReciprocalSquaredEuclideanDistance",{"MeanReciprocalSquaredEuclideanDistance",.3},"MutualInformationVariation",{"MutualInformationVariation",32},"NormalizedMutualInformationVariation",{"NormalizedMutualInformationVariation",32},"DifferenceNormalizedEntropy",{"DifferenceNormalizedEntropy",32},"MeanPatternIntensity",{"MeanPatternIntensity",.1,3},"GradientCorrelation","MeanReciprocalGradientDistance","EarthMoverDistance",{"EarthMoverDistance",8}};
Grid[Table[{f,ImageDistance[,,DistanceFunction->f]},
{f,flist}],Alignment->{{Right,Left},Center}]

Compute the root mean square distance of approximate perceptual distances between pixels:

f[image1_,image2_]:=RootMeanSquare[Flatten@ImageData@ColorDistance[image1,image2]];
ImageDistance[,,DistanceFunction->f]

OpenersExampleSubsection1(Spacer1)Masking  (3)

Use a mask to specify the region of interest in the first image:

ImageDistance[,,Masking->]

Use a mask to specify the region of interest in the second image:

ImageDistance[,,Masking->{All,Graphics[Disk[]]}]

Use masks for both images:

ImageDistance[,,Masking->{,Graphics[Disk[]]}]

OpenersExampleSection3(Spacer1)Applications  (2)

Construct a similarity graph of a set of images based on the earth mover distance:

l={,,,,,,,};

Compute a matrix of distances for all image pairs:

distances=Table[ImageDistance[l[[i]],l[[j]],DistanceFunction->"EarthMoverDistance"],{i,Length[l]},{j,i+1,Length[l]}];
With[{mtemp=PadLeft[#,Length[l]]&/@distances},
distmatrix=mtemp+Transpose[mtemp]];
NumberForm[distmatrix,3]

Threshold the distances to construct an adjacency matrix and visualize using a graph:

adjmatrix=1-Unitize[Threshold[distmatrix,Quantile[Flatten[distances],1/3]]];
GraphPlot[adjmatrix,VertexRenderingFunction->(Inset[l[[#2]],#,Center,.5]&),SelfLoopStyle->None,Method->"SpringEmbedding",ImageSize->500]

Register two images related by a translation and a rotation:

moving=;
fixed=;
{w,h}=ImageDimensions[fixed];

objfun[θ_Real,tx_Real,ty_Real]:=
ImageDistance[
fixed,
ImagePerspectiveTransformation[moving,TransformationFunction[(({{Cos[θ], -Sin[θ], tx}, {Sin[θ], Cos[θ], ty}, {0, 0, 1}}))],DataRange->Full,PlotRange->{{0,w},{0,h}},Padding->0]
];

NMinimize[
{objfun[θ,tx,ty],0≤θ<2*Pi&&0≤tx≤100&&0≤ty≤100},{{θ,Pi/7,Pi/5},{tx,50,80},{ty,0,10}},
MaxIterations->50,
WorkingPrecision->3
]

ImagePerspectiveTransformation[moving,TransformationFunction[(({{Cos[θ], -Sin[θ], tx}, {Sin[θ], Cos[θ], ty}, {0, 0, 1}}))]/.%[[2]],DataRange->Full,PlotRange->{{0,w},{0,h}},Padding->0]

ImageDifference[%,fixed]

OpenersExampleSection4(Spacer1)Properties & Relations  (8)

The distance between an image and itself is always zero:

img=;
ImageDistance[img,img]

The distance between two different images may be 0:

ImageDistance[,,DistanceFunction->CorrelationDistance]

ImageDistance[,]

The distance between two images is symmetric:

i1=RandomImage[1,{100,200}];
i2=RandomImage[1,{50,100}];
ImageDistance[i1,i2]==ImageDistance[i2,i1]

The distance may not be symmetric with masks:

ImageDistance[i1,i2,Masking->Graphics[Disk[]]]

ImageDistance[i2,i1,Masking->Graphics[Disk[]]]

When the images have no overlap, the distance is indeterminate:

img=;
ImageDistance[img,img,{Right,Bottom},{Left,Top}]

The distance between an image and its rotations:

i=;
t=Table[ImageDistance[i,ImageRotate[i,θ,Full]],{θ,0,2Pi,Pi/20}];
ListLinePlot[t,DataRange->{0,2Pi},Ticks->{{0,π/2,π,3π/2,2π},Automatic}]

Normalized mutual information variation of an MRI and a translated PET image:

ListLinePlot[
Table[
{x,ImageDistance[
,ImagePerspectiveTransformation[,TranslationTransform[{x,0}]],DistanceFunction->"NormalizedMutualInformationVariation"
]},
{x,-0.2,0.2,0.01}]]

Correcting for overall change in brightness is a typical preprocessing with some distances:

img1=;
img2=;ImageDistance[img1,ImageMultiply[img2,.8],DistanceFunction->#]&/@{"DifferenceNormalizedEntropy", "MeanPatternIntensity"}

Without correction, it is less apparent that the two images are aligned:

ImageDistance[img1,img2,DistanceFunction->#]&/@{"DifferenceNormalizedEntropy", "MeanPatternIntensity"}

Some distance measures are effectively invariant to specific transformations.

Spatial reflection:

ImageDistance[,,DistanceFunction->"EarthMoverDistance"]

Color negation:

img=;
ImageDistance[img,ColorNegate[img],DistanceFunction->#]&/@{"MutualInformationVariation","NormalizedMutualInformationVariation"}

Pointwise addition:

img=RandomImage[{.2,.7},{100,100}];
ImageDistance[img,ImageAdd[img,.25],DistanceFunction->#]&/@{NormalizedSquaredEuclideanDistance,CorrelationDistance,"MutualInformationVariation","NormalizedMutualInformationVariation","DifferenceNormalizedEntropy","MeanPatternIntensity","GradientCorrelation","MeanReciprocalGradientDistance"}

Pointwise multiplication by a positive number:

img=RandomImage[1,{100,100}];
ImageDistance[img,ImageMultiply[img,.4],DistanceFunction->#]&/@{CosineDistance,CorrelationDistance,"GradientCorrelation"}

Pointwise affine transformation with positive slope coefficient:

img=RandomImage[{.2,.7},{100,100}];
ImageDistance[img,ImageApply[.3*#-.2&,img],DistanceFunction->#]&/@{CorrelationDistance,"GradientCorrelation"}

For a rectangular mask, image distance is equal to the distance of the trimmed images:

i1=;i2=;mask=;

ImageDistance[i1,i2,Masking->mask]

ImageDistance[ImageTrim[i1,ImageValuePositions[mask,1]],ImageTrim[i2,ImageValuePositions[mask,1]]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ImageDifference, paclet:ref/ImageDifference])  ▪  (RefLink[ImageSubtract, paclet:ref/ImageSubtract])  ▪  (RefLink[ColorDistance, paclet:ref/ColorDistance])  ▪  (RefLink[ImageAlign, paclet:ref/ImageAlign])  ▪  (RefLink[FeatureDistance, paclet:ref/FeatureDistance])  ▪  (RefLink[FeatureNearest, paclet:ref/FeatureNearest])  ▪  (RefLink[Nearest, paclet:ref/Nearest])  ▪  (RefLink[CentralFeature, paclet:ref/CentralFeature])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Geometric Operations, paclet:guide/ImageGeometry])}, {▪(RefLinkPlain[Distance and Similarity Measures, paclet:guide/DistanceAndSimilarityMeasures])}})}})