|
| 1 | +# --- |
| 2 | +# title: Cropping, Resizing and Rescaling |
| 3 | +# cover: assets/lighthouse.png |
| 4 | +# author: Ashwani Rathee |
| 5 | +# date: 2020-11-24 |
| 6 | +# --- |
| 7 | + |
| 8 | +# This demonstration shows how to use cropping,resizing and rescaling operations on an |
| 9 | +# image in Julia using ImageTransformations.jl |
| 10 | + |
| 11 | +using Images, ImageTransformations, TestImages, OffsetArrays |
| 12 | +## load an example image |
| 13 | +img_source = testimage("lighthouse") |
| 14 | + |
| 15 | +# ## Cropping Operation |
| 16 | + |
| 17 | +# Cropping is one of the most basic photo manipulation processes, and it is carried out to |
| 18 | +# remove an unwanted object or irrelevant noise from the periphery of a photograph, to |
| 19 | +# change its aspect ratio, or to improve the overall composition. |
| 20 | + |
| 21 | +# Let's first check the size of the image |
| 22 | + |
| 23 | +img_size = size(img_source) |
| 24 | + |
| 25 | +# Output is `(512,768)` which stands means `img_source` is `512` in height and `768` in width. |
| 26 | +# In Julia, images as multidimensional arrays are stored in column-major order, which means that this first index corresponds to the |
| 27 | +# vertical axis (column) and the second to the horizontal axis (row). |
| 28 | +# |
| 29 | +# !!! tip |
| 30 | +# 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. |
| 31 | + |
| 32 | +# Let's crop the image from sides by 1/8 of img_source each side and leave it as it is from |
| 33 | +# top to bottom. |
| 34 | + |
| 35 | +# Easiest way to do this is indexing: `img_source[y1:y2, x1:x2]` |
| 36 | + |
| 37 | +# Region of Interest: [y1, y2] sets the range for y-axis and [x1, x2] sets the range for |
| 38 | +# x-axis of source image. |
| 39 | + |
| 40 | +img_cropped = @view img_source[ :,floor(Int, 1/8*img_size[2]) : floor(Int, 7/8*img_size[2])] |
| 41 | + |
| 42 | +# Let's see the size of the cropped image: |
| 43 | + |
| 44 | +size(img_cropped) |
| 45 | + |
| 46 | +# We can also do size-preserved cropping of the image by replacing the contents of image to white pixels or transparent pixels using PaddedView: |
| 47 | + |
| 48 | +img_padded = PaddedView(ARGB(0, 0, 0, 0), OffsetArray(img_cropped, OffsetArrays.Origin(1, floor(Int, 1/8*img_size[2]))), axes(img_source)) |
| 49 | + |
| 50 | +# ## Resizing Operation |
| 51 | + |
| 52 | +# Resizing is a method to resize an image to a given specific output image shape. It is |
| 53 | +# different from rescaling as in rescaling we use a scaling factor to manipulate the image. |
| 54 | + |
| 55 | +img_square = imresize(img_source, (400, 400)); |
| 56 | +img_small = imresize(img_source, ratio=1/4); |
| 57 | +img_medium = imresize(img_small, size(img_small).*2); |
| 58 | +mosaicview(img_source, img_square, img_small, img_medium; nrow=1) |
| 59 | + |
| 60 | +# ## Rescaling |
| 61 | + |
| 62 | +# Rescale operation resizes an image by a given scaling factor. The scaling factor can |
| 63 | +# either be a single floating point value, or multiple values - one along each axis. |
| 64 | +# Image scaling is the process of changing the size of an image while preserving the |
| 65 | +# original aspect ratio. |
| 66 | + |
| 67 | +# ### Rescaling by percentage |
| 68 | + |
| 69 | +percentage_scale = 0.6 |
| 70 | +new_size = trunc.(Int, size(img_source) .* percentage_scale) |
| 71 | +img_rescaled = imresize(img_source, new_size); |
| 72 | +mosaicview(img_source, img_rescaled; nrow=1) |
| 73 | + |
| 74 | +# We calculated new size by estimating the size of frame by multiplying size by scale and |
| 75 | +# then truncated it to Int format. |
| 76 | + |
| 77 | +# ### Rescaling to a specific dimension |
| 78 | + |
| 79 | +new_width = 200 |
| 80 | +percentage_scale = new_width / size(img_source,2); |
| 81 | +new_size = trunc.(Int, size(img_source) .* percentage_scale); |
| 82 | +img_rescaled = imresize(img_source, new_size); |
| 83 | +mosaicview(img_source, img_rescaled; nrow=1) |
| 84 | + |
| 85 | +# We have updated our scale by percentage solution to calculate scale-percentage |
| 86 | +# dynamically based on a change in one of the dimensions. |
| 87 | +# Remember: `size(sourceimage) == (height, width) |
| 88 | + |
| 89 | +# ### Rescaling by two-fold using restrict function |
| 90 | + |
| 91 | +rescaled_both = restrict(img_source); # both side |
| 92 | +rescaled_height = restrict(img_source, 1); # height |
| 93 | +rescaled_width = restrict(img_source, 2); # width |
| 94 | +mosaicview(img_source, rescaled_both, rescaled_height, rescaled_width; nrow=1) |
0 commit comments