Skip to content

Commit

Permalink
add tests for chessboard corner detection
Browse files Browse the repository at this point in the history
  • Loading branch information
yakir12 committed Dec 24, 2024
1 parent 0bed41b commit 5753120
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
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
69 changes: 69 additions & 0 deletions test/test_corner_detection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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)))
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, "]")))

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]))
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

@testset "detecting corners" begin

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

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

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

corners, n_corners = parse_corners_file(data_file)
detected_corners = _detect_corners(img_file, n_corners)

ϵ = calc_error(corners, detected_corners)

@test ϵ < 1

end
end

0 comments on commit 5753120

Please sign in to comment.