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

docs: add nb for image tiling workflow #434

Merged
merged 10 commits into from
Oct 8, 2024
229 changes: 229 additions & 0 deletions notebooks/preprocessing-workflow/tiling.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
{
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"HOME = \"../..\" # path to the root of the project two levels up\n",
"\n",
"# Activate the environment\n",
"using Pkg\n",
"Pkg.activate(HOME)\n",
"Pkg.precompile()\n",
"\n",
"using IceFloeTracker: get_tiles, get_tile_dims, load\n",
"using ColorTypes: RGBA"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img = load(\"bering_strait/truecolor/20200504.aqua.truecolor.250m.tiff\")"
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Say we want to split the image into roughly 8x8 tiles\n",
"size(img) .÷ 8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tile size\n",
"Let us choose tiles of size 450x450. To do that use the `get_tiles` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's view the documentation for the function\n",
"@info @doc get_tiles"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tiles = get_tiles(img, 450)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Top left tile dimensions\n",
"get_tile_dims(tiles[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Bottom right tile dimensions\n",
"get_tile_dims(tiles[end])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the bottom right tile has been extended to cover the rest of the image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# View the first tile\n",
"img[tiles[1]...]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Further, note that the tiles on the right and bottom edges are slightly bigger."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### View the full tiling"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"function build_tiling_illustration(img, side_length)\n",
"\n",
" # Create new canvas to draw on\n",
" newimg = similar(img, RGBA{Float64})\n",
"\n",
" # Apply transparency to the tiles\n",
" for tile_coords in get_tiles(img, side_length)\n",
" tile = @view img[tile_coords...]\n",
" alpha = rand(0.5:0.05:1)\n",
" transparent_tile = map(c -> RGBA(c.r, c.g, c.b, alpha), tile)\n",
" newimg[tile_coords...] .= transparent_tile\n",
" end\n",
"\n",
" # View the image\n",
" newimg\n",
"end\n",
"\n",
"build_tiling_illustration(img, 450)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Optimal tile side length\n",
"\n",
"Perhaps there is a better way to split the image into tiles. We can use the `get_optimal_tile_size` function to determine a better tiling given an initial tile side length."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"using IceFloeTracker: get_optimal_tile_size\n",
"\n",
"@info @doc get_optimal_tile_size"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"best_side_length = get_optimal_tile_size(450, size(img))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As expected, a tile size of 450 is not optimal for this image. The function get_optimal_tile_size suggests a fitter tiling is possible using tiles of 451 pixels in side length for this image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"build_tiling_illustration(img, 451)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Area of corner tile with side length 451\n",
"get_tile_dims(get_tiles(img, 451)[end]) |> prod"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the area of the corner tile for the 450-tiling is larger."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"get_tile_dims(get_tiles(img, 450)[end]) |> prod"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.9.3",
"language": "julia",
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading