-
Notifications
You must be signed in to change notification settings - Fork 51
new demo: block-by-block visualization as GIF #191
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
Open
johnnychen94
wants to merge
2
commits into
source
Choose a base branch
from
jc/block_visualize
base: source
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# --- | ||
# title: Block-by-Block Visualization | ||
# description: This demo shows you how to create a block-by-block image visualization. | ||
# cover: assets/block_visualization.gif | ||
# author: Johnny Chen | ||
# date: 2021-05-05 | ||
# --- | ||
|
||
using Images | ||
using PaddedViews, OffsetArrays | ||
using TestImages | ||
using ImageBinarization | ||
|
||
# !!! tip | ||
# It is recommended to read the demo [Layer Compositing](@ref Layer-Compositing) first to get an idea | ||
# of how [OffsetArrays.jl](https://github.com/JuliaArrays/OffsetArrays.jl) and | ||
# [PaddedViews.jl](https://github.com/JuliaArrays/PaddedViews.jl) can be used to simplify the | ||
# canvas/axes organization. | ||
|
||
img = imresize(testimage("lighthouse"), ratio=0.5) | ||
img₀₁ = binarize(img, AdaptiveThreshold()) | ||
|
||
# As you may have seen in other demos, the simplest way to compare multiple images is to use `mosaic`: | ||
|
||
mosaic(img, img₀₁; nrow=1) | ||
|
||
# In this demo, we will use `Images.gif` to create something different by adding a "time" axis. As | ||
# an example, we use `TiledIteration` to move our focus on a block/patch/tile level. | ||
|
||
using TiledIteration | ||
tile_indices = TileIterator(axes(img), (32, 32))[:] | ||
tiles = [img[R...] for R in tile_indices] | ||
## visualize the tiles at fps rate 5, i.e., 5 frames per second. | ||
Images.gif(tiles; fps=5) | ||
|
||
# This is not very meaningful as it simply stacks all tiles together, so next we'll add | ||
# back the axes information for each block. We also use the original image as the | ||
# background. | ||
|
||
function create_frames(img, background, tile_indices; rf=0.8, rb=1-rf) | ||
map(tile_indices) do R | ||
## add back axes information to given blcok | ||
block = OffsetArray(img[R...], OffsetArrays.Origin(first.(R))) | ||
canvas, block = paddedviews(zero(eltype(background)), background, block) | ||
frame = clamp01!(@. rb*canvas .+ rf*block) | ||
end | ||
Comment on lines
+41
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are extra memory allocations here so it's not very efficient. The main reason I didn't optimize it is that the current |
||
end | ||
|
||
frames = create_frames(img, img, tile_indices) | ||
Images.gif(frames; fps=5) | ||
|
||
# Now, instead of using `mosaic`, we use this to compare our images. | ||
|
||
frames = create_frames(img₀₁, img, tile_indices; rb=0.4) | ||
Images.gif(frames; fps=5) | ||
|
||
# This may not be the best way to visually compare these two images, but you get the | ||
# idea of how to add back axes information to each block, and visualize it as an animated | ||
# GIF. | ||
|
||
# make cover image #src | ||
tile_indices = TileIterator(axes(img), (48, 48))[:] #src | ||
frames = create_frames(img₀₁, img, tile_indices; rb=0.4) #src | ||
save("assets/block_visualization.gif", Images.gif(frames); fps=2) #src |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.