Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alias sadn and ssdn, port ncc #35

Merged
merged 2 commits into from
Sep 16, 2019
Merged
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ version = "0.2.4"
ColorVectorSpace = "c3611d14-8923-5661-9e6a-0046d554d3a4"
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MappedArrays = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
Distances = "0.8.1"
Expand Down
6 changes: 4 additions & 2 deletions src/ImageDistances.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ImageDistances

using MappedArrays
using Distances
using Distances, LinearAlgebra, Statistics
using Distances:
get_common_ncols,
get_colwise_dims,
Expand Down Expand Up @@ -68,6 +68,7 @@ export
MeanAbsoluteError,
MeanSquaredError,
RootMeanSquaredError,
NCC,

# helper functions
hausdorff,
Expand All @@ -77,7 +78,8 @@ export
ssd,
mae,
mse,
rmse
rmse,
ncc

"""
`ImageDistances` is an image-related distance package built based on `Distances`.
Expand Down
51 changes: 50 additions & 1 deletion src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,37 @@ struct SumSquaredDifference <: Metric end
@doc raw"""
MeanAbsoluteError <: Metric
mae(x, y)
sadn(x, y)

mean absolute error(mae) is calculated by [`sad`](@ref)`(x, y)/length(x)`
"""
struct MeanAbsoluteError <: Metric end

@doc raw"""
MeanSquaredError <: Metric
mse(x, y) <: Metric
mse(x, y)
ssdn(x, y)

mean squared error(mse) is calculated by [`ssd`](@ref)`(x, y)/length(x)`
"""
struct MeanSquaredError <: Metric end

@doc raw"""
NCC <: Metric
ncc(x, y)

Normalized Cross-correlation(ncc) is calculated by `dot(mean(x),mean(y))/(norm(mean(x))*norm(mean(y)))`

```math
\frac{<\bar{x}, \bar{y}>}{||\bar{x}||*||\bar{y}||}
```

!!! info

NCC isn't a Metric because `ncc(x, x) == 1.0`
"""
struct NCC <: PreMetric end

@doc raw"""
RootMeanSquaredError <: Metric
rmse(x, y)
Expand Down Expand Up @@ -64,6 +82,8 @@ ssd(a::GenericImage, b::GenericImage) = SumSquaredDifference()(a, b)

@doc (@doc MeanAbsoluteError)
mae(a::GenericImage, b::GenericImage) = MeanAbsoluteError()(a, b)
@doc (@doc MeanAbsoluteError)
const sadn = mae


# MeanSquaredError
Expand All @@ -72,6 +92,8 @@ mae(a::GenericImage, b::GenericImage) = MeanAbsoluteError()(a, b)

@doc (@doc MeanSquaredError)
mse(a::GenericImage, b::GenericImage) = MeanSquaredError()(a, b)
@doc (@doc MeanSquaredError)
const ssdn = mse


# RootMeanSquaredError
Expand All @@ -80,3 +102,30 @@ mse(a::GenericImage, b::GenericImage) = MeanSquaredError()(a, b)

@doc (@doc RootMeanSquaredError)
rmse(a::GenericImage, b::GenericImage) = RootMeanSquaredError()(a, b)

# NCC
function (::NCC)(a::GenericGrayImage, b::GenericGrayImage)
A = channelview(of_eltype(floattype(eltype(a)), a))
B = channelview(of_eltype(floattype(eltype(b)), b))
return _ncc(A, B)
end

function (::NCC)(a::AbstractArray{<:Color3}, b::AbstractArray{<:Color3})
A = of_eltype(floattype(eltype(a)), a)
B = of_eltype(floattype(eltype(b)), b)
return _ncc(A, B)
end

function _ncc(A::GenericImage, B::GenericImage)
Am = (A.-mean(A))[:]
Bm = (B.-mean(B))[:]
return _dot(Am,Bm)/(norm(Am)*norm(Bm))
end

_dot(a::AbstractArray{<:Number}, b::AbstractArray{<:Number}) =
dot(a, b)
_dot(a::GenericImage, b::GenericImage) =
mapreduce(xy->dotc(xy...), +, zip(a, b))

@doc (@doc NCC)
ncc(a::GenericImage, b::GenericImage) = NCC()(a, b)
1 change: 1 addition & 0 deletions test/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dist_list = [SqEuclidean(),
Minkowski(2.5),
Hamming(),
TotalVariation(),
NCC(),
SumAbsoluteDifference(),
SumSquaredDifference(),
MeanAbsoluteError(),
Expand Down
1 change: 1 addition & 0 deletions test/references/NCC_2d_Gray.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-1.0
1 change: 1 addition & 0 deletions test/references/NCC_2d_RGB.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.06333332508802414