Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yakir12 committed Dec 20, 2024
1 parent 14e302a commit 183fc1a
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 104 deletions.
5 changes: 2 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ CoordinateTransformations = "0.6"
FileIO = "1"
ImageBase = "0.1.7"
ImageDraw = "0.2"
ImageFiltering = "0.7.9"
ImageIO = "0.6"
ImageTransformations = "0.9, 0.10"
JSON3 = "1.14.1"
Expand All @@ -47,11 +46,11 @@ julia = "1.10"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
ImageFiltering = "6a3955dd-da59-5b1f-98d4-e7296123deb5"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PaddedViews = "5432bcbf-9aad-5242-b902-cca2824c8663"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "ColorTypes", "FileIO", "ImageFiltering", "LinearAlgebra", "PaddedViews", "StaticArrays", "Test"]
test = ["Aqua", "ColorTypes", "FileIO", "JSON3", "LinearAlgebra", "PaddedViews", "StaticArrays", "Test"]
1 change: 1 addition & 0 deletions src/detect_fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function _detect_corners(file, n_corners, sz)
OpenCV.CALIB_CB_ACCURACY # Up sample input image to improve sub-pixel accuracy due to aliasing effects.
)
!ret && return missing
# corners = reshape(RowCol.(eachslice(cv_corners, dims = 3)), n_corners)
ref_corners = OpenCV.cornerSubPix(gry, cv_corners, OpenCV.Size{Int32}(11,11), OpenCV.Size{Int32}(-1,-1), CRITERIA)
corners = reshape(RowCol.(eachslice(ref_corners, dims = 3)), n_corners)
return (file, corners)
Expand Down
1 change: 1 addition & 0 deletions test/example/corners.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"2.png":[[99,119],[143,121],[186,122],[229,124],[270,126],[99,163],[143,165],[186,166],[228,168],[269,169],[99,207],[143,208],[185,209],[227,210],[268,211],[99,250],[143,251],[185,251],[226,252],[267,252],[98,293],[142,293],[184,293],[225,293],[266,293],[98,336],[141,335],[183,334],[224,334],[265,334],[97,378],[140,377],[183,376],[224,376],[264,375],[97,420],[140,419],[182,418],[223,416],[263,415]],"6.png":[[112,177],[136,179],[159,180],[181,181],[202,182],[112,201],[136,202],[159,203],[181,204],[203,205],[113,225],[137,226],[160,226],[182,227],[203,227],[113,249],[137,249],[160,249],[182,249],[203,249],[113,273],[137,273],[160,272],[182,272],[204,271],[114,297],[138,296],[161,295],[183,294],[204,293],[114,322],[138,320],[161,319],[183,317],[205,316],[114,346],[138,344],[161,342],[184,340],[205,339]],"3.png":[[85,161],[119,163],[152,164],[183,166],[213,167],[86,179],[122,181],[157,182],[189,183],[221,184],[87,200],[126,201],[162,202],[197,203],[230,203],[88,223],[129,224],[168,224],[205,224],[240,224],[89,250],[134,250],[175,250],[214,249],[251,249],[91,282],[139,280],[183,279],[225,278],[265,276],[92,320],[144,316],[192,314],[237,311],[280,309],[93,365],[151,360],[203,355],[253,351],[299,348]],"1.png":[[86,197],[141,195],[194,194],[246,192],[298,190],[93,243],[144,241],[193,239],[241,236],[290,234],[99,283],[146,280],[192,277],[238,275],[283,273],[104,317],[148,314],[192,311],[234,309],[277,307],[108,348],[150,345],[191,342],[231,340],[271,338],[112,376],[152,373],[191,370],[229,367],[267,365],[115,400],[153,397],[190,394],[226,392],[262,389],[119,422],[154,419],[190,416],[224,414],[258,411]],"5.png":[[61,109],[93,101],[128,92],[167,83],[212,72],[63,151],[95,146],[130,140],[169,133],[213,126],[66,193],[97,190],[133,187],[171,184],[215,179],[68,235],[100,234],[135,233],[173,232],[216,231],[71,275],[102,276],[137,278],[175,279],[218,281],[73,315],[104,318],[139,322],[177,326],[220,330],[75,355],[106,360],[141,366],[179,372],[221,380],[78,394],[108,401],[143,409],[181,418],[223,428]],"4.png":[[106,90],[141,102],[171,113],[198,122],[221,130],[106,135],[142,144],[172,152],[198,159],[221,165],[107,181],[143,187],[173,192],[199,196],[222,200],[108,227],[144,229],[174,231],[200,232],[222,234],[109,272],[144,271],[174,270],[200,269],[223,268],[109,318],[145,313],[175,309],[201,305],[224,302],[109,365],[145,356],[175,348],[202,342],[225,336],[110,411],[146,399],[176,389],[202,379],[225,372]]}
57 changes: 38 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CameraCalibrations
using Test
using Aqua
using FileIO, StaticArrays, PaddedViews, ColorTypes, ImageFiltering, LinearAlgebra
using FileIO, StaticArrays, PaddedViews, ColorTypes, LinearAlgebra, JSON3

function index2bw(ij::CartesianIndex)
i, j = Tuple(ij)
Expand All @@ -12,38 +12,57 @@ function generate_checkerboard(n_corners, n)
xys = [n .* SVector{2, Float32}(Tuple(ij)) - SVector(0.5, 0.5) for ij in CartesianIndices(StepRange.(n_corners .+ 1, -1, 2))]
img = index2bw.(CartesianIndices(n_corners .+ 1))
imgl = kron(PaddedView(true, img, UnitRange.(0, n_corners .+ 2)), ones(Int, n, n))
imgw = imfilter(imgl, Kernel.gaussian(2))
return xys, imgw
# imgw = imfilter(imgl, Kernel.gaussian(2))
return xys, imgl
end

function compare(n_corners, ratio)
xys, img = generate_checkerboard(n_corners, ratio)
sz = size(img)
mktempdir() do path
file = joinpath(path, "img.png")
FileIO.save(file, Gray.(img))
res = CameraCalibrations._detect_corners(file, n_corners, sz)
return !ismissing(res) && all(<(1), norm.(xys .- res[2]))
end
end


@testset "CameraCalibrations.jl" begin
@testset "Code quality (Aqua.jl)" begin
Aqua.test_all(CameraCalibrations; piracies = false)
end

@testset "detect corners" begin
for _ in 1:100
n_corners = (rand(5:15), rand(5:15))
if isequal(isodd.(n_corners)...)
n_corners = (n_corners[1] + 1, n_corners[2])
@testset "Detect corners" begin
@testset "In artificial images" begin
for w in 13:15, h in 13:15, ratio in 95:100
# for w in 5:15, h in 5:15, ratio in 50:100
if isodd(w) isodd(h)
n_corners = (w, h)
@test compare(n_corners, ratio)
end
end
ratio = rand(50:100)
xys, img = generate_checkerboard(n_corners, ratio)
sz = size(img)
xys2 = mktempdir() do path
file = joinpath(path, "img.png")
FileIO.save(file, Gray.(img))
_, xys2 = CameraCalibrations._detect_corners(file, n_corners, sz)
return xys2
end

@testset "In real images" begin
dir = joinpath(@__DIR__(), "example")
files = filter(file -> last(splitext(file)) == ".png", readdir(dir, join = true))
sz = size(FileIO.load(first(files)))
n = length(files)
n_corners = (5, 8)
target = JSON3.read(joinpath(dir, "corners.json"))
for file in files
res = CameraCalibrations._detect_corners(file, n_corners, sz)
target_corners = target[basename(file)]
@test !ismissing(res) && all(<(1), norm.(target_corners .- vec(res[2])))
end
@test all(<(1), norm.(xys .- xys2))
end
end

@testset "Real data" begin
@testset "Full calibration" begin
n_corners = (5, 8)
dir = joinpath(@__DIR__(), "example")
files = readdir(dir; join=true)
files = filter(file -> last(splitext(file)) == ".png", readdir(dir, join = true))
checker_size = 1
c, (n, ϵ...) = fit(files, n_corners, checker_size)

Expand Down
Loading

0 comments on commit 183fc1a

Please sign in to comment.