Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add tests for chessboard corner detection #52

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
OpenCV = "f878e3a2-a245-4720-8660-60795d644f2a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
XML = "72c71f33-b9b6-44de-8c94-c961784809e2"
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ using LazyArtifacts
using OpenCV
using FileIO
using Test
using XML
using LinearAlgebra

if "OPENCV_TEST_DATA_PATH" in keys(ENV)
test_dir = joinpath(ENV["OPENCV_TEST_DATA_PATH"], "cv")
Expand All @@ -18,4 +20,5 @@ end
include("test_objdetect.jl")
include("test_dnn.jl")
include("test_fileio.jl")
include("test_corner_detection.jl")
end
73 changes: 73 additions & 0 deletions test/test_corner_detection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function _detect_corners(file, n_corners)
img = load(file)
gry = img[1:1, :, :]
cv_n_corners = OpenCV.Size{Int32}(n_corners...)
_cv_corners = OpenCV.Mat(Array{Float32}(undef, 2, 1, prod(n_corners)))
timholy marked this conversation as resolved.
Show resolved Hide resolved
ret, cv_corners = OpenCV.findChessboardCorners(gry, cv_n_corners, _cv_corners, 0)
@assert ret "Failed to detect any corners!"
corners = reshape(vec.(eachslice(cv_corners, dims = 3)), n_corners)
return corners
end

function parse_corners_file(file)
open(file, "r") do o
readuntil(o, "rows:")
rows = parse(Int, readuntil(o, "\n"))
readuntil(o, "cols:")
cols = parse(Int, readuntil(o, "\n"))
readuntil(o, "[")
corners = [Float32[] for i in 1:cols, j in 1:rows]
for i in 1:cols*rows, j in 1:2
if i == cols*rows && j == 2
break
end
push!(corners[i], parse(Float32, readuntil(o, ",")))
end
push!(corners[cols*rows], parse(Float32, readuntil(o, "]")))
timholy marked this conversation as resolved.
Show resolved Hide resolved

return reverse(permutedims(corners, (2, 1)); dims = 1), (rows, cols)
end
end

function get_list(file)
doc = read(file, LazyNode)
str = filter(≠('\"'), simple_value(doc[end][end]))
yakir12 marked this conversation as resolved.
Show resolved Hide resolved
list = Dict{String, String}()
for line in split(str, '\n')
img_file, data_file = split(line)
list[img_file] = data_file
end
return list
end

function calc_error(ps1, ps2)
s = 0.0
for (p1, p2) in zip(ps1, ps2)
s += LinearAlgebra.norm_sqr(p1 .- p2)
end
sqrt(s/length(ps1))
end

function test_one(img_file, data_file)
timholy marked this conversation as resolved.
Show resolved Hide resolved
corners, n_corners = parse_corners_file(data_file)
detected_corners = _detect_corners(img_file, n_corners)
calc_error(corners, detected_corners)
end

@testset "detecting corners" begin

path = joinpath(test_dir, "cameracalibration")
list = get_list(joinpath(path, "chessboard_list.dat"))

k, v = first(list)
test_one(joinpath(path, k), joinpath(path, v)) # why do we need this?

@testset "in $k" for (k, v) in list

img_file = joinpath(path, k)
data_file = joinpath(path, v)

@test test_one(img_file, data_file) < 1

end
end
Loading