# Image rotation

**URL:** <https://discourse.julialang.org/t/image-rotation/2637>\
**Category:** Visualization\
**Created:** [March 13, 2017, 1:18pm UTC](https://discourse.julialang.org/t/image-rotation/2637 "2017-03-13T13:18:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![yalexand](https://avatars.discourse-cdn.com/v4/letter/y/5daacb/32.png) [@yalexand](https://discourse.julialang.org/u/yalexand)\
**Post date:** [March 13, 2017, 1:18pm UTC](https://discourse.julialang.org/t/image-rotation/2637/1 "2017-03-13T13:18:52Z")

</div>

Hi All,

I can’t find an analog for Matlab “imrotate” function.  
Maybe I overlooked it, or really there isn’t such thing?

Here it is in Matlab, to save you clicking:

```julia
imrotate Rotate image.
    B = imrotate(A,ANGLE) rotates image A by ANGLE degrees in a
    counterclockwise direction around its center point. To rotate the image
    clockwise, specify a negative value for ANGLE. imrotate makes the output
    image B large enough to contain the entire rotated image. imrotate uses
    nearest neighbor interpolation, setting the values of pixels in B that
    are outside the rotated image to 0 (zero).
 
    B = imrotate(A,ANGLE,METHOD) rotates image A, using the interpolation
    method specified by METHOD. METHOD is a string that can have one of the
    following values. The default value is enclosed in braces ({}).
 
         {'nearest'} Nearest neighbor interpolation
 
         'bilinear' Bilinear interpolation
 
         'bicubic' Bicubic interpolation. Note: This interpolation
                      method can produce pixel values outside the original
                      range.
 
    B = imrotate(A,ANGLE,METHOD,BBOX) rotates image A, where BBOX specifies
    the size of the output image B. BBOX is a text string that can have
    either of the following values. The default value is enclosed in braces
    ({}).
 
         {'loose'} Make output image B large enough to contain the
                      entire rotated image. B is generally larger than A.
 
         'crop' Make output image B the same size as the input image
                      A, cropping the rotated image to fit.

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 13, 2017, 2:40pm UTC](https://discourse.julialang.org/t/image-rotation/2637/2 "2017-03-13T14:40:27Z")

</div>

Use `warp` from ImageTransformations. But I see it’s missing a docstring…I’ll fix that and publish a new version today.

---

<div class="post-metadata">

**Author:** ![yalexand](https://avatars.discourse-cdn.com/v4/letter/y/5daacb/32.png) [@yalexand](https://discourse.julialang.org/u/yalexand)\
**Post date:** [March 13, 2017, 3:02pm UTC](https://discourse.julialang.org/t/image-rotation/2637/3 "2017-03-13T15:02:54Z")

</div>

great thank you, I’ll try.

some unrelated stuff - translating from Matlab, I struggle with couple of syntactic trivia.  
First is thresholding, like here in Matlab:

```julia
>> x = [1 3 78 98 11];
>> y=x>60

y =

  1×5 logical array

   0 0 1 1 0

```

another one, how to initiate array by NaNs.  
Julia:

```julia
> NaN(10)
x MethodError: objects of type Float64 are not callable

```

certainly can write local functions, but that’s clumsy, if language allows, it would be great to know.  
sorry for nuisance.  
…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 13, 2017, 3:39pm UTC](https://discourse.julialang.org/t/image-rotation/2637/4 "2017-03-13T15:39:28Z")

</div>

You can use dot-operators like `.>` in Julia for your first question:

```julia
julia> x = [1 3 78 98 11]
1×5 Array{Int64,2}:
 1 3 78 98 11

julia> y = x .> 60
1×5 BitArray{2}:
 false false true true false

julia> x[y]
2-element Array{Int64,1}:
 78
 98

```

and `fill()` for your second question:

```julia
julia> fill(NaN, 10, 10)

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 13, 2017, 4:38pm UTC](https://discourse.julialang.org/t/image-rotation/2637/5 "2017-03-13T16:38:16Z")

</div>

You can read the new docstring [here](https://github.com/JuliaImages/ImageTransformations.jl/pull/9); not merged yet because, for something really robust, there’s work to do in Interpolations.jl, and I may not get to it instantly. But it should be good enough to get you started.

---

<div class="post-metadata">

**Author:** ![yalexand](https://avatars.discourse-cdn.com/v4/letter/y/5daacb/32.png) [@yalexand](https://discourse.julialang.org/u/yalexand)\
**Post date:** [March 13, 2017, 4:57pm UTC](https://discourse.julialang.org/t/image-rotation/2637/6 "2017-03-13T16:57:36Z")

</div>

thanks very much both!  
I will check “warp” and let know, if it performs as expected.  
…

---

<div class="post-metadata">

**Author:** ![yalexand](https://avatars.discourse-cdn.com/v4/letter/y/5daacb/32.png) [@yalexand](https://discourse.julialang.org/u/yalexand)\
**Post date:** [March 14, 2017, 4:01pm UTC](https://discourse.julialang.org/t/image-rotation/2637/7 "2017-03-14T16:01:18Z")

</div>

Great, the transform works, however i didn’t find how to extract correctly the central part in the resulting image. So I did it manually.

As I need only a rotation about center, I wrote the following helper to cut it shorter:

```julia
###################################
# rotate image "I" around its centre by "angle" [degrees]
# returns or "full" or "central" part of the rotated image
function imrotate(I,angle,spec::String="central")

  tform = recenter(RotMatrix(angle/180*pi), center(I))
  Irot = parent(warp(I,tform))

  if (spec=="full")
    Irot
  elseif (spec=="central")
    szX,szY = size(I)
    Dx = Int64(round(szX/2))
    Dy = Int64(round(szY/2))
    x0 = Int64(round(center(Irot)[1]))
    y0 = Int64(round(center(Irot)[2]))
    Ifin = Irot[x0-Dx+1:x0-Dx+szX,y0-Dy+1:y0-Dy+szY]
  else
    I
  end

end

```

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [March 14, 2017, 4:43pm UTC](https://discourse.julialang.org/t/image-rotation/2637/8 "2017-03-14T16:43:31Z")

</div>

Unsure if I understand your intend, by try if the following code does what you intend. Basically I assume you would like to rotate around the center of the original image and not expand it’s size in any direction

```julia
using ImageTransformations, CoordinateTransformations, TestImages

img = testimage("camera")
tfm = recenter(RotMatrix(π/4), center(img))
imr = warp(img, tfm)
img_new = imr[UnitRange.(indices(img))...]

```

---

<div class="post-metadata">

**Author:** ![yalexand](https://avatars.discourse-cdn.com/v4/letter/y/5daacb/32.png) [@yalexand](https://discourse.julialang.org/u/yalexand)\
**Post date:** [March 14, 2017, 5:02pm UTC](https://discourse.julialang.org/t/image-rotation/2637/9 "2017-03-14T17:02:59Z")

</div>

many thanks, this is exactly a thing I was looking for!  
…

---

<div class="post-metadata">

**Author:** ![Ashwani\_Rathee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashwani_rathee/32/49551_2.png) [@Ashwani\_Rathee](https://discourse.julialang.org/u/Ashwani_Rathee)\
**Post date:** [December 15, 2020, 9:03am UTC](https://discourse.julialang.org/t/image-rotation/2637/10 "2020-12-15T09:03:53Z")

</div>

Hey @Evizero,I was looking for something exactly like this…last line doesn’t seem to work due to  
UndefVarError: indices not defined  
How to correct that??

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [December 15, 2020, 10:17am UTC](https://discourse.julialang.org/t/image-rotation/2637/11 "2020-12-15T10:17:14Z")

</div>

If you’re trying out old code (prior to the release of Julia 1.0), download a copy of Julia 0.7 and try it there: it will give you helpful hints about what the new names are.

In this case, use `axes` instead of `indices`.
