Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/metrics_distances.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# patches to metrics.jl of Distances.jl
# patches to Distances.jl

const metrics = (SqEuclidean, Euclidean, Cityblock, Minkowski, Hamming, TotalVariation)
const UnionMetrics = Distances.UnionMetrics

# Before evaluation, unwrap the AbstractGray colorant and promote storage type
#
Expand Down Expand Up @@ -39,15 +38,33 @@ for M in metrics
end


# ambiguities
### ambiguities

# These metrics in Distances.jl define their own result_type
const independentmetrics = (CorrDist, Mahalanobis, SqMahalanobis, SpanNormDist, Distances.UnionMetrics, Distances.UnionWeightedMetrics)

for (ATa, ATb) in ((AbstractGray, AbstractGray),
(AbstractGray, Number),
(Number, AbstractGray),
(PromoteType, PromoteType),
(Color3, Color3))
@eval function result_type(dist::UnionMetrics, ::Type{Ta}, ::Type{Tb}) where {Ta <: $ATa,Tb <: $ATb}
T1 = eltype(floattype(Ta))
T2 = eltype(floattype(Tb))
result_type(dist, T1, T2)
for M in independentmetrics
@eval function result_type(dist::$M, ::Type{Ta}, ::Type{Tb}) where {Ta <: $ATa,Tb <: $ATb}
T1 = eltype(floattype(Ta))
T2 = eltype(floattype(Tb))
result_type(dist, T1, T2)
end
end
end

# WeightedEuclidean defines its own method of colwise!
function colwise!(r::AbstractVector, dist::WeightedEuclidean,
a::AbstractMatrix{<:GenericImage},
b::AbstractMatrix{<:GenericImage})
(m, n) = get_colwise_dims(r, a, b)
m == 1 || throw(DimensionMismatch("The number of columns should be 1."))
@inbounds for j = 1:n
r[j] = dist(a[1,j], b[1,j]) # TODO: use view
end
r
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ using ImageCore
using IterTools
using ReferenceTests
using ImageDistances
using Distances

# there're still two ambiguities on colwise! for SqMahalanobis and Mahalanobisat
@test length(detect_ambiguities(Distances, ImageDistances)) == 2

# general distances should cover any combination of number_types and color_types unless it's special designed
include("testutils.jl")
Expand Down