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

Demo-Spatial Transformations #167

Merged
merged 6 commits into from
Dec 5, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
suggestions incorporated,except one
15 minor changes
  • Loading branch information
ashwanirathee committed Nov 25, 2020
commit cb3ff3cc14a0c9bd0d3d78682f574fe30955907e
52 changes: 27 additions & 25 deletions docs/examples/spatial_transformation/SpatialTransformations.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# ---
# title: Cropping,Resizing and Rescaling
# title: Cropping, Resizing and Rescaling
# cover: assets/lighthouse.png
# author: Ashwani Rathee
# date: 2020-11-24
# ---

# This demonstration shows how to use cropping,resizing and rescaling operations on an
# image in julia using ImageTransformations.jl
# image in Julia using ImageTransformations.jl

using ImageCore, ImageTransformations, TestImages
## load img_source
using ImageCore, ImageShow, ImageTransformations, TestImages
## load an example image
img_source = testimage("lighthouse")

# ## Cropping Operation
Expand All @@ -20,71 +20,73 @@ img_source = testimage("lighthouse")

# Let's first check the size of the image

img_size=size(img_source)
img_size = size(img_source)

# Output is (512,768) which stands means img_source is 512 in height and 768 in width.
# In Julia,Images are vertical major ,which means that this first index corresponds to the
# vertical axis and the second to the horizontal axis.This might be different from other
# programming languages.
# Output is `(512,768)` which stands means `img_source` is `512` in height and `768` in width.
# In Julia, images as multidimensional arrays are stored in column-major order, which means that this first index corresponds to the
# vertical axis (column) and the second to the horizontal axis (row).
#
# !!! tip
# An related issue about the memory order is the indexing performance, see [Performance Tips](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-column-major) for more details.

# Let's crop the image from sides by 1/8 of img_source each side and leave it as it is from
# top to bottom.

# Easiest way to do this is indexing: img_source[y1:y2,x1:x2]
# Easiest way to do this is indexing: `img_source[y1:y2, x1:x2]`

# Region of Interest:(y1,y2) sets the range for y-axis and (x1,x2) sets the range for
# # Region of Interest: [y1, y2] sets the range for y-axis and [x1, x2] sets the range for
# x-axis of source image.

img_cropped = img_source[ : ,floor(Int, 1/8*img_size[2]) : floor(Int, 7/8*img_size[2])]
img_cropped = img_source[ :,floor(Int, 1/8*img_size[2]) : floor(Int, 7/8*img_size[2])]

# Let's see img_cropped size:
# Let's see the size of the cropped image:

size(img_cropped)

# Ther is another method to do this and that is through using PaddedView,which is shown [here](https://juliaimages.org/stable/democards/examples/spatial_transformation/alpha_compositing/#Alpha-Compositing/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I believe it is a "misuse" of PaddedViews.

Originally, PaddedViews is designed to automatically pad the borders with the given value.

julia> A = collect(reshape(1:4, 2, 2))
2×2 Array{Int64,2}:
 1  3
 2  4

julia> Ap = PaddedView(0, A, (0:3, 0:3))
4×4 PaddedView(0, ::Array{Int64,2}, (0:3, 0:3)) with eltype Int64 with indices 0:3×0:3:
 0  0  0  0
 0  1  3  0
 0  2  4  0
 0  0  0  0

I interpret it as a "misuse" because the output axes can be smaller than the original axes:

julia> Ap = PaddedView(0, A, (1:1, 1:1))
1×1 PaddedView(0, ::Array{Int64,2}, (1:1, 1:1)) with eltype Int64 with indices 1:1×1:1:
 1

This is allowed, because sometimes we want to crop along one dimension, while padding along the other dimension, e.g.,

julia> Ap = PaddedView(0, A, (1:1, 0:3))
1×4 PaddedView(0, ::Array{Int64,2}, (1:1, 0:3)) with eltype Int64 with indices 1:1×0:3:
 0  1  3  0

Quoted "misuse" because it totally depends on how users want from this.

Instead of referring to the "Alpha-Compositing" example, I guess we can add another example here for size-preserved cropping, i.e., filling the border with white pixels, or transparent pixels:

PaddedView(ARGB(0, 0, 0, 0), OffsetArray(img_cropped, OffsetArrays.Origin(1, floor(Int, 1/8*img_size[2]))), axes(img_source))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing I'd like to see here is the view version.

img_cropped = @view img_source[:, floor(Int, 1/8*img_size[2]) : floor(Int, 7/8*img_size[2])]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am playing around with padded view for image pyramids demo,I'll add a example of this here in a day or two.


# ## Resizing Operation

# Resizing is a method to resize an image to given specific outout image shape.It is different from rescaling as in rescaling we use scaling factor to manipulate image.
# Resizing is a method to resize an image to a given specific output image shape. It is
# different from rescaling as in rescaling we use a scaling factor to manipulate the image.

img_square = imresize(img_source,(400,400));
img_square = imresize(img_source, (400, 400));
img_small = imresize(img_source, ratio=1/4);
img_medium = imresize(img_small, size(img_small).*2);
mosaicview(img_source, img_square, img_small, img_medium;nrow=1)
mosaicview(img_source, img_square, img_small, img_medium; nrow=1)

# ## Rescaling

# Rescale operation resizes an image by a given scaling factor. The scaling factor can
# either be a single floating point value, or multiple values - one along each axis.
# Image scaling is the process of changing the size of an image and saving the original
# proportions.
# Image scaling is the process of changing the size of an image while preserving the
# original aspect ratio.

# ### Rescaling by percentage

percentage_scale=0.6
percentage_scale = 0.6
new_size = trunc.(Int, size(img_source) .* percentage_scale)
img_rescaled = imresize(img_source, new_size);
mosaicview(img_source, img_rescaled; nrow = 1)
mosaicview(img_source, img_rescaled; nrow=1)

# We calculated new size by estimating the size of frame by multiplying size by scale and
# then truncated it to Int format.

# ### Rescaling to a specific dimension

new_width = 200
percentage_scale = new_width / size(img_source)[2];
percentage_scale = new_width / size(img_source,2);
new_size = trunc.(Int, size(img_source) .* percentage_scale);
img_rescaled = imresize(img_source, new_size);
mosaicview(img_source, img_rescaled; nrow = 1)
mosaicview(img_source, img_rescaled; nrow=1)

# We have updated our scale by percentage solution to calculate scale-percentage
# dynamically based on a change in one of the dimensions.
# Remember: size(sourceimage)[1] corresponds to height, while size(sourceimage)[2]
# corresponds to width.
# Remember: `size(sourceimage) == (height, width)

# ### Rescaling by two-fold using restrict function

rescaled_both = restrict(img_source); # both side
rescaled_height = restrict(img_source, 1); # height
rescaled_width = restrict(img_source, 2); # width
mosaicview(img_source, rescaled_both, rescaled_height, rescaled_width; nrow = 1)
mosaicview(img_source, rescaled_both, rescaled_height, rescaled_width; nrow=1)