text:
# Julia code for Chapter 9

# Chapter 9.1 Confidence Interval

####################################################################
## Histogram of the sample average
using Distributions, Plots, Printf
gmm = MixtureModel(Normal[Normal(), Normal(5., 1.),], [0.3, 0.7])
x = range(-5., 10., length=1000)
# the population distribution
plot(x, pdf(gmm, x), linewidth=3, label=false, title="Population distribution")
# histograms of 4 random samples of size 50
plot_array = []
for i=1:4
    bin = -5:10
    Y = rand(gmm, 50)
    tt = @sprintf("Mean = %3.2f", mean(Y))
    push!(plot_array, histogram(Y, bins=bin, label = tt, legend=:topleft))
end
plot(plot_array...)
# histogram of sample average
M = zeros(10000)
bin = range(2., 5., step=0.1)
for i = 1:10000
    M[i] = mean(rand(gmm, 50))
end
histogram(M, bins=bin, label=false, title="Histogram of the sample average")

####################################################################
## Compute confidence interval
# Julia code to compute the width of the confidence interval
using Distributions
α = 0.05
μ = 0.
σ = 1.
ϵ = quantile(Normal(μ, σ ), 1. - α/2.) # CI is estimator ± ϵ

####################################################################
## Visualize the t-distribution
# Julia code to plot the t-distribution
using Distributions, Plots
x = range(-5.,5.,length=100)
p1 = pdf(Normal(),x)
p2 = pdf(TDist(11-1),x)
p3 = pdf(TDist(3-1),x)
p4 = pdf(TDist(2-1),x)
plot([p1,p2,p3,p4], label = ["Gaussian(0,1)" "t-dist, N = 11" "t-dist, N = 3" "t-dist, N = 2"], legend=:topright)

####################################################################
## Construct a confidence interval from data
# Julia code to generate a confidence interval
using Statistics, Distributions, Printf
x = [72., 69., 75., 58., 67., 70., 60., 71., 59., 65.]
θhat = mean(x)  # Sample mean
σhat = std(x)   # Sample standard deviation
N = size(x,1)
ν = N-1 # degrees of freedom
α = 0.05        # confidence level
z = quantile(TDist(ν), 1. - α/2.)
CI_L = θhat - z*σhat/sqrt(N)
CI_U = θhat + z*σhat/sqrt(N)
@printf("Confidence interval: [%5.3f - %5.3f]", CI_L, CI_U)

####################################################################

# Chapter 9.2 Bootstrap

####################################################################
## Visualize bootstrap
# Julia code to illustrate bootstrap
using Plots, Distributions, Statistics
gmm = MixtureModel(Normal[Normal(), Normal(5., 1.),], [0.3, 0.7])
# visualize the population
x = range(-5., 10., length=1000);
f = pdf(gmm, x);
plot(x, pdf(gmm,x), linewidth=3, label="Population", legend=:topleft)
# Visualize bootstrap dataset, the real sample data
N = 100
X = rand(gmm, N) # the real sample data
p1 = histogram(X, bins = range(-5,10,length=30), label=false)
# visualize 3 bootstrap samples
plot_array = []
for i=1:3
    # Bootstrap dataset
    idx = rand(1:N, N)
    Xb1 = X[idx]
    push!(plot_array, histogram(Xb1, bins = range(-5.,10.,length=30)))
end
plot(plot_array..., label=false)

# Bootstrap the median
K = 1000 # number of bootstrap re-samples
θhat = zeros(K)
for i=1:K               # repeat K times
    idx = rand(1:N, N)  # sampling w/ replacement
    Y = X[idx]
    θhat[i] = median(Y) # estimator
end
M = mean(θhat)          # bootstrap mean
V = var(θhat)           # bootstrap variance
# histogram of bootstrap replications of estimator
histogram(θhat, bins=range(3.5,5.5,length=30), legend=false)

####################################################################
## Bootstrap median using real data
# Julia code to estimate a bootstrap variance
using Statistics
X = [72., 69., 75., 58., 67., 70., 60., 71., 59., 65.]
N = size(X,1)
K = 1000
θhat = zeros(K)
for i=1:K                       # repeat K times
    idx = rand(1:N, N)          # sampling w/ replacement
    Y = X[idx]
    θhat[i] = median(Y)         # estimator
end
M = mean(θhat)                  # bootstrap mean
V = var(θhat)                   # bootstrap variance

####################################################################

# Chapter 9.3 Hypothesis Testing

####################################################################
## Estimate Z-value
θhat = 0.29                 # Your estimate
θ = 0.25                    # Your hypothesis
σ = sqrt(θ*(1. - θ))        # Known standard deviation
N = 1000                    # Number of samples
Z_hat = (θhat - θ)/(σ/sqrt(N))

####################################################################
## Compute critical value
using Distributions
α = 0.05
z_α = quantile(Normal(), 1-α)

####################################################################
## Compute p-value
using Distributions
p = cdf(Normal(), -1.92)

####################################################################

# Chapter 9.5 ROC Curve

####################################################################
## Plot an ROC curve
# Julia code to plot ROC curve
using Distributions, Plots, Statistics
σ = 2.
μ = 3.
PF1 = zeros(1000); PD1 = zeros(1000)
PF2 = zeros(1000); PD2 = zeros(1000)
αset = range(0.,1.,length=1000)
d = Normal()
for i=1:1000
    α = αset[i]
    PF1[i] = α
    PD1[i] = α
    PF2[i] = α
    PD2[i] = 1. - cdf(d, quantile(d, 1-α)-μ/σ)
end
plot(PF1, PD1, linewidth=3, label = "Blind guess")
plot!(PF2, PD2, linewidth=3, label = "Neyman-Pearson", legend=:bottomright)
# Computer area under curve
auc1 = sum(PD1.*[0.; diff(PF1)])
auc2 = sum(PD2.*[0.; diff(PF2)])

####################################################################
## Another ROC curve
# Julia code to generate the ROC curve.
using Distributions, Plots
σ = 2.
μ = 3.
PF1 = zeros(1000); PD1 = zeros(1000)
PF2 = zeros(1000); PD2 = zeros(1000)
αset = range(0.,0.5,length=1000)
d = Normal()
for i=1:1000
    α = αset[i]
    PF1[i] = 2. *α
    PD1[i] = 1. - (cdf(d, quantile(d,1. - α) - μ/σ) -
        cdf(d, -quantile(d, 1. -α) - μ/σ))
end
αset = range(0.,1.,length=1000)
for i=1:1000
    α = αset[i]
    PF2[i] = α
    PD2[i] = 1. - cdf(d, quantile(d, 1-α)-μ/σ)
end
args = (linewidth=3, xlabel = "p_F", ylabel="p_D")
plot(PF1, PD1; args..., label = "Proposed decision")
plot!(PF2, PD2; args..., label = "Neyman-Pearson", legend=:bottomright)

####################################################################
## ROC on real data
using DelimitedFiles, Plots
scores = readdlm("ch9_ROC_example_data.txt")
scores = scores[:] # a vector
tau = range(0.,1.,length=1000)
labels = [ones(50); zeros(50)]
PF = zeros(1000)
PD = zeros(1000)
for i=1:1000
    idx   = (scores .<= tau[i])
    predict = zeros(100)
    predict[idx]   .= 1.
    true_positive  = 0; true_negative  = 0;
    false_positive = 0; false_negative = 0;
    for j=1:100
        if (predict[j]==1) && (labels[j]==1)
            true_positive += 1   end
        if (predict[j]==1) && (labels[j]==0)
            false_positive += 1 end
        if (predict[j]==0) && (labels[j]==1)
            false_negative += 1 end
        if (predict[j]==0) && (labels[j]==0)
            true_negative += 1   end
    end
    PF[i] = false_positive/50
    PD[i] = true_positive/50
end
plot(PF, PD, linewidth=3, legend=false)


text:
Using APES

## Li & Stoica fig1
spectral_lines_f = [0.04, 0.05, 0.08, 0.09,    0.16:0.005:0.35]
spectral_lines_p = vec(repmat([pi/4], length(spectral_lines_f)))
spectral_lines_a = [10, 10, 10, 2,   vec(repmat([0.5], length(spectral_lines_f)-4, 1))]

## Simulation properties
N = 128
noise_mean = 0
noise_vari = 1

## Create example signal
z = vec(zeros(N, 1))
n = 1.0:N
for i in 1:length(spectral_lines_f)
    z = z + spectral_lines_a[i] * sin(2 * pi * spectral_lines_f[i] * n + spectral_lines_p[i])
end
srand(1) # For reproducible results
z += sqrt(noise_vari)*randn(size(z))

println("Prime timer")
@time apes(z, spectral_lines_f);
println()
println()

println("Time for 10 runs")
@time for n=1:10; apes(z, 0.0:0.001:0.5); end

ext: jl


text:
using ConstrainedDynamics

# Parameters
ex = [1.;0.;0.]
ey = [0.;1.;0.]
ez = [0.;0.;1.]

l1 = 1.0
x, y = .1, .1
b1 = Box(x, y, l1, l1, color = RGBA(1., 1., 0.))

vert11 = [0.;0.;l1 / 2]
vert12 = -vert11

# Initial orientation
phi1, phi2, phi3 = pi / 4, -pi / 4, pi / 2
q1, q2, q3 = UnitQuaternion(RotX(phi1)), UnitQuaternion(RotX(phi2)), UnitQuaternion(RotX(phi3))

# Links
origin = Origin{Float64}()
link1 = Box(x, y, l1, l1, color = RGBA(1., 1., 0.))
link2 = deepcopy(link1)

# Constraints
joint0to12 = EqualityConstraint(CylindricalFree(origin, link1, ey; p2=vert11), Spherical(origin, link2; p2=vert11))
joint1to2 = EqualityConstraint(CylindricalFree(link1, link2, ex))

links = [link1; link2]
constraints = [joint0to12;joint1to2]

mech = Mechanism(origin, links, constraints)
setPosition!(origin,link1,p1 = [0;-0.5 * sqrt(2);0],p2 = vert11,Δq = q1)
setPosition!(origin,link2,p2 = vert11,Δq = q2)


text:
using Pkg
pkg"activate ."

@info "Installed packages:"
show(stdout, "text/plain", sort(collect(Pkg.installed())))
println("\n---------------------------------------------------")

max_stars_repo_name: Gilga/AI-Cywwtaip

max_issues_repo_name: Gilga/AI-Cywwtaip

max_forks_repo_name: Gilga/AI-Cywwtaip


text:
function RBMs.initialize!(rbm::ConvRBM, data::AbstractArray; ϵ::Real = 1e-6, λ::Real = 0.1)
    RBMs.initialize!(visible(rbm), data; ϵ)
    RBMs.initialize!(hidden(rbm))
    RBMs.initialize_w!(rbm, data; ϵ, λ)
    RBMs.zerosum!(rbm)
    return rbm
end

function RBMs.initialize!(rbm::ConvRBM; ϵ::Real = 1e-6, λ::Real = 0.1)
    RBMs.initialize!(visible(rbm))
    RBMs.initialize!(hidden(rbm))
    RBMs.initialize_w!(rbm; ϵ, λ)
    RBMs.zerosum!(rbm)
    return rbm
end

function RBMs.initialize_w!(rbm::ConvRBM, data::AbstractArray; ϵ::Real = 1e-6, λ::Real = 0.1)
    d = dot(data, data) / prod(vsizes(rbm, data).batch_size)
    randn!(weights(rbm))
    J = prod(kernel_size(rbm))
    K = prod(output_size(rbm, data))
    weights(rbm) .*= sqrt(K/J) * λ / √(d + ϵ)
    return rbm # does not impose zerosum
end

function RBMs.initialize_w!(rbm::ConvRBM; ϵ::Real = 1e-6, λ::Real = 0.1)
    @assert 0 < ϵ < 1/2
    μ = RBMs.transfer_mean(visible(rbm))
    ν = RBMs.transfer_var(visible(rbm))
    d = sum(ν + μ.^2)
    randn!(weights(rbm))
    J = prod(kernel_size(rbm))
    K = prod(output_size(rbm, data))
    weights(rbm) .*= sqrt(K/J) * λ / √(d + ϵ)
    return rbm
end


text:
using TFRecord
using Test

@testset "TFRecord.jl" begin
    # Write your tests here.
end


text:
#!/bin/bash
# -*- mode: julia -*-
#=
exec julia --project=@. --color=yes --startup-file=no -e "include(popfirst!(ARGS))" "${BASH_SOURCE[0]}" "$@"
=#

using JuMP, Cbc, GLPK

function run(optimizer)
	m = Model(optimizer)

	empty_terms = AffExpr()
	@constraint(m, empty_terms <= 2)
	optimize!(m)
	@show JuMP.primal_status(m)
	if JuMP.primal_status(m) == MOI.FEASIBLE_POINT
		@show JuMP.objective_value(m)
	end
end

println("\n===================== GLPK First Time ====================\n")
@time run(GLPK.Optimizer)
println("\n==================== GLPK Second Time ====================\n")
@time run(GLPK.Optimizer)
println("\n====================== Cbc First Time ====================\n")
@time run(Cbc.Optimizer)
println("\n===================== Cbc Second Time ====================\n")
@time run(Cbc.Optimizer)


text:
# Autogenerated wrapper script for SFML_jll for x86_64-w64-mingw32-cxx11
export libsfml_audio, libsfml_graphics, libsfml_network, libsfml_system, libsfml_window

using Libglvnd_jll
using Ogg_jll
using FLAC_jll
using FreeType2_jll
using libvorbis_jll
using Xorg_libXrandr_jll
using Xorg_libX11_jll
using OpenAL_jll
using eudev_jll
JLLWrappers.@generate_wrapper_header("SFML")
JLLWrappers.@declare_library_product(libsfml_audio, "sfml-audio-2.dll")
JLLWrappers.@declare_library_product(libsfml_graphics, "sfml-graphics-2.dll")
JLLWrappers.@declare_library_product(libsfml_network, "sfml-network-2.dll")
JLLWrappers.@declare_library_product(libsfml_system, "sfml-system-2.dll")
JLLWrappers.@declare_library_product(libsfml_window, "sfml-window-2.dll")
function __init__()
    JLLWrappers.@generate_init_header(Libglvnd_jll, Ogg_jll, FLAC_jll, FreeType2_jll, libvorbis_jll, Xorg_libXrandr_jll, Xorg_libX11_jll, OpenAL_jll, eudev_jll)
    JLLWrappers.@init_library_product(
        libsfml_audio,
        "bin\\sfml-audio-2.dll",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        libsfml_graphics,
        "bin\\sfml-graphics-2.dll",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        libsfml_network,
        "bin\\sfml-network-2.dll",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        libsfml_system,
        "bin\\sfml-system-2.dll",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        libsfml_window,
        "bin\\sfml-window-2.dll",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()


text:
# Copyright (c) 2020-2021, Bank of Canada
# All rights reserved.

# augment arraymath.jl for TSeries

# These are operations that treat a TSeries as a single object (rather than a collection of values indexed by MIT)
# Conceptually we must distinguish these operations from broadcasting (element-wise) operations.

# For example, t + s is valid if t and s are of the same TSeries type (same frequency).
# The result of t + s is a new TSeries of the same frequency over the common range.
# In practice t + s and t .+ s produce identical results, although conceptually they're different operations.
#
# t + n is not valid because TSeries + Number doesn't work, but we can do t .+ n.
# t + v is not valid because TSeries + Vector doesn't work, but we can do t .+ v so long as length(t) == length(v).
# n*t is valid because Number * TSeries is a valid operation.
# t/n is valid (same as (1/n)*t), but n/t is not because we can't divide a Number by TSeries. We can still do n ./ t.

# function applications are not valid, so we must use dot to broadcast, e.g. log(t) throws an error, we must do log.(t)

@inline Base.promote_shape(a::TSeries, b::TSeries) = mixed_freq_error(a, b)
@inline Base.promote_shape(a::TSeries{F}, b::TSeries{F}) where F <: Frequency = intersect(eachindex(a), eachindex(b))

@inline shape_error(A::Type,B::Type) = throw(ArgumentError("This operation is not valid for $(A) and $(B). Try using . to do it element-wise."))
@inline shape_error(a,b) = shape_error(typeof(a), typeof(b))

@inline Base.promote_shape(a::TSeries, b::AbstractVector) = shape_error(a, b)
@inline Base.promote_shape(a::AbstractVector, b::TSeries) = shape_error(a, b)

# +, -, *, / work out of the box with the above methods for promote_shape.

@inline function Base.isapprox(x::TSeries, y::TSeries; kwargs...)
    shape = promote_shape(x, y)
    isapprox(x[shape].values, y[shape].values; kwargs...)
end

####################################################################
# Now we implement some time-series operations that do not really apply to vectors.

@inline shift(ts::TSeries, k::Int) = TSeries(ts.firstdate - k, copy(ts.values))
@inline shift!(ts::TSeries, k::Int) = (ts.firstdate -= k; ts)
@inline lag(t::TSeries, k::Int=1) = shift(t, -k)
@inline lag!(t::TSeries, k::Int=1) = shift!(t, -k)
@inline lead(t::TSeries, k::Int=1) = shift(t, k)
@inline lead!(t::TSeries, k::Int=1) = shift!(t, k)

"""
    shift(x, n)
    shift!(x, n)
    lag(x, n=1)
    lag!(x, n=1)
    lead(x, n=1)
    lead!(x, n=1)

Shift, lag or lead the TSeries `x` by `n` periods.
By convention `shift` is the same as `lead` while `lag(x,n)` is the same as `shift(x, -n)`.
The versions ending in ! do it in place, while the others create a new TSeries instance.

Examples
```julia-repl
julia> shift(TSeries(2020Q1, 1:4), 1)
TSeries{Quarterly} of length 4
2019Q4: 1.0
2020Q1: 2.0
2020Q2: 3.0
2020Q3: 4.0

julia> shift(TSeries(2020Q1, 1:4), -1)
TSeries{Quarterly} of length 4
2020Q2: 1.0
2020Q3: 2.0
2020Q4: 3.0
2021Q1: 4.0

julia> x = TSeries(2020Q1, 1:4);

julia> shift!(x, 1);

julia> x
TSeries{Quarterly} of length 4
2019Q4: 1.0
2020Q1: 2.0
2020Q2: 3.0
2020Q3: 4.0
```
"""
shift, shift!, lead, lead!, lag, lag!


text:
# MSF_ploty_v3.jl
# ==================

# master stability functions for the Roessler system with different coupling terms

using DrWatson
@quickactivate "NonlinearDynamicsTextbook"
include(srcdir("style.jl"))
#using DynamicalSystems
using PyPlot, OrdinaryDiffEq
using StaticArrays
using LinearAlgebra

# coupling matrix
# ---------------

wmat =  [0   7    0    5    0  ;
         7   0    3    0    4  ;
         0   3    0    0    6  ;
         5   0    0    0    0  ;
         0   4    6    0    0 ]

gmat = - wmat

dg = sum(wmat,dims=2)

for i = 1:5
    gmat[i,i] = dg[i]
end

#check row sum
# println( sum(gmat, dims=2) )

# eigenvalues
evvec = eigvals(gmat)
# println(evvec)

# load results
# -------------
filepath = datadir("master_stability_function.jld")

alpha_vec = load(filepath,"alpha_vec")

LE_vec_xx = load(filepath,"LE_vec_xx")
LE_vec_xy = load(filepath,"LE_vec_xy")
LE_vec_xz = load(filepath,"LE_vec_xz")

LE_vec_yx = load(filepath,"LE_vec_yx")
LE_vec_yy = load(filepath,"LE_vec_yy")
LE_vec_yz = load(filepath,"LE_vec_yz")

LE_vec_zx = load(filepath,"LE_vec_zx")
LE_vec_zy = load(filepath,"LE_vec_zy")
LE_vec_zz = load(filepath,"LE_vec_zz")

# plot version selection
# ======================
fig = figure("MSF selection 1",figsize=(0.5*figx,1.1*figy))

# coupling signal x

# Jh = [1 0 0 ; 0 0 0 ; 0 0 0 ]   # x drives x-ODE
plot(alpha_vec,LE_vec_xx, color = COLORS[1], linestyle="-")

# Jh = [0 0 0 ; 1 0 0 ; 0 0 0 ]  # x drives y-ODE
# plot(alpha_vec,LE_vec_xy, color = COLORS[1], linestyle=":")

# Jh = [0 0 0 ; 0 0 0 ; 1 0 0 ]  # x drives z-ODE
plot(alpha_vec,LE_vec_xz, color = COLORS[2], linestyle="--")

# coupling signal y

# Jh = [0 1 0 ; 0 0 0 ; 0 0 0 ]   # y drives x-ODE
# plot(alpha_vec,LE_vec_yx, color = COLORS[4])

# Jh = [0 0 0 ; 0 1 0 ; 0 0 0 ]  # y drives y-ODE
plot(alpha_vec,LE_vec_yy, color = COLORS[4], linestyle=":")

# Jh = [0 0 0 ; 0 0 0 ; 0 1 0 ]  # y drives z-ODE
# plot(alpha_vec,LE_vec_yz, color = COLORS[4], linestyle="--")

# coupling signal z

# Jh = [0 0 1 ; 0 0 0 ; 0 0 0 ] # z drives x-ODE
# plot(alpha_vec,LE_vec_zx, color = COLORS[2])

# Jh = [0 0 0 ; 0 0 1 ; 0 0 0 ]  # z drives y-ODE
# plot(alpha_vec,LE_vec_zy, color = COLORS[2], linestyle=":")

# Jh = [0 0 0 ; 0 0 0 ; 0 0 1 ]  # z drives z-ODE
plot(alpha_vec,LE_vec_zz, color = COLORS[3], linestyle="--")

xlabel(L"$\alpha$";labelpad = -20)
ylabel(L"$\lambda_1$")

LE_min = -0.3
LE_max = 0.3

ax = gca()
ax.set_xlim([alpha_vec[1], alpha_vec[end]])
ax.set_ylim([LE_min,LE_max])
ax.legend([L"x \to \dot x"; L"x \to \dot z"; L"y \to \dot y"; L"z \to \dot z" ], fontsize = 28,
           loc = "upper right", ncol = 2, handlelength = 1. , handletextpad = 0.5)

# zero line
plot(alpha_vec, 0 .* LE_vec_xx, linewidth=1.0,linestyle="-",color="k" )
ylabel(L"$\lambda_{1}$") #

# plot eigenvalues
sigma = 0.15
scatter(sigma*evvec, 0 .* evvec,color="k",s = 40. )

#add_identifiers!(fig)
fig.tight_layout(pad=0.3)

wsave(plotsdir("10", "MSF_Roessler_selection"), fig)

max_stars_repo_name: JuliaDynamics/NonlinearDynamicsTextbook

max_issues_repo_name: JuliaDynamics/NonlinearDynamicsTextbook

max_forks_repo_name: JuliaDynamics/NonlinearDynamicsTextbook


text:
abstract type AbstractDualNumber
end

struct DualNumber{T} <: AbstractDualNumber
    x::T
    v::T
    function DualNumber{T}(x::T, v::T) where {T}
        return new(x, v)
    end
end
function DualNumber(x::T, v::T) where {T}
    return DualNumber{T}(x, v)
end
function DualNumber(x, v)
    return DualNumber( promote(x, v)... )
end
function DualNumber(x::T) where {T}
    return DualNumber{T}(x, zero(T))
end

function Base.eltype(d::DualNumber)
    return typeof(d.x)
end

function Base.zero(x::DualNumber{T}) where {T}
    return DualNumber(zero(T))
end

function Base.one(x::DualNumber{T}) where {T}
    return DualNumber(one(T))
end

function Base.zero(::Type{DualNumber{T}}) where {T}
    return DualNumber(zero(T))
end

function Base.one(::Type{DualNumber{T}}) where {T}
    return DualNumber(one(T))
end

function value(vec::Vector{D}) where {D <: AbstractDualNumber}
    Dtype = eltype(vec)
    Xtype = Dtype.parameters[1]
    L = length(vec)
    v = Vector{Xtype}(undef, L)
    for kk = 1:L
        v[kk] = vec[kk].x
    end
    return v
end
function derivative(vec::Vector{D}) where {D <: AbstractDualNumber}
    T = eltype(vec[1])
    L = length(vec)
    v = Vector{T}(undef, L)
    for kk = 1:L
        v[kk] = vec[kk].v
    end
    return v
end

# function derivative(fcn, x0::T) where {T <: AbstractDualNumber}
# end

# function gradient(fcn, x0::Vector{T}) where {T <: Number}
# end

# function gradient(fcn, d0::Vector{T}) where {T <: AbstractDualNumber}
# end

# function jacobian(fcn, x0::Vector{T}) where {T <: Number}
# end

# function directional_derivative(fcn, x, s)
# end

function derivative(::ForwardMode, fcn, x0::T) where {T <: Number}
    d = DualNumber(x0, one(T))
    y = fcn(d)
    return y.v
end

# ambigous in presence of reverse mode, forward mode is chosen
function derivative(fcn, x0::T) where {T <: Number}
    return derivative(ForwardMode(), fcn, x0)
end

function derivative(::ForwardMode, fcn, x0::T) where {T <: AbstractDualNumber}
    y = fcn(x0)
    return y.v
end

function derivative(fcn, x0::T) where {T <: AbstractDualNumber}
    return derivative(ForwardMode(), fcn, x0)
end

function gradient(::ForwardMode, fcn, x0::Vector{T}) where {T <: Number}

    L      = length(x0)
    e_i    = zeros(T, L)
    ds     = map(DualNumber, x0, e_i)
    g      = zeros(T, L)

    for ii = 1:L
        ds[ii] = DualNumber(x0[ii], one(T))
        y      = fcn(ds)
        g[ii]  = y.v
        ds[ii] = DualNumber(x0[ii], zero(T))
    end

    return g
end

# ambigous in presence of reverse mode, reverse mode is chosen
# function gradient(fcn, x0::Vector{T}) where {T <: Number}
#     return gradient(::ForwardMode, fcn, x0)
# end

function gradient(::ForwardMode, fcn, d0::Vector{T}) where {T <: AbstractDualNumber}
    x0 = value(d0)
    return gradient(fcn, x0)
end

function gradient(fcn, d0::Vector{T}) where {T <: AbstractDualNumber}
    return gradient(ForwardMode(), fcn, d0)
end

function jacobian(::ForwardMode, fcn, x0::Vector{T}) where {T <: Number}
    # f: R^n -> R^m
    # g_ij = df_i / dx_j

    L      = length(x0)
    e_i    = zeros(T, L)
    e_i[1] = one(T)
    ds     = map(DualNumber, x0, e_i)
    f1     = fcn(ds)
    jac    = zeros(T, length(f1), length(x0)) # m-by-n

    jac[:, 1] = derivative(f1)
    for ii = 2:L
        ds[ii-1]   = DualNumber(x0[ii-1], zero(T))
        ds[ii]     = DualNumber(x0[ii], one(T))
        f1         = fcn(ds)
        jac[:, ii] = derivative(f1)
    end

    return jac
end

# ambigous in presence of reverse mode, reverse mode is chosen
# function jacobian(fcn, x0::Vector{T}) where {T <: Number}
# end

function directional_derivative(::ForwardMode, fcn, x, s)
    # naive implementation
    # g  = gradient(fcn, x)
    # dg = dot(g, s)

    # a bit better version, take the gradient of
    sn = normalize(s)
    df(t) = begin
        z = map(a -> t * a, sn)
        y = x + z
        return fcn(y)
    end # fcn(x + t .* s)
    dg = derivative(df, 0.0)
    return dg
end

function directional_derivative(fcn, x, s)
    return directional_derivative(ForwardMode(), fcn, x, s)
end

function Base.:(==)(d1::DualNumber, d2::DualNumber)
    return d1.x == d2.x && d1.v == d1.v
end

function Base.:(!=)(d1::DualNumber, d2::DualNumber)
    return !(d1 == d2)
end

function Base.:+(d1::D, d2::D) where {D <: AbstractDualNumber}
    x_ = d1.x + d2.x
    v_ = d1.v + d2.v
    return DualNumber(x_, v_)
end
function Base.:+(d1::D, d2::T) where {D <: AbstractDualNumber, T <: Number}
    x_ = d1.x + d2
    v_ = d1.v
    return DualNumber(x_, v_)
end
function Base.:+(d1::T, d2::D) where {D <: AbstractDualNumber, T <: Number}
    return d2 + d1
end

function Base.:-(d1::DualNumber)
    return DualNumber(-d1.x, -d1.v)
end
function Base.:-(d1::D, d2::D) where {D <: AbstractDualNumber}
    x_ = d1.x - d2.x
    v_ = d1.v - d2.v
    return DualNumber(x_, v_)
end
function Base.:-(d1::D, d2::T) where {D <: AbstractDualNumber, T <: Number}
    x_ = d1.x - d2
    v_ = d1.v
    return DualNumber(x_, v_)
end
function Base.:-(d2::T, d1::D) where {D <: AbstractDualNumber, T <: Number}
    x_ = d2 - d1.x
    v_ = -d1.v
    return DualNumber(x_, v_)
end

function Base.:*(d1::D, d2::D) where {D <: AbstractDualNumber}
    x_ = d1.x * d2.x
    v_ = d1.v * d2.x + d1.x * d2.v
    return DualNumber(x_, v_)
end
function Base.:*(d1::D, d2::T) where {D <: AbstractDualNumber, T <: Number}
    x_ = d1.x * d2
    v_ = d1.v * d2
    return DualNumber(x_, v_)
end
function Base.:*(d1::T, d2::D) where {D <: AbstractDualNumber, T <: Number}
    return d2 * d1
end

function Base.:/(d1::D, d2::D) where {D <: AbstractDualNumber}
    x_ = d1.x / d2.x
    v_ = (d1.v * d2.x - d1.x * d2.v) / d2.x^2
    return DualNumber(x_, v_)
end
function Base.:/(d1::D, d2::T) where {D <: AbstractDualNumber, T <: Number}
    x_ = d1.x / d2
    v_ = d1.v / d2
    return DualNumber(x_, v_)
end
function Base.:/(d1::T, d2::D) where {D <: AbstractDualNumber, T <: Number}
    x_ = d1 / d2.x
    v_ = (- d1 * d2.v) / d2.x^2
    return DualNumber(x_, v_)
end

function Base.:^(d1::D, d2::D) where {D <: AbstractDualNumber}
    x_ = d1.x ^ d2.x
    v_ = x_ * (d2.v * log(d2.x) + d2.x * d1.v / d1.x)
    return DualNumber(x_, v_)
end
function Base.:^(d1::T, d2::D) where {D <: AbstractDualNumber, T <: Number}
    x_ = d1 ^ d2.x
    v_ = x_ * log(d1)
    return DualNumber(x_, v_)
end
function Base.:^(d1::D, d2::T) where {D <: AbstractDualNumber, T <: Number}
    x_ = d1.x ^ d2
    v_ = d1.v * d2 * d1.x ^ (d2 - 1)
    return DualNumber(x_, v_)
end

function Base.abs(d::D) where {D <: AbstractDualNumber}
    x_ = abs(d.x)
    v_ = sign(d.v)
    return DualNumber(x_, v_)
end

function Base.max(d1::D, d2::D) where {D <: AbstractDualNumber}
    x_ = max(d1.x, d2.x)
    v_ = (d1.x < d2.x) * d2.v + !(d1.x < d2.x) * d1.v
    return DualNumber(x_, v_)
end
function Base.max(d1::D, d2::T) where {D <: AbstractDualNumber, T <: Number}
    x_ = max(d1.x, d2)
    v_ = (d1.x < d2) * 0.0 + !(d1.x < d2) * d1.v
    return DualNumber(x_, v_)
end
function Base.max(d1::T, d2::D) where {D <: AbstractDualNumber, T <: Number}
    return max(d2, d1)
end

function Base.min(d1::D, d2::D) where {D <: AbstractDualNumber}
    x_ = min(d1.x, d2.x)
    v_ = (d1.x > d2.x) * d2.v + !(d1.x > d2.x) * d1.v
    return DualNumber(x_, v_)
end
function Base.min(d1::D, d2::T) where {D <: AbstractDualNumber, T <: Number}
    x_ = min(d1.x, d2)
    v_ = (d1.x > d2) * 0.0 + !(d1.x > d2) * d1.v
    return DualNumber(x_, v_)
end
function Base.min(d1::T, d2::D) where {D <: AbstractDualNumber, T <: Number}
    return min(d2, d1)
end

function Base.sin(d::D) where {D <: AbstractDualNumber}
    x_ = sin(d.x)
    v_ = d.v * cos(d.x)
    return DualNumber(x_, v_)
end
function Base.cos(d::D) where {D <: AbstractDualNumber}
    x_ = cos(d.x)
    v_ = - d.v * sin(d.x)
    return DualNumber(x_, v_)
end
function Base.tan(d::D) where {D <: AbstractDualNumber}
    x_ = tan(d.x)
    v_ = d.v * (1/cos(d.x))^2
    return DualNumber(x_, v_)
end

function Base.sinh(d::D) where {D <: AbstractDualNumber}
    x_ = sinh(d.x)
    v_ = d.v * cosh(d.x)
    return DualNumber(x_, v_)
end
function Base.cosh(d::D) where {D <: AbstractDualNumber}
    x_ = cosh(d.x)
    v_ = d.v * sinh(d.x)
    return DualNumber(x_, v_)
end
function Base.tanh(d::D) where {D <: AbstractDualNumber}
    x_ = tanh(d.x)
    v_ = d.v * (1/cosh(d.x))^2
    return DualNumber(x_, v_)
end

function Base.exp(d::D) where {D <: AbstractDualNumber}
    x_ = exp(d.x)
    v_ = d.v * exp(d.x)
    return DualNumber(x_, v_)
end
function Base.log(d::D) where {D <: AbstractDualNumber}
    x_ = log(d.x)
    v_ = d.v / d.x
    return DualNumber(x_, v_)
end
function Base.sqrt(d::D) where {D <: AbstractDualNumber}
    x_ = sqrt(d.x)
    v_ = d.v / sqrt(d.x)
    return DualNumber(x_, v_)
end

# I think they are implemented by map -> no extra implementation is needed
# sum
# prod

max_stars_repo_name: HomoModelicus/julia

max_issues_repo_name: HomoModelicus/julia

max_forks_repo_name: HomoModelicus/julia


text:
@testset "$(rpad("Symmetry",80))" begin

    for s in 1:10
        @test check_symmetry(TableauGauss(s)) == Array{Bool}(ones(s,s))
        @test issymmetric(TableauGauss(s))
    end

    for s in 2:10
        @test check_symmetry(TableauLobattoIIIA(s)) == Array{Bool}(ones(s,s))
        @test check_symmetry(TableauLobattoIIIB(s)) == Array{Bool}(ones(s,s))
        @test check_symmetry(TableauLobattoIIIC(s)) != Array{Bool}(ones(s,s))
        @test check_symmetry(TableauLobattoIIIC̄(s)) != Array{Bool}(ones(s,s))
        @test check_symmetry(TableauLobattoIIID(s)) == Array{Bool}(ones(s,s))
        @test check_symmetry(TableauLobattoIIIE(s)) == Array{Bool}(ones(s,s))
        @test check_symmetry(TableauRadauIA(s))     != Array{Bool}(ones(s,s))
        @test check_symmetry(TableauRadauIIA(s))    != Array{Bool}(ones(s,s))

        @test  issymmetric(TableauLobattoIIIA(s))
        @test  issymmetric(TableauLobattoIIIB(s))
        @test !issymmetric(TableauLobattoIIIC(s))
        @test !issymmetric(TableauLobattoIIIC̄(s))
        @test  issymmetric(TableauLobattoIIID(s))
        @test  issymmetric(TableauLobattoIIIE(s))
        @test !issymmetric(TableauRadauIA(s))
        @test !issymmetric(TableauRadauIIA(s))
    end

end


text:
# This file was generated by the Julia Swagger Code Generator
# Do not modify this file directly. Modify the swagger specification instead.

struct FlowLogsApi <: SwaggerApi
    client::Swagger.Client
end

"""

Create or update a flow log for the specified network security group.
Param: resourceGroupName::String (required)
Param: networkWatcherName::String (required)
Param: flowLogName::String (required)
Param: parameters::FlowLog (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: FlowLog
"""
function _swaggerinternal_flowLogsCreateOrUpdate(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, flowLogName::String, parameters, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "PUT", FlowLog, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs/{flowLogName}", ["azure_auth"], parameters)
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "networkWatcherName", networkWatcherName)  # type String
    Swagger.set_param(_ctx.path, "flowLogName", flowLogName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function flowLogsCreateOrUpdate(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, flowLogName::String, parameters, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsCreateOrUpdate(_api, resourceGroupName, networkWatcherName, flowLogName, parameters, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function flowLogsCreateOrUpdate(_api::FlowLogsApi, response_stream::Channel, resourceGroupName::String, networkWatcherName::String, flowLogName::String, parameters, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsCreateOrUpdate(_api, resourceGroupName, networkWatcherName, flowLogName, parameters, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

"""

Deletes the specified flow log resource.
Param: resourceGroupName::String (required)
Param: networkWatcherName::String (required)
Param: flowLogName::String (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: Nothing
"""
function _swaggerinternal_flowLogsDelete(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, flowLogName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "DELETE", Nothing, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs/{flowLogName}", ["azure_auth"])
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "networkWatcherName", networkWatcherName)  # type String
    Swagger.set_param(_ctx.path, "flowLogName", flowLogName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function flowLogsDelete(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, flowLogName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsDelete(_api, resourceGroupName, networkWatcherName, flowLogName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function flowLogsDelete(_api::FlowLogsApi, response_stream::Channel, resourceGroupName::String, networkWatcherName::String, flowLogName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsDelete(_api, resourceGroupName, networkWatcherName, flowLogName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

"""

Gets a flow log resource by name.
Param: resourceGroupName::String (required)
Param: networkWatcherName::String (required)
Param: flowLogName::String (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: FlowLog
"""
function _swaggerinternal_flowLogsGet(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, flowLogName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", FlowLog, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs/{flowLogName}", ["azure_auth"])
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "networkWatcherName", networkWatcherName)  # type String
    Swagger.set_param(_ctx.path, "flowLogName", flowLogName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function flowLogsGet(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, flowLogName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsGet(_api, resourceGroupName, networkWatcherName, flowLogName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function flowLogsGet(_api::FlowLogsApi, response_stream::Channel, resourceGroupName::String, networkWatcherName::String, flowLogName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsGet(_api, resourceGroupName, networkWatcherName, flowLogName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

"""

Lists all flow log resources for the specified Network Watcher.
Param: resourceGroupName::String (required)
Param: networkWatcherName::String (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: FlowLogListResult
"""
function _swaggerinternal_flowLogsList(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", FlowLogListResult, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs", ["azure_auth"])
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "networkWatcherName", networkWatcherName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function flowLogsList(_api::FlowLogsApi, resourceGroupName::String, networkWatcherName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsList(_api, resourceGroupName, networkWatcherName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function flowLogsList(_api::FlowLogsApi, response_stream::Channel, resourceGroupName::String, networkWatcherName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_flowLogsList(_api, resourceGroupName, networkWatcherName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

export flowLogsCreateOrUpdate, flowLogsDelete, flowLogsGet, flowLogsList


text:
using EngEconomics

# Given
aInit = 680
bInit = 1100
aAnnualSavings = 245
bAnnualSavings = 440
aAnnualMaintenance = 35
aAnnualMaintenanceg = 10
bAnnualMaintenance = 60
aServiceLife = 4
bServiceLife = 6
aScrapValue = 100
bScrapValue = 250

# Find
# a.) Use PW Method to determine which is better
MARR = 0.1
years = 12 # Just knew b/c it's the least common multiple of 4 and 6s
aPeriods = years / aServiceLife
bPeriods = years / bServiceLife

# Product A
aMultiplicand = 1 + presentWorthFactor(MARR, aServiceLife) + presentWorthFactor(MARR, 2 * aServiceLife)
aInitPW = -aInit * aMultiplicand
aAnnualSavingsPW = aAnnualSavings *
		seriesPresentAmountFactor(MARR, years)
aAnnualMaintenancePW = -aAnnualMaintenance *
		aAnnualMaintenanceg * sinkingFundPaymentArithmetic(MARR, aServiceLife) *
		aMultiplicand
aScrapValuePW = aScrapValue *
		(presentWorthFactor(MARR, aServiceLife) +
		presentWorthFactor(MARR, 2 * aServiceLife) +
		presentWorthFactor(MARR, 3 * aServiceLife))
aPW = aInitPW + aAnnualSavingsPW + aAnnualMaintenancePW + aScrapValuePW

# Product B

if aPW > bPW
	println("CR1000 is better")
else
	println("CRX is better")
end


text:
ops = Go.build_ops(100, 1)
for i in 1:10
    a, b = random_expression(Go.lheader+Go.chunk*12), random_expression(Go.lheader+Go.chunk*12)
    c = Go.weighted_rand(ops)(Vector[a, b])
    Go.unpack_expression(c)
end

#d = random_population(Go.lheader+Go.chunk*50, 100)
#evolve2(d, 19, 1000, 50, 10000, "../evol-1.dump")

d = final_gen("../evol-1.log", "../evol-1.dump", 100, 0.9)
evolve2(d, 19, 1000, 50, 10000, "../evol-2.dump")


text:
fname = :TRLDLt_abs
c = Combi(hessian_dense,PDataLDLt,solve_modelTRDiagAbs,preprocessLDLt,decreaseFact,Tparam())
include("Template.jl")


text:
#####################################################################################################
# imgproc. Image Processing
#####################################################################################################

# 1. Image Filtering
#     bilateralFilter
#     blur
#     boxFilter
#     buildPyramid
#     dilate
#     erode
#     filter2D
#     GaussianBlur
#     getDerivKernels
#     getGaussianKernel
#     getGaborKernel
#     getStructuringElement
#     medianBlur
#     morphologyEx
#     Laplacian
#     pyrDown
#     pyrUp
#     pyrMeanShiftFiltering
#     sepFilter2D
#     Smooth
#     Sobel
#     Scharr

# 2. Geometric Image Transformations
#     convertMaps
#     getAffineTransform
#     getPerspectiveTransform
#     getRectSubPix
#     getRotationMatrix2D
#     invertAffineTransform
#     LinearPolar
#     LogPolar
#     remap
#     resize
#     warpAffine
#     warpPerspective
#     initUndistortRectifyMap
#     getDefaultNewCameraMatrix
#     undistort
#     undistortPoints

# 3. Miscellaneous Image Transformations
#     adaptiveThreshold
#     cvtColor
#     distanceTransform
#     floodFill
#     integral
#     threshold
#     watershed
#     grabCut

# 4. Drawing Functions
#     circle
#     clipLine
#     ellipse
#     ellipse2Poly
#     fillConvexPoly
#     fillPoly
#     getTextSize
#     InitFont
#     line
#     arrowedLine
#     LineIterator
#     rectangle
#     polylines
#     drawContours
#     putText

# 5. ColorMaps in OpenCV
#     applyColorMap
#
# 6. Histograms
#     calcHist
#     calcBackProject
#     compareHist
#     EMD
#     equalizeHist
#     CalcBackProjectPatch
#     CalcProbDensity
#     ClearHist
#     CopyHist
#     CreateHist
#     GetMinMaxHistValue
#     MakeHistHeaderForArray
#     NormalizeHist
#     ReleaseHist
#     SetHistBinRanges
#     ThreshHist

# 7. Structural Analysis and Shape Descriptors
#     moments
#     HuMoments
#     connectedComponents
#     findContours
#     approxPolyDP
#     ApproxChains
#     arcLength
#     boundingRect
#     contourArea
#     convexHull
#     convexityDefects
#     fitEllipse
#     fitLine
#     isContourConvex
#     minAreaRect
#     boxPoints
#     minEnclosingTriangle
#     minEnclosingCircle
#     matchShapes
#     pointPolygonTest
#     rotatedRectangleIntersection

# 8. Motion Analysis and Object Tracking
#     accumulate
#     accumulateSquare
#     accumulateProduct
#     accumulateWeighted
#     phaseCorrelate
#     createHanningWindow

# 9. Feature Detection
#     Canny
#     cornerEigenValsAndVecs
#     cornerHarris
#     cornerMinEigenVal
#     cornerSubPix
#     goodFeaturesToTrack
#     HoughCircles
#     HoughLines
#     HoughLinesP
#     LineSegmentDetector
#     createLineSegmentDetector
#     LineSegmentDetector::detect
#     LineSegmentDetector::drawSegments
#     LineSegmentDetector::compareSegments
#     preCornerDetect

# 10. Object Detection
#     matchTemplate

#-------------------------------------------------------------------------------------------------------------------#
# 1. Image Filtering

# bilateralFilter
# void bilateralFilter(InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace,
# int borderType=BORDER_DEFAULT )
# Parameters:
# src        – Source 8-bit or floating-point, 1-channel or 3-channel image.
# dst        – Destination image of the same size and type as src .
# d          – Diameter of each pixel neighborhood that is used during filtering. If it is
#              non-positive, it is computed from sigmaSpace.
# sigmaColor – Filter sigma in the color space. A larger value of the parameter means that
#              farther colors within the pixel neighborhood
#              (see sigmaSpace ) will be mixed together, resulting in larger areas of semi-equal color.
# sigmaSpace – Filter sigma in the coordinate space. A larger value of the parameter means
#              that farther pixels will influence
#              each other as long as their colors are close enough (see sigmaColor ). When d>0 ,
#              it specifies the neighborhood size
#              regardless of sigmaSpace . Otherwise, d is proportional to sigmaSpace
# Setting parameters
# Sigma values: if sigmaColor and sigmaSpace < 10, not much effect
#               if sigmaColor and sigmaSpace > 150,the image look will look “cartoonish”
# Filter size:   d > 5 is slow
#                d=5 for real-time applications
#                d=9 for offline applications that need heavy noise filtering

bilateralFilter(src, dst, d::Int, sigmaColor::Float64, sigmaSpace::Float64, borderType=BORDER_DEFAULT) =
     @cxx cv::bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType)

# Blur an image using the normalized box filter
# void blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT )
# Parameters:
# src – input image; it can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
# dst – output image of the same size and type as src.
# ksize – blurring kernel size. (cv::Size)
# anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
# borderType – border mode used to extrapolate pixels outside of the image

blur(src, dst, ksize, anchor=cvPoint(-1,-1), borderType=BORDER_DEFAULT) = @cxx cv::blur(src, dst, ksize, anchor, borderType)

# Blur an image using the box filter
# void boxFilter(InputArray src, OutputArray dst, int ddepth, Size ksize, Point anchor=Point(-1,-1), bool normalize=true,
# int borderType=BORDER_DEFAULT )
# Parameters:
# src – input image.
# dst – output image of the same size and type as src.
# ddepth – the output image depth (-1 to use src.depth()).
# ksize – blurring kernel size.
# anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
# normalize – flag, specifying whether the kernel is normalized by its area or not.
# borderType – border mode used to extrapolate pixels outside of the image.

boxFilter(src, dst, ddepth::Int, ksize, anchor=cvPoint(-1,-1), normalize=true, borderType=BORDER_DEFAULT) =
    @cxx cv::boxFilter(src, dst, ddepth, ksize, anchor, normalize, borderType)

# Construct the Gaussian pyramid for an image
# void buildPyramid(InputArray src, OutputArrayOfArrays dst, int maxlevel, int borderType=BORDER_DEFAULT )
# Parameters:
# src        – Source image. Check pyrDown() for the list of supported types.
# dst        – Destination vector of maxlevel+1 images of the same type as src . dst[0] will be the same as src .
#               dst[1] is the next pyramid layer, a smoothed and down-sized src , and so on.
# maxlevel   – 0-based index of the last (the smallest) pyramid layer. It must be non-negative.
# borderType – Pixel extrapolation method (BORDER_CONSTANT don’t supported). See borderInterpolate for details.

buildPyramid(src, dst, maxlevel::Int, borderType=BORDER_DEFAULT) = @cxx cv::buildPyramid(src, dst, maxlevel, borderType)

# Dilate an image by using a specific structuring element
# void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT,
#      const Scalar& borderValue=morphologyDefaultBorderValue() )
# Parameters:
# src         – input image; the number of channels can be arbitrary, but the depth should be one of CV_8U, CV_16U, CV_16S, CV_32F` or ``CV_64F.
# dst         – output image of the same size and type as src.
# kernel      – structuring element used for dilation; if elemenat=Mat() ,
#               a 3 x 3 rectangular structuring element is used. Kernel can be created using getStructuringElement()
# anchor      – position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
# iterations  – number of times dilation is applied.
# borderType  – pixel extrapolation method (see borderInterpolate for details)
# borderValue – border value in case of a constant border  => cv::morphologyDefaultBorderValue()

dilate(src, dst, kernel, anchor=cvPoint(-1,-1), iterations=1, borderType=BORDER_DEFAULT) = @cxx cv::dilate(src, dst, kernel, anchor, iterations, borderType)

# Erode an image by using a specific structuring element
# void erode(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT,
#    const Scalar& borderValue=morphologyDefaultBorderValue() )
# Parameters:
# src         – input image; the number of channels can be arbitrary, but the depth should be one of CV_8U, CV_16U, CV_16S, CV_32F` or ``CV_64F.
# dst         – output image of the same size and type as src.
# kernel      – structuring element used for erosion; if element=Mat()
#               a 3 x 3 rectangular structuring element is used.
#               Kernel can be created using getStructuringElement().
# anchor      – position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
# iterations  – number of times erosion is applied.
# borderType  – pixel extrapolation method (see borderInterpolate for details)
# borderValue – border value in case of a constant border

erode(src, dst, kernel, anchor=cvPoint(-1,-1), iterations=1, borderType=BORDER_DEFAULT) = @cxx cv::erode(src, dst, kernel, anchor, iterations, borderType)

# Convolve an image with the kernel
# void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel,
#    Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT )
# Parameters:
# src         – input image.
# dst         – output image of the same size and the same number of channels as src.
# ddepth      – desired depth of the destination image
#               if < 0, it will be the same as src.depth() => supported
#               src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
#               src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
#               src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
#               src.depth() = CV_64F, ddepth = -1/CV_64F
#               if ddepth=-1, output image depth = source depth
# kernel      – convolution kernel (or rather a correlation kernel), a single-channel floating point matrix;
#               if you want to apply different kernels to different channels, split the image into separate color planes
#               using split() and process them individually.
# anchor      – anchor of the kernel that indicates the relative position of a filtered point within the kernel;
#               the anchor should lie within the kernel; default value (-1,-1) means that the anchor is at the kernel center.
# delta       – optional value added to the filtered pixels before storing them in dst.
# borderType  – pixel extrapolation method (see borderInterpolate for details)

filter2D(src, dst, ddepth, kernel, anchor=cvPoint(-1,-1), delta=float(0), borderType=BORDER_DEFAULT) = @cxx cv::filter2D(src, dst, ddepth, kernel, anchor, delta, borderType)

# Blur an image using a Gaussian filter
# void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT)
# Parameters:
# src         – input image; the image can have any number of channels, which are processed independently,
#                but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
# dst         – output image of the same size and type as src.
# ksize       – Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd.
#               Or, they can be zero’s and then they are computed from sigma* .
# sigmaX      – Gaussian kernel standard deviation in X direction.
# sigmaY      – Gaussian kernel standard deviation in Y direction;
#              if sigmaY==zero, sigma = sigmaX
#              if sigmaY==zero && sigmaX==zero both sigmas
#              computed from ksize.width and ksize.height
# borderType – pixel extrapolation method (see borderInterpolate for details

gaussianBlur(src, dst, ksize, sigmaX=10.0, sigmaY=float(0), borderType=BORDER_DEFAULT) = @cxx cv::GaussianBlur(src, dst, ksize, sigmaX, sigmaY, borderType)

# Return filter coefficients for computing spatial image derivatives
# void getDerivKernels(OutputArray kx, OutputArray ky, int dx, int dy, int ksize, bool normalize=false, int ktype=CV_32F )
# Parameters:
# kx        – Output matrix of row filter coefficients. It has the type ktype .
# ky        – Output matrix of column filter coefficients. It has the type ktype .
# dx        – Derivative order in respect of x.
# dy        – Derivative order in respect of y.
# ksize     – Aperture size. It can be CV_SCHARR , 1, 3, 5, or 7.
# normalize – Flag indicating whether to normalize (scale down) the filter coefficients or not.
#             Theoretically, the coefficients should have the denominator  =2^{ksize*2-dx-dy-2} .
#             If you are going to filter floating-point images, you are likely to use the normalized kernels.
#             But if you compute derivatives of an 8-bit image, store the results in a 16-bit image,
#             and wish to preserve all the fractional bits, you may want to set normalize=false .
# ktype     – Type of filter coefficients. It can be CV_32f or CV_64F .

getDerivKernels(kx, ky, dx, dy, ksize, normalize=false, ktype=CV_32F) = @cxx cv::getDerivKernels(kx, ky, dx, dy, ksize, normalize, ktype)

# Return Gaussian filter coefficients
# Mat getGaussianKernel(int ksize, double sigma, int ktype=CV_64F)
# Parameters:
# ksize – Aperture size. It should be odd ( \texttt{ksize} \mod 2 = 1 ) and positive.
# sigma – Gaussian standard deviation. If it is non-positive, it is computed from ksize as sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8 .
# ktype – Type of filter coefficients. It can be CV_32F or CV_64F .

getGaussianKernel(ksize, sigma::Float64, ktype=CV_64F) = @cxx cv::getDerivKernels(kx, ky, dx, dy, ksize, normalize, ktype)

# Return Gabor filter coefficients
# Mat getGaborKernel(Size ksize, double sigma, double theta, double lambd, double gamma, double psi=CV_PI*0.5, int ktype=CV_64F )
# Parameters:
# ksize – Size of the filter returned.
# sigma – Standard deviation of the gaussian envelope.
# theta – Orientation of the normal to the parallel stripes of a Gabor function.
# lambd – Wavelength of the sinusoidal factor.
# gamma – Spatial aspect ratio.
# psi   – Phase offset.
# ktype – Type of filter coefficients. It can be CV_32F or CV_64F .

getGaborKernel(ksize, sigma::Float64, theta::Float64, lambd::Float64, gamma::Float64, psi=CV_PI*0.5, ktype=CV_64F) =
  cv::getGaborKernel(ksize, sigma, theta, lambd, gamma, psi, ktype)

# Get structuring element of the specified size and shape for morphological operations
getStructuringElement(shape::Int, ksize, anchor=cvPoint(-1,-1)) = @cxx cv::getStructuringElement(shape, ksize, anchor)
# shape –
       # MORPH_RECT
       # MORPH_ELLIPSE
       # MORPH_CROSS
# ksize  – Size
# cols   – Width
# rows   – Height
# anchor – Anchor position, anchor_x, anchor_y

# Blur an image using the median filter
medianBlur(src, dst, ksize::Int) = @cxx cv::medianBlur(src, dst, ksize)
# src   – input 1-, 3-, or 4-channel image; when ksize is 3 or 5
# dst   – destination array
# ksize – aperture linear size

# Advanced morphological transformations
morphologyEx(src, dst, op::Int, kernel, anchor=cvPoint(-1,-1), iterations=1, borderType=BORDER_CONSTANT) =
    @cxx cv::morphologyEx(src, dst, op, kernel, anchor, iterations, borderType)
# optional: const Scalar& borderValue=morphologyDefaultBorderValue()
# src         – Source image: CV_8U, CV_16U, CV_16S, CV_32F or CV_64F
# dst         – Destination image
# kernel      – Structuring element obtained with getStructuringElement()
# anchor      – Anchor position with the kernel
# op –
#   MORPH_OPEN
#   MORPH_CLOSE
#   MORPH_GRADIENT
#   MORPH_TOPHAT
#   MORPH_BLACKHAT
# iterations   – Number of times erosion and dilation are applied
# borderType   – Pixel extrapolation method
# borderValue  – Border value in case of a constant border

# Laplacian of an image
laplacian(src, dst, ddepth::Int, ksize=5, scale=1.0, delta=float(0), borderType=BORDER_DEFAULT) =
    @cxx cv::Laplacian(src, dst, ddepth, ksize, scale, delta, borderType)

# Blur image and then downsample
pyrDown(src, dst, dstsize, borderType=BORDER_DEFAULT) = @cxx cv::pyrDown(src, dst, dstsize, borderType)
# dstsize = cvSize()

# Upsample image and then blur it
pyrUp(src, dst, dstsize, borderType=BORDER_DEFAULT) = @cxx cv::pyrUp(src, dst, dstsize, borderType)
# dstsize = cvSize()

# meanshift segmentation of an image
# pyrMeanShiftFiltering(src, dst, sp::Float64, sr::Float64, maxLevel=1,
#      termcrit=TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS,5,1) )

# Separable linear filter to an image
sepFilter2D(src, dst, ddepth::Int, kernelX, kernelY, anchor=cvPoint(-1,-1), delta=float(0), borderType=BORDER_DEFAULT) =
    @cxx cv::sepFilter2D(src, dst, ddepth, kernelX, kernelY, anchor, delta, borderType)
# supported ddepth
# src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
# src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
# src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
# src.depth() = CV_64F, ddepth = -1/CV_64F
# kernelX – Coefficients for filtering each row
# kernelY – Coefficients for filtering each column
# anchor  – Anchor position within the kernel
# delta   – Value added to the filtered results

# Edge detection with Sobel and Scharr operators
# Calculate the first, second, third, or mixed image derivatives using an extended Sobel operator
sobel(src, dst, ddepth::Int, dx::Int, dy::Int, ksize=3, scale=1.0, delta=float(0), borderType=BORDER_DEFAULT) =
     @cxx cv::Sobel(src, dst, ddepth, dx, dy, ksize, scale, delta, borderType)

# Calculate the first x- or y- image derivative using Scharr operator (edge detection)
scharr(src, dst, ddepth::Int, dx::Int, dy::Int, scale=1.0, delta=float(0), borderType=BORDER_DEFAULT) =
   @cxx cv::Scharr(src, dst, ddepth, dx, dy, scale, delta, borderType)
# xorder – order of the derivative x
# yorder – order of the derivative y
# ksize  – size of the extended Sobel kernel; it must be 1, 3, 5, or 7
# scale  – optional scale factor for the computed derivative values

#-------------------------------------------------------------------------------------------------------------------#
# 2. Geometric transformations

# Convert image transformation maps from one representation to another
convertMaps(map1, map2, dstmap1, dstmap2, dstmap1type::Int, nninterpolation=false) =
     @cxx cv::convertMaps(map1, map2, dstmap1, dstmap2, dstmap1type, nninterpolation)
# map1            – The first input map of type CV_16SC2, CV_32FC1, or CV_32FC2
# map2            – The second input map of type CV_16UC1 , CV_32FC1
# dstmap1         – The first output map
# dstmap2         – The second output map
# dstmap1type     – CV_16SC2 , CV_32FC1 , or CV_32FC2
# nninterpolation – Flag indicating whether the fixed-point maps are used

# Calculate an affine transform from three pairs of the corresponding points
getAffineTransform(src, dst) = @cxx cv::getAffineTransform(src, dst)

# Calculate a perspective transform from four pairs of the corresponding points
getPerspectiveTransform(src, dst) = @cxx cv::getPerspectiveTransform(src, dst)
# src: InputArray or const Point2f []
# dst: InputArray or const Point2f []

# Retrieve a pixel rectangle from an image with sub-pixel accuracy
getRectSubPix(image, patchSize, center, patch, patchType=-1) =  @cxx cv::getRectSubPix(image, patchSize, center, patch, patchType)
# src       – Source image
# patchSize – Size of the extracted patch
# center    – Floating point coordinates of the center of the extracted rectangle within the source image.
# dst       – Extracted patch that has the size patchSize and the same number of channels as src
# patchType – Depth of the extracted pixels

# Calculate an affine matrix of 2D rotation
getRotationMatrix2D(center, angle::Float64, scale::Float64) = @cxx cv::getRotationMatrix2D(center, angle, scale)
# center     – Center of the rotation in the source image
# angle      – Rotation angle in degrees
# scale      – Isotropic scale factor
# map_matrix – The output affine transformation, 2x3 floating-point matrix

# Invert an affine transformation.
invertAffineTransform(M, iM) = @cxx cv::invertAffineTransform(M, iM)

# Apply a generic geometrical transformation to an image
remap(src, dst, map1, map2, interpolation::Int, borderMode=BORDER_CONSTANT) =
    @cxx cv::remap(src, dst, map1, map2, interpolation, borderMode)
# map1          – The first map of either (x,y)
# map2          – The second map of y values having the type CV_16UC1 , CV_32FC1
# interpolation – Interpolation method
        # INTER_NEAREST - a nearest-neighbor interpolation
        # INTER_LINEAR - a bilinear interpolation (used by default)
        # INTER_AREA - resampling using pixel area relation
        # INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
        # NTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
# borderMode    – Pixel extrapolation method

# Resize an image
resize(src, dst, dsize, fx=float(0), fy=float(0), interpolation=INTER_LINEAR) =
    @cxx cv::resize(src, dst, dsize, fx, fy, interpolation)

# Apply an affine transformation to an image
warpAffine(src, dst, M, dsize, flags=INTER_LINEAR, borderMode=BORDER_CONSTANT) =
    @cxx cv::warpAffine(src, dst, M, dsize, flags, borderMode)
# optional: const Scalar& borderValue=Scalar()
# M – 2 x 3 transformation matrix

# Apply a perspective transformation to an image
warpPerspective(src, dst, M, dsize, flags=INTER_LINEAR, borderMode=BORDER_CONSTANT) =
    @cxx cv::warpPerspective(src, dst, M, dsize, flag, borderMode)
# optional: const Scalar& borderValue=Scalar()

# Compute the undistortion and rectification transformation map
initUndistortRectifyMap(cameraMatrix, distCoeffs, R, newCameraMatrix, size, m1type::Int, map1, map2) =
    @cxx cv::initUndistortRectifyMap(cameraMatrix, distCoeffs, R, newCameraMatrix, size, m1type, map1, map2)
# cameraMatrix     – Input camera matrix
# distCoeffs       – Input vector of distortion coefficients, 4, 5, or 8 elements
# R                – Optional rectification transformation in the object space (3x3 matrix).
# newCameraMatrix  – New camera matrix
# size             – Undistorted image size
# m1type           – Type of the first output map that can be CV_32FC1 or CV_16SC2
# map1             – The first output map
# map2             – The second output map

# Returns the default new camera matrix
getDefaultNewCameraMatrix(cameraMatrix, imgsize, centerPrincipalPoint=false) =
    @cxx cv::getDefaultNewCameraMatrix(cameraMatrix, imgsize, centerPrincipalPoint)
# imgsize              – Camera view image size in pixels
# centerPrincipalPoint – Location of the principal point in the new camera matrix

# Transforms an image to compensate for lens distortion
undistort(src, dst, cameraMatrix, distCoeffs, newCameraMatrix=noArray() ) = @cxx cv::undistort(src, dst, cameraMatrix, distCoeffs, )
# optional: newCameraMatrix=noArray()

# Compute the ideal point coordinates from the observed point coordinates
undistortPoints(src, dst, cameraMatrix, distCoeffs, R=noArray(), P=noArray()) = @cxx cv::undistortPoints(src, dst, cameraMatrix, distCoeffs,R ,P)
# optional: InputArray R=noArray(), InputArray P=noArray()
# R – Rectification transformation in the object space (3x3 matrix)
# P – New camera matrix (3x3) or new projection matrix (3x4)

#-------------------------------------------------------------------------------------------------------------------#
# 3. Miscellaneous Image Transformations

# Apply an adaptive threshold to an array
adaptiveThreshold(src, dst, maxValue::Float64, adaptiveMethod::Int, thresholdType::Int, blockSize::Int, C::Float64) =
    @cxx cv::adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C)
# adaptiveMethod:  ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C
# thresholdType:  THRESH_BINARY  or THRESH_BINARY_INV

# Convert an image from one color space to another
cvtColor(src, dst, code::Int, dstCn=0) = @cxx cv::cvtColor(src, dst, code, dstCn)
# code – color space conversion code, e.g., COLOR_BGR2GRAY

# Calculate the distance to the closest zero pixel for each pixel of the source
distanceTransform(src, dst, distanceType::Int, maskSize::Int, dstType=CV_32F) =
     @cxx cv::distanceTransform(src, dst, distanceType, maskSize, dstType=CV_32F)
# src          – 8-bit, single-channel (binary) source image
# dst          – Output image with calculated distances. 8-bit or 32-bit floating-point.
# distanceType – Type of distance.
#     DIST_L1
#     DIST_L2
#     DIST_C
# maskSize     – Size of the distance transform mask. It can be 3, 5, or DIST_MASK_PRECISE
# labels       – Optional output 2D array of labels (the discrete Voronoi diagram) - CV_32SC1
# labelType    – Type of the label array to build

# Fill a connected component with the given color
floodFill(image, seedPoint, newVal, rect, loDiff, SupDiff, flags=4) =
     @cxx cv::floodFill(image, seedPoint, newVal, rect, loDiff, SupDiff, flags)
# Rect* rect=0
# image        – Input/output 1- or 3-channel, 8-bit, or floating-point image
# mask         – Operation mask that should be a single-channel 8-bit image,
#                2 pixels wider and 2 pixels taller than image
# seedPoint    – Starting point
# newVal       – New value of the repainted domain pixels
# loDiff       – Maximal lower brightness/color difference
# upDiff       – Maximal upper brightness/color difference
# rect         – Optional output parameter set by the function to the minimum bounding rectangle
# flags        –
#       FLOODFILL_FIXED_RANGE
#       FLOODFILL_MASK_ONLY

# Calculate the integral of an image
integral(src, sum, sdepth=-1) = @cxx cv::integral(src, sum, sdepth)
integral(src, sum, sqsum, sdepth=-1, sqdepth=-1) = @cxx cv::integral(src, sum, sqsum, sdepth, sqdepth)
# image   – input image as W x H, 8-bit or floating-point (32f or 64f).
# sum     – integral image as (W+1) x (H+1), 32-bit integer or floating-point (32f or 64f).
# sqsum   – integral image for squared pixel values
# tilted  – integral for the image rotated by 45 degrees
# sdepth  – depth of the integral and the tilted integral images, CV_32S, CV_32F, or CV_64F
# sqdepth – depth of the integral image of squared pixel values, CV_32F or CV_64F

# Apply a fixed-level threshold to each array element
threshold(src, dst, thresh, maxval, ttype::Int) =
    @cxx cv::threshold(src, dst, float(thresh), float(maxval), ttype)
# src     – input array (single-channel, 8-bit or 32-bit floating point)
# dst     – output array
# thresh  – threshold value
# maxval  – maximum value with THRESH_BINARY and THRESH_BINARY_INV
# ttype   – thresholding type
#      THRESH_BINARY
#      THRESH_BINARY_INV
#      THRESH_TRUNC
#      THRESH_TOZERO
#      THRESH_TOZERO_INV

# Marker-based image segmentation using the watershed algorithm
watershed(image, markers) = @cxx cv::watershed(image, markers)
# image   – Input 8-bit 3-channel image
# markers – Input/output 32-bit single-channel image (map) of markers

# Run the GrabCut algorithm
grabCut(img, mask, rect, bgdModel, fgdModel, iterCount::Int, mode=GC_EVAL) =
     @cxx cv::grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode)
# img       – Input 8-bit 3-channel image
# mask –
      # GC_BGD = obvious background pixels
      # GC_FGD = obvious foreground (object) pixel
      # GC_PR_BGD = possible background pixel
      # GC_PR_FGD = possible foreground pixel
# rect      – ROI containing a segmented object
# bgdModel  – Temporary array for the background model
# fgdModel  – Temporary arrays for the foreground model
# iterCount – Number of iterations the algorithm should make before returning the result
# mode –
     # GC_INIT_WITH_RECT
     # GC_INIT_WITH_MASK
     # GC_EVAL

#-------------------------------------------------------------------------------------------------------------------#
# 4. Drawing functions

# Draw a circle
circle(img, center, radius::Int, color, thickness=1, lineType=LINE_8, shift=0) =
    @cxx cv::circle(img, center, radius, color, thickness, lineType, shift)
# Clip the line against the image rectangle
clipLine(imgSize, pt1, pt2) = @cxx cv::clipLine(imgSize, pt1, pt2)  #cvSize
clipLine(imgRect, pt1, pt2) = @cxx cv::clipLine(imgRect, pt1, pt2)  #cvRect
# Draw a simple or thick elliptic arc or fills an ellipse sector
ellipse(img, center, axes, angle::Float64, startAngle::Float64, endAngle::Float64, color, thickness=1, lineType=LINE_8, shift=0) =
    @cxx cv::ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType, shift)
# img         – Image
# center      – Center of the ellipse
# axes        – Half of the size of the ellipse main axes
# angle       – Ellipse rotation angle in degrees
# startAngle  – Starting angle of the elliptic arc in degrees
# endAngle    – Ending angle of the elliptic arc in degrees
# box         – Alternative ellipse representation
# color       – Ellipse color
# thickness   – Thickness of the ellipse arc outline
# lineType    – Type of the ellipse boundary
# shift       – Number of fractional bits in the coordinates of the center and values of axes

# Approximate an elliptic arc with a polyline
ellipse2Poly(center, axes, angle::Int, arcStart::Int, arcEnd::Int, delta::Int, pts) =
     @cxx cv::ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta, pts)
# pts = vector<Point>&
# center   – Center of the arc.
# axes     – Half of the size of the ellipse main axes
# angle    – Rotation angle of the ellipse in degrees
# arcStart – Starting angle of the elliptic arc in degrees
# arcEnd   – Ending angle of the elliptic arc in degrees
# delta    – Angle between the subsequent polyline vertices
# pts      – Output vector of polyline vertices

# Fills a convex polygon
fillConvexPoly(img, pts, npts::Int, color, lineType=LINE_8, shift=0) = @cxx cv::fillConvexPoly(img, pts, npts, color, lineType, shift)
# pts = const Point*
fillConvexPoly(img, points, color, lineType=LINE_8, shift=0) = @cxx cv::fillConvexPoly(img, points, color, lineType, shif)

# Fill the area bounded by one or more polygons
fillPoly(img, pts, npts::Ptr{Int}, ncontours::Int, color, lineType=LINE_8, shift=0) =
    @cxx cv::fillPoly(img, pts, npts, ncontours, color, lineType, shift)
# pts = const Point**
# npts = const int*
# optional: Point offset=Point()

fillPoly(img, pts, color, lineType=LINE_8, shift=0) = @cxx cv::fillPoly(img, pts, color, lineType, shift)
# pts = InputArrayOfArrays

# Calculate the width and height of a text string
getTextSize(text::String, fontFace::Int, fontScal::Float64, thickness::Int, baseLine::Ptr{Int}) =
    @cxx cv::getTextSize(pointer(text), fontFace, fontScal, thickness, baseLine)
# text       – Input text string
# fontFace   – Font to use
# fontScale  – Font scale
# thickness  – Thickness of lines
# baseLine   – Output parameter - y-coordinate of the baseline
# text_size  – Output parameter - The size of a box that contains the specified text

# Draw a line segment connecting two points
line(img, pt1, pt2, color, thickness=1, lineType=LINE_8, shift=0) =
     @cxx cv::line(img, pt1, pt2, color, thickness, lineType, shift)
# img       – Image
# pt1       – First point
# pt1       – First point
# color     – Line color
# thickness – Line thickness
# lineType  –
#        LINE_8
#        LINE_4
#        LINE_AA
# shift     – Number of fractional bits in the point coordinates

# Draw a arrow segment pointing from the first point to the second one
arrowedLine(img, pt1, pt2, color, thickness=1, lineType=8, shift=0, tipLength=0.1) =
    @cxx cv::arrowedLine(img, pt1, pt2, color, thickness, lineType, shift, tipLength)
# tipLength – The length of the arrow tip in relation to the arrow length

# Class for iterating pixels on a raster line
# cv::LineIterator
# see http://docs.opencv.org/trunk/modules/imgproc/doc/drawing_functions.html

# Draw a simple, thick, or filled up-right rectangle
rectangle(img, pt1, pt2, color, thickness=1, lineType=LINE_8, shift=0) =
     @cxx cv::rectangle(img, pt1, pt2, color, thickness, lineType, shift)

# Draws several polygonal curves
polylines(img, pts, npts::Ptr{Int}, ncontours::Int, isClosed, color, thickness=1, lineType=LINE_8, shift=0) =
     @cxx cv::polylines(img, pts, npts, ncontours, isClosed, color, thickness, lineType, shift)
# pts = const Point* const*

polylines(img, pts, isClosed,color, thickness=1, lineType=LINE_8, shift=0) =
     @cxx cv::polylines(img, pts, isClosed,color, thickness, lineType, shift)
# pts – Array of polygonal curves

# Draw contours outlines or filled contours
# draw contours
INT_MAX = 0 # draw only the specified contour is drawn
# INT_MAX = 1 # draws the contour(s) and all the nested contours
# INT_MAX = 2 # draws the contours, all the nested contours,and so on...

drawContours(image, contours, contourIdx::Int, color, thickness=1, lineType=LINE_8, hierarchy=noArray(), maxLevel=INT_MAX, offset=cvPoint(0,0)) =
    @cxx drawContours(image, contours, contourIdx, color, thickness, lineType, hierarchy, maxLevel, offset)
# optional:  hierarchy=noArray()
# optional:  Point offset=Point()
# offset   - Optional contour shift parameter (x,y)

# Draws a text string
putText(img, text::String, org, fontFace::Int, fontScale::Float64, color, thickness=1, lineType=LINE_8,
  bottomLeftOrigin=false) =  @cxx cv::putText(img, pointer(text), org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin)
# fontFace – Font type
# FONT_HERSHEY_SIMPLEX
# FONT_HERSHEY_PLAIN
# FONT_HERSHEY_DUPLEX
# FONT_HERSHEY_COMPLEX
# FONT_HERSHEY_TRIPLEX
# FONT_HERSHEY_COMPLEX_SMALL
# FONT_HERSHEY_SCRIPT_SIMPLEX
# FONT_HERSHEY_SCRIPT_COMPLEX
# FONT_ITALIC

#-------------------------------------------------------------------------------------------------------------------#
# 5. ColorMaps in OpenCV

# Appl a GNU Octave/MATLAB equivalent colormap on a given image
applyColorMap(src, dst, colormap::Int) = @cxx cv::applyColorMap(src, dst, colormap)
# colormap =
#     COLORMAP_AUTUMN
#     COLORMAP_BONE
#     COLORMAP_JET
#     COLORMAP_WINTER
#     COLORMAP_RAINBOW
#     COLORMAP_OCEAN
#     COLORMAP_SUMMER
#     COLORMAP_SPRING
#     COLORMAP_COOL
#     COLORMAP_HSV
#     COLORMAP_PINK
#     COLORMAP_HOT

#-------------------------------------------------------------------------------------------------------------------#
# 6. Histograms

calcHist(images, nimages::Int, channels::Ptr{Int}, mask, hist, dims::Int, histSize::Ptr{Int}, ranges::Ptr{Ptr{Float64}}, uniform=true,
    accumulate=false) = @cxx cv::calcHist(images, nimages, channels, mask, hist, dims, histSize, ranges, uniform, accumulate)
# images = const Mat*
# Version1    -> hist = Mat&
# Version2    -> hist = SparseMat&
# nimages     – Number of source images
# channels    – The list of channels used to compute the back projection.
# hist        – Input histogram that can be dense or sparse
# backProject – Destination back projection array
# ranges      – Array of arrays of the histogram bin boundaries in each dimension
# scale       – Optional scale factor for the output back projection
# uniform     – Flag indicating whether the histogram is uniform or not
# accumulate  – Accumulation flag: compute a single histogram from several sets of arrays, or to update the histogram in time

# Calculate the back projection of a histogram
calcBackProject(images, nimages::Int, channels::Ptr{Int}, hist, backProject, ranges::Ptr{Ptr{Float64}}, scale=1.0, uniform=true) =
    @cxx cv::calcBackProject(images, nimages, channels, hist, backProject, ranges, scale, uniform)
# parameters same as above

# Compare two histograms
compareHist(H1, H2, method::Int) = @cxx cv::compareHist(H1, H2, method)
# Version1 -> H1, H2 = Mat&
# Version2 -> H1, H2 = SparseMat&
# method –
# HISTCMP_CORREL        Correlation
# HISTCMP_CHISQR        Chi-Square
# HISTCMP_CHISQR_ALT    Alternative Chi-Square
# HISTCMP_INTERSECT     Intersection
# HISTCMP_BHATTACHARYYA Bhattacharyya distance
# HISTCMP_HELLINGER     Synonym for CV_COMP_BHATTACHARYYA
# HISTCMP_KL_DIV        Kullback-Leibler divergence

# EMD: Computes the “minimal work” distance between two weighted point configurations
emd(signature1, signature2, distType::Int, cost=noArray(),lowerBound=[pointer(float(0))], flow=noArray()) =
    @cxx cv::emd(signature1, signature2, distType, cost, lowerBound, flow)
# optional: InputArray cost=noArray()
# optional: OutputArray flow=noArray()

# equalizeHist: Equalizes the histogram of a grayscale image
equalizeHist(src, dst) = @cxx cv::equalizeHist(src, dst)

#-------------------------------------------------------------------------------------------------------------------#
# 7. Structural Analysis and Shape Descriptors

# moments
moments(array, binaryImage=false) = @cxx cv::moments(array, binaryImage)
# array       – Raster image (single-channel, 8-bit or floating-point 2D array)
# binaryImage – If it is true, all non-zero image pixels are treated as 1’s
# moments     – Output moments

# Calculates seven Hu invariant
huMoments(m, hu) = @cxx cv::HuMoments(m, hu)

cxx"""
void humoments(const cv::Moments& moments)
  {
    std::vector<double>hu(7);
    cv::HuMoments(moments, hu);
  }
"""

huMoments(moments) = @cxx humoments(moments)

# connectedComponents
connectedComponents(image, labels, connectivity=8, ltype=CV_32S) = @cxx cv::connectedComponents(image, labels, connectivity, ltype)
connectedComponentsWithStats(image, labels, stats, centroids, connectivity=8, ltype=CV_32S) =
    @cxx cv::connectedComponentsWithStats(image, labels, stats, centroids, connectivity, ltype)
# image        – the image to be labeled
# labels       – destination labeled image
# connectivity – 8 or 4 for 8-way or 4-way connectivity respectively
# ltype        – output image label type. Currently CV_32S and CV_16U are supported.
# statsv –
#     CC_STAT_LEFT    leftmost (x) of bounding box in the horizontal direction
#     CC_STAT_TOP     topmost (y)  of the bounding box in the vertical direction
#     CC_STAT_WIDTH   horizontal size of the bounding box
#     CC_STAT_HEIGHT  vertical size of the bounding box
#     CC_STAT_AREA    The total area (in pixels) of the connected component
# centroids     – floating point centroid (x,y) output for each label, including the background label

# findContours: Finds contours in a binary image
findContours(image, contours, hierarchy, mode::Int, method::Int) = @cxx cv::findContours(image, contours, hierarchy, mode, method)
findContours(image, contours, mode::Int, method::Int) = @cxx cv::findContours(image, contours, mode, method)
# optional: Point offset=Point()
# Parameters
# image     – Source, an 8-bit single-channel image
# contours  – Detected contours. Each contour is stored as a vector of points
# hierarchy – Optional output vector, containing information about the image topology
# mode –
# RETR_EXTERNAL        extreme outer contours. It sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours
# RETR_LIST            all contours without establishing any hierarchical relationships
# RETR_CCOMP           all contours and organizes them into a two-level hierarchy
# RETR_TREE            all contours and reconstructs a full hierarchy of nested contours
# method –
# CHAIN_APPROX_NONE       all the contour points.
# CHAIN_APPROX_SIMPLE     compresses horizontal, vertical, and diagonal segments and leaves only their end points
# CHAIN_APPROX_TC89_L1
# CHAIN_APPROX_TC89_KCOS  Teh-Chin chain approximation algorithm
# offset      –           Optional offset by which every contour point is shifted
#                         useful if the contours are extracted from the image ROI

# approxPolyDP: Approximate a polygonal curve(s) with the specified precision
approxPolyDP(curve, approxCurve, epsilon::Float64, closed=false) = @cxx cv::approxPolyDP(curve, approxCurve, epsilon, closed)
# curve         –    Input vector of a 2D point stored in: std::vector or Mat
# approxCurve   –    Result of the approximation. The type should match the type of the input curve
# epsilon       –    Parameter specifying the approximation accuracy
# closed        –    bool = true, then approximated curve is closed. Otherwise, not closed
# header_size   –    Header size of the approximated curve. Normally, sizeof(CvContour)
# storage       –    Memory storage where the approximated curve is stored
# method        –    Contour approximation algorithm. Only CV_POLY_APPROX_DP is supported.
# recursive     –    Recursion flag.

# arcLength: Calculate a contour perimeter or a curve length
arcLength(curve, closed) = @cxx cv::arcLength(curve, closed)
# curve  – Input vector of 2D points, stored in std::vector or Mat
# closed – Flag indicating whether the curve is closed or not

# boundingRect: Calculate the up-right bounding rectangle of a point set
boundingRect(points) = @cxx cv::boundingRect(points)

# contourArea: Calculates a contour area
contourArea(contour, oriented=false) = @cxx cv::contourArea(contour, oriented)
# contour      – Input vector of 2D points (contour vertices), stored in std::vector or Mat
# oriented     – Oriented area flag, default = false
#                if true, signed area value depending on clockwise or counter-clockwise
#                used to determine orientation of a contour by taking the sign of an area

# convexHull: Finds the convex hull of a point set
convexHull(points, hull, clockwise=false, returnPoints=true) = @cxx cv::convexHull(points, hull, clockwise, returnPoints)
# points       – Input 2D point set, stored in std::vector or Mat
# hull         – Output convex hull: integer vector of indices or vector of points
# clockwise    – Orientation flag. If true, the output convex hull is oriented clockwise. Otherwise, counter-clockwise.
# returnPoints – Operation flag. In case of a matrix, when the flag is true, the function returns convex hull points.

# convexityDefects: Finds the convexity defects of a contour
convexityDefects(contour, convexhull, convexityDefects) = @cxx cv::convexityDefects(contour, convexhull, convexityDefects)
# contour          – Input contour
# convexhull       – Convex hull obtained using convexHull()
# convexityDefects – The output vector of convexity defects: 4-element integer vector (a.k.a. cv::Vec4i)

# fitEllipse: Fits an ellipse around a set of 2D points
fitEllipse(points) = @cxx cv::fitEllipse(points)
# points – Input 2D point set, stored in std::vector<> or Mat
# returns: cvRotatedRect

# fitLine: Fits a line to a 2D or 3D point set
fitLine(points, line, distType::Int, param::Float64, reps::Float64, aeps::Float64) =
    @cxx cv::fitLine(points, line, distType, param, reps, aeps)
# points   – Input vector of 2D or 3D points, stored in std::vector<> or Mat.
# line     – Output line parameters.
#             2D: 4 elements (like Vec4f) normalized vector colinear, (x0, y0) is on the line
#             3D: 6 elements (like Vec6f) normalized vector colinear, (x0, y0, z0) is on the line
# distType – Distance used by the M-estimator
#     DIST_L2
#     DIST_L1
#     DIST_FAIR
#     DIST_WELSCH
#     DIST_HUBER
# param    – Numerical parameter (C) for some types of distances. If 0, optimal is chosen.
# reps     – radius accuracy, set default = 0.01 (distance between the coordinate origin and the line)
# aeps     – angle accuracy, set default = 0.01

# isContourConvex: Tests a contour convexity
isContourConvex(contour) = @cxx cv::isContourConvex(contour)
# contour – 2D std::vector<> or Mat

# minAreaRect: Find a rotated rectangle of the minimum area enclosing the input 2D point set
minAreaRect(points) = @cxx cv::minAreaRect(points)
# contour – 2D std::vector<> or Mat
# returns RotatedRect

# boxPoints: Find the four vertices of a rotated rect. Useful to draw the rotated rectangle
boxPoints(box, points) = @cxx cv::boxPoints(box, points)
# box      – The input rotated rectangle
# points   – The output array of four vertices of rectangles.

# minEnclosingTriangle: Finds a triangle of minimum area enclosing a 2D point set and returns its area
minEnclosingTriangle(points, triangle) = @cxx cv::minEnclosingTriangle(points, triangle)
# points   – Input vector of 2D points with depth CV_32S or CV_32F in std::vector<> or Mat
# triangle – Output vector of three 2D points defining the vertices of the triangle, must be CV_32F

# minEnclosingCircle: Finds a circle of the minimum area enclosing a 2D point set
minEnclosingCircle(points, center, radius) = @cxx cv::minEnclosingCircle(points, center, radius)
# points     – Input vector of 2D points in std::vector<> or Mat
# center     – Output center of the circle, cvPoint2f&
# radius     – Output radius of the circle, double&

# matchShapes: Compares two shapes
matchShapes(contour1, contour2,method::Int, parameter::Float64) =
     @cxx cv::matchShapes(contour1, contour2,method::Int, parameter::Float64)
# object1 – First contour or grayscale image
# object2 – Second contour or grayscale image
# method –  all 3 use the Hu invariants
#    CV_CONTOURS_MATCH_I1
#    CV_CONTOURS_MATCH_I2
#    CV_CONTOURS_MATCH_I3

# pointPolygonTest: Performs a point-in-contour test
pointPolygonTest(contour, pt, measureDist) = @cxx cv::pointPolygonTest(contour, pt, measureDist)
# contour     – Input contour
# pt          – Point tested against the contour
# measureDist – If true, estimates signed distance from the point to the nearest contour edge

# rotatedRectangleIntersection: Find out if there is any intersection between two rotated rectangles
rotatedRectangleIntersection(rect1, rect2, intersectingRegion) =
    @cxx cv::rotatedRectangleIntersection(rect1, rect2, intersectingRegion)
# rect1                – First rectangle  (cvRotatedRect)
# rect2                – Second rectangle (cvRotatedRect)
# intersectingRegion   – The output array of the verticies of the intersecting region
#                        max 8 vertices => std::vector<cv::Point2f> or cv::Mat as Mx1 of type CV_32FC2
# pointCount           – The number of vertices
# Output
# INTERSECT_NONE=0     – No intersection
# INTERSECT_PARTIAL=1  – There is a partial intersection
# INTERSECT_FULL=2     – One of the rectangle is fully enclosed in the other

#-------------------------------------------------------------------------------------------------------------------#
# 8. Motion Analysis and Object Tracking

# accumulate: Adds an image to the accumulator
accumulate(src, dst) = @cxx cv::accumulate(src, dst)
# accumulateSquare: Adds the square of a source image to the accumulator
accumulateSquare(src, dst,mask=noArray()) = @cxx cv::accumulateSquare(src, dst, mask)
# optional: InputArray mask=noArray()
# src         – Input image as 1- or 3-channel, 8-bit or 32-bit floating point
# dst         – Accumulator image with 32-bit or 64-bit floating-point (same channels as src)

# accumulateProduct: Adds the per-element product of two input images to the accumulator
accumulateProduct(src1, src2, dst, mask=noArray()) = @cxx cv::accumulateProduct(src1, src2, dst, mask)
# optional: InputArray mask=noArray()
# src1, src2  – Input images as 1- or 3-channel, 8-bit or 32-bit floating point
# dst         – Accumulator image with 32-bit or 64-bit floating-point (same channels as src)

# accumulateWeighted: Updates a running average
accumulateWeighted(src, dst, alpha::Float64, mask=noArray()) = @cxx cv::accumulateWeighted(src, dst, alpha, mask)
# optional: InputArray mask=noArray()
# src1, src2  – Input images as 1- or 3-channel, 8-bit or 32-bit floating point
# dst         – Accumulator image with 32-bit or 64-bit floating-point (same channels as src
# alpha       – Weight of the input image

# phaseCorrelate: detect translational shifts that occur between two images
phaseCorrelate(src1, src2, window=noArray(), response=pointer(float(0))) = @cxx cv::phaseCorrelate(src1, src2, window, response)
# window=noArray()
# src1        – Source floating point array (CV_32FC1 or CV_64FC1)
# src2        – Source floating point array (CV_32FC1 or CV_64FC1)
# window      – Floating point array with windowing coefficients to reduce edge effects (optional)
# response    – Signal power within the 5x5 centroid around the peak, between 0 and 1 (optional)

# createHanningWindow: computes a Hanning window coefficients in two dimensions
createHanningWindow(dst, winSize, rtype::Int) = @cxx cv::createHanningWindow(dst, winSize, rtype)
# dst         – Destination array to place Hann coefficients in
# winSize     – The window size specifications
# winSize     – The window size specifications

#-------------------------------------------------------------------------------------------------------------------#
# 9. Feature Detection

# Canny: Finds edges in an image using the [Canny86] algorithm
Canny(image, edges, threshold1::Float64, threshold2::Float64, apertureSize=3, L2gradient=false) =
    @cxx cv::Canny(image, edges, threshold1, threshold2, apertureSize, L2gradient)
# image         – 8-bit input image
# edges         – output edge map; single channels 8-bit image, which has the same size as image
# threshold1    – first threshold for the hysteresis procedure
# threshold2    – second threshold for the hysteresis procedure
# apertureSize  – aperture size for the Sobel() operator
# L2gradient    – a flag, indicating whether a more accurate

# cornerEigenValsAndVecs
cornerEigenValsAndVecs(src, dst, blockSize::Int, ksize::Int, borderType=BORDER_DEFAULT) =
    @cxx cv::cornerEigenValsAndVecs(src, dst, blockSize, ksize, borderType)
# src          – Input single-channel 8-bit or floating-point image
# dst          – Image to store the results. It has the same size as src and the type CV_32FC(6)
# blockSize    – Neighborhood size
# ksize        – Aperture parameter for the Sobel() operator
# borderType   – Pixel extrapolation method. See borderInterpolate()

# cornerHarris: Harris corner detector
cornerHarris(src, dst, blockSize::Int, ksize::Int, k::Float64, borderType=BORDER_DEFAULT) =
    @cxx cv::cornerHarris(src, dst, blockSize, ksize, k, borderType)
# src          – Input single-channel 8-bit or floating-point image
# dst          – Image to store the Harris detector responses. It has the type CV_32FC1
# blockSize    – Neighborhood size
# ksize        – Aperture parameter for the Sobel() operator
# k            – Harris detector free parameter
# borderType   – Pixel extrapolation method

# cornerMinEigenVal: Calculates the minimal eigenvalue of gradient matrices for corner detection
cornerMinEigenVal(src, dst, blockSize::Int,ksize=3, borderType=BORDER_DEFAULT) =
     @cxx cv::cornerMinEigenVal(src, dst, blockSize,ksize, borderType)
# src          – Input single-channel 8-bit or floating-point image
# dst          – Image to store the Harris detector responses. It has the type CV_32FC1
# blockSize    – Neighborhood size
# ksize        – Aperture parameter for the Sobel() operator
# borderType   – Pixel extrapolation method

# cornerSubPix: Refines the corner locations
cornerSubPix(image, corners, winSize, zeroZone, criteria) =
     @cxx cv::cornerSubPix(image, corners, winSize, zeroZone, criteria)
# image         – Input image
# corners       – Initial coordinates of the input corners and refined coordinates provided for output
# winSize       – Half of the side length of the search window e.g., winSize=Size(5,5)
# zeroZone      – Half of the size of the dead region in the middle of the search zone
# criteria      – Criteria for termination of the iterative process of corner refinement  (TermCriteria)

# goodFeaturesToTrack:
goodFeaturesToTrack(image, corners, maxCorners::Int, qualityLevel::Float64, minDistance::Float64,mask=noArray(),
     blockSize=3, useHarrisDetector=false, k=0.04) = @cxx cv::goodFeaturesToTrack(image, corners,
         maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k)
# image             – Input 8-bit or floating-point 32-bit, single-channel image.
# corners           – Output vector of detected corners
# maxCorners        – Maximum number of corners to return
# qualityLevel      – Parameter characterizing the minimal accepted quality of image corners
# minDistance       – Minimum possible Euclidean distance between the returned corners
# blockSize         – Size of an average block for computing a derivative covariation matrix over each pixel neighborhood.
# useHarrisDetector – Parameter indicating whether to use a Harris detector (see cornerHarris()) or cornerMinEigenVal().
# optional:           Region of interest - InputArray mask=noArray()
# k                 – Free parameter of the Harris detector

method=HOUGH_GRADIENT  # only option now
# HoughCircles: Finds circles in a grayscale image using the Hough transform
houghCircles(image, circles, method, dp::Float64, minDist::Float64, param1=100.0, param2=100.0,
      minRadius=0, maxRadius=0) = @cxx cv::HoughCircles(image, circles, method, dp, minDist, param1,
            param2, minRadius, maxRadius)
# image      – 8-bit, single-channel, grayscale input image.
# circles    – Output vector of found circles. 3-element floating-point vector (x, y, radius)
# method     – Detection method = HOUGH_GRADIENT, described in [Yuen90].
# dp         – Inverse ratio of the accumulator resolution to the image resolution
#             dp=1 : resolution unchanged
#             dp=2 : 1/2 resolution of original
# minDist    – Minimum distance between the centers of the detected circles
# param1     – First method-specific parameter (Canny() edge detector threshold)
# param2     – Second method-specific parameter (accumulator threshold for circle centers)
# minRadius  – Minimum circle radius
# maxRadius  – Maximum circle radius

# HoughLines: Finds lines in a binary image using the standard Hough transform
houghLines(image, lines, rho::Float64, theta::Float64, threshold::Int, srn=float(0),
     stn=float(0), min_theta =float(0), max_theta=CV_PI) = @cxx cv::HoughLines(image, lines,
          rho, theta, threshold, srn, stn, min_theta, max_theta)
# image       – 8-bit, single-channel binary source image
# lines       – Output vector of lines
# rho         – Distance resolution of the accumulator in pixels
# theta       – Angle resolution of the accumulator in radians
# threshold   – Accumulator threshold parameter
# srn         – For the multi-scale Hough transform, it is a divisor for the distance resolution rho
# stn         – For the multi-scale Hough transform, it is a divisor for the distance resolution theta
# min_theta   – minimum angle to check for lines - range {0, max_theta}
# min_theta   – maximum angle to check for lines - range {min_theta, CV_PI}
# method –
#         HOUGH_STANDARD
#         HOUGH_PROBABILISTIC
#         HOUGH_MULTI_SCALE

# HoughLinesP: Finds line segments in a binary image using the probabilistic Hough transform
houghLinesP(image, lines, rho::Float64, theta::Float64, threshold::Int, minLineLength=float(0), maxLineGap=float(0)) =
     @cxx cv::HoughLinesP(image, lines, rho, theta, threshold, minLineLength, maxLineGap)
# same as above, but with the following added
# minLineLength   – Minimum line length. Line segments shorter than that are rejected
# maxLineGap      – Maximum allowed gap between points on the same line to link them

# createLineSegmentDetector
createLineSegmentDetector(_refine=LSD_REFINE_STD, _scale=0.8, _sigma_scale=0.6, _quant=2.0, _ang_th=22.5,
    _log_eps=float(0), _density_th=0.7, _n_bins=1024) = @cxx cv::createLineSegmentDetector(_refine,
          _scale, _sigma_scale, _quant, _ang_th, _log_eps, _density_th, _n_bins)
# _refine  -
# LSD_REFINE_NONE
# LSD_REFINE_STD
# LSD_REFINE_ADV
# scale       – The scale of the image that will be used to find the lines. Range (0..1]
# sigma_scale – Sigma for Gaussian filter. It is computed as sigma = _sigma_scale/_scale.
# quant       – Bound to the quantization error on the gradient norm.
# ang_th      – Gradient angle tolerance in degrees
# log_eps     – Detection threshold: -log10(NFA) > log_eps. (ONLY REFINE_ADV)
# density_th  – Minimal density of aligned region points in the enclosing rectangle
# n_bins      – Number of bins in pseudo-ordering of gradient modulus

# LineSegmentDetector::detect
detectLines(linesegmentDetector, _image, _lines, width=noArray(),prec=noArray(),nfa=noArray()) = @cxx cv::linesegmentDetector->detect(_image, _lines, width, prec, nfa)
# _lines        – A vector of Vec4i elements specifying the beginning and ending point of a line
# nfa           – Vector containing number of false alarms
#                 -1 =>  10 mean false alarms
#                  0 =>   1 mean false alarm
#                  1 => 0.1 mean false alarms
# Default:
# OutputArray width=noArray()
# OutputArray prec=noArray()
# OutputArray nfa=noArray()

# LineSegmentDetector::drawSegments
drawSegments(linesegmentDetector, _image, lines)  = @cxx cv::linesegmentDetector->drawSegments(_image, lines)
# image – The image, where the liens will be drawn
# lines – A vector of the lines that needed to be drawn

# LineSegmentDetector::compareSegments
compareSegments(linesegmentDetector, size, lines1, lines2, image)  = @cxx cv::linesegmentDetector->compareSegments(size, lines1, lines2, image)
# size     – The size of the image, where lines1 and lines2 were found.
# lines1   – The first group of lines that needs to be drawn. It is visualized in blue color.
# lines2   – The second group of lines. They visualized in red color.
# image    – Optional image, where the lines will be drawn.

# preCornerDetect: Calculates a feature map for corner detection
preCornerDetect(src, dst, ksize::Int, borderType=BORDER_DEFAULT) = @cxx cv::preCornerDetect(src, dst, ksize, borderType)
# src – Source single-channel 8-bit of floating-point image
# dst – Output image that has the type CV_32F and the same size as src
# ksize – Aperture size of the Sobel()
# borderType – Pixel extrapolation method

#-------------------------------------------------------------------------------------------------------------------#
# 10. Object Detection

# matchTemplate: Compares a template against overlapped image regions
matchTemplate(image, templ, result, method::Int) = @cxx cv::matchTemplate(image, templ, result, method)
# image    – Image where the search is running. It must be 8-bit or 32-bit floating-point
# templ    – Searched template. It must be not greater than the source image and have the same data type.
# result   – Map of comparison results. It must be single-channel 32-bit floating-point
# method   – Parameter specifying the comparison method
#    TM_SQDIFF
#    TM_SQDIFF_NORMED
#    TM_CCORR
#    TM_CCORR_NORMED
#    TM_CCOEFF
#    TM_CCOEFF_NORMED