Skip to content

Useful Code Scraps

Dehann Fourie edited this page Sep 11, 2021 · 17 revisions

Logging with @debug

Hi, just thought this is handy enough to share (if you don't already know about it) You can turn debug messages on for only certain modules or files using the JULIA_DEBUG ENV variable, for example:

ENV["JULIA_DEBUG"] = DistributedFactorGraphs
ENV["JULIA_DEBUG"] = :JunctionTree (edited) 

Debugging with CSM

https://github.com/JuliaRobotics/IncrementalInference.jl/issues/443#

Limit CSM steps

  • getSolverParams(fg).limititers=100
  • tree, smt, hist = solveTree!(fg, recordcliqs=ls(fg), limititercliqs=[(:x0=>2);])

Plot image in Gadfly

https://github.com/GiovineItalia/Gadfly.jl/issues/288#issuecomment-462754808

Images and Bytes

Converting a Julia image to PNG byte stream

using TestImages
img = testimage("mandrill")

using ImageMagick, FileIO
# convert image
io = IOBuffer()
pngSm = Stream(format"PNG", io)
FileIO.save(pngSm, img)
pngBytes = take!(io)

The png can be converted back to bitmap (array) format:

rgb = ImageMagick.readblob(pngBytes);

using ImageView
imshow(rgb)

Alternatively, ImageMagick/FileIO can work through files for lazy convert:

using FileIO
# Lazy convert to PNG 
save("/tmp/test.png", img)

# Lazy convert to bytes
pngBytes = open(read, "/tmp/test.png")
# alternative
# pngBytes = Vector{UInt8}(undef, stat(f).size ÷ sizeof(UInt8))
# fid = open("/tmp/something.png", "r")
# read!(fid, pngBytes)
# close(fid)

Make Gadfly Plot Movie Video

PL = [Gadfly.plot(y=randn(10), Geom.line) for i in 1:10];

using ImageMagick, RoMEPlotting
PLi = convert.(Matrix{RGB}, PL)

using Images, Caesar
writevideo("/tmp/test.avi", PLi, fps=5)

Gadfly plot to image

using Cairo
using Gadfly
using Images

pl = gadfly.plot(y=rand(10), Geom.path)

io = IOBuffer()
# https://github.com/GiovineItalia/Gadfly.jl/blob/423f170cc5fa4966f1dfc31640464e02c2e4e588/src/Gadfly.jl#L961-L962
draw(PNG(io), pl) 
# https://juliaimages.org/stable/function_reference/#FileIO.save
im = load(Stream{format"PNG"}(io))

# see the result
using ImageView
imshow(im)

Separately, https://juliaio.github.io/VideoIO.jl/stable/writing/