# Rotate a image

**URL:** <https://discourse.julialang.org/t/rotate-a-image/17586>\
**Category:** Specific Domains\
**Tags:** question\
**Created:** [November 16, 2018, 3:52am UTC](https://discourse.julialang.org/t/rotate-a-image/17586 "2018-11-16T03:52:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zsz00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsz00/32/6314_2.png) [@zsz00](https://discourse.julialang.org/u/zsz00)\
**Post date:** [November 16, 2018, 3:52am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/1 "2018-11-16T03:52:12Z")

</div>

I want to rotate a image with camara extri parameters matrix. but have error, can you help me amend it ? please!!!

using Images, ImageFeatures, FileIO, ImageView  
using CoordinateTransformations, StaticArrays, ImageTransformations, LinearAlgebra;

img2 = load(“…/test/all\_3/2.jpg”);

rot\_1 = [0.999788 0.003629 -0.020254;  
-0.003697 0.999988 -0.003359;  
0.020242 0.003433 0.999789 ]  
tran = [23.244938 -0.077094 0.316536]

rot\_1 = RotMatrix{3, Float64}(rot)

H = LinearMap(rot\_1)

img2\_1 = ImageTransformations.warp(img2, H) # error

DimensionMismatch(“Tried to multiply arrays of size (3, 3) and (2,)”)

Stacktrace:  
[1] #s177#398 at C:\Users\m1.julia\packages\StaticArrays\WmJnA\src\matrix\_multiply.jl:44 [inlined]  
[2] #s177#398(::Any, ::Any, ::Any, ::Any, ::Any, ::Type, ::Any, ::Any, ::Any) at .\none:0  
[3] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at .\boot.jl:506  
[4] \* at C:\Users\m1.julia\packages\StaticArrays\WmJnA\src\matrix\_multiply.jl:8 [inlined]  
[5] LinearMap at C:\Users\m1.julia\packages\CoordinateTransformations\IrN9A\src\affine.jl:50 [inlined]  
[6] autorange(::CartesianIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}, ::LinearMap{RotMatrix{3,Float64,9}}) at C:\Users\m1.julia\packages\ImageTransformations\2CYgE\src\autorange.jl:7  
[7] warp(::Interpolations.FilledExtrapolation{RGB{Normed{UInt8,8}},2,Interpolations.BSplineInterpolation{RGB{Normed{UInt8,8}},2,Array{RGB{Normed{UInt8,8}},2},Interpolations.BSpline{Interpolations.Linear},Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}},Interpolations.BSpline{Interpolations.Linear},RGB{Normed{UInt8,8}}}, ::LinearMap{RotMatrix{3,Float64,9}}) at C:\Users\m1.julia\packages\ImageTransformations\2CYgE\src\autorange.jl:3  
[8] warp(::Array{RGB{Normed{UInt8,8}},2}, ::LinearMap{RotMatrix{3,Float64,9}}) at C:\Users\m1.julia\packages\ImageTransformations\2CYgE\src\warp.jl:101  
[9] top-level scope at In[264]:2

help me, please …  
image is this:

 ![1](https://global.discourse-cdn.com/julialang/original/3X/a/6/a62f53054b48bbfd924cbaba51396eeabe6d965f.jpeg)

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 16, 2018, 4:07am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/2 "2018-11-16T04:07:13Z")

</div>

What is the error (copy it please), so others may be of more help. What does “image in accessory” mean?

This is from comments in the ImageTransformations.jl source. Have you tried rotating this way?

```julia
julia> using Images, CoordinateTransformations, TestImages, OffsetArrays
julia> img = testimage("lighthouse"); # use your image here
julia> axes(img)
(Base.OneTo(512),Base.OneTo(768))
# Rotate around the center of `img`
julia> tfm = recenter(RotMatrix(-pi/4), center(img))
AffineMap([0.707107 0.707107; -0.707107 0.707107], [-196.755,293.99])
julia> imgw = warp(img, tfm);

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 16, 2018, 4:45am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/3 "2018-11-16T04:45:19Z")

</div>

use full paths (your use of three dots may be a problem)  
I stored a copy of your image locally as “test.jpg”

```julia
using Images, ImageTransformations, ImageMagick, 
          CoordinateTransformations, OffsetArrays, FileIO

imagefile = "/home/usr/Pictures/test.jpg" # use your full path here
img = load(imagefile);

tfm = recenter(RotMatrix(-pi/2), center(img))
rotatetest = warp(img, tfm);

save("/home/usr/Pictures/rotatetest.jpg", rotatetest) # use your full path here

```

The above worked for me.

---

<div class="post-metadata">

**Author:** ![zsz00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsz00/32/6314_2.png) [@zsz00](https://discourse.julialang.org/u/zsz00)\
**Post date:** [November 16, 2018, 6:01am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/5 "2018-11-16T06:01:48Z")

</div>

The above worked, i kown .  
tfm = recenter(RotMatrix(-pi/2), center(img)) , i want to use a user-defined RotMatrix rot , not pi\*x .

---

<div class="post-metadata">

**Author:** ![zsz00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsz00/32/6314_2.png) [@zsz00](https://discourse.julialang.org/u/zsz00)\
**Post date:** [November 16, 2018, 6:01am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/6 "2018-11-16T06:01:50Z")

</div>

> [@JeffreySarnoff](#):
>
> tfm = recenter(RotMatrix(-pi/2), center(img))

i want use a user-defined RotMatrix, such as rot . i dont kown how to use a 3\*3 matix .

---

<div class="post-metadata">

**Author:** ![zsz00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsz00/32/6314_2.png) [@zsz00](https://discourse.julialang.org/u/zsz00)\
**Post date:** [November 16, 2018, 6:01am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/7 "2018-11-16T06:01:52Z")

</div>

tfm = recenter(RotMatrix(-pi/2), center(img))

i want to use a user-defined RotMatrix such as rot .  
i dont kown how to use a 3\*3 RotMatrix …

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [November 16, 2018, 6:02am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/8 "2018-11-16T06:02:26Z")

</div>

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 16, 2018, 10:13am UTC](https://discourse.julialang.org/t/rotate-a-image/17586/9 "2018-11-16T10:13:37Z")

</div>

Your 3x3 matrix `rot` is not a Rotation Matrix, it is matrix that is 3x3. A 3x3 Rotation Matrix is appropriate for use when rotating something in a 3 dimensional coordinate system. When working with images [flat pictures], a rotation is a transformation that rotates the image within the plane that the image occupies. Depending upon whether you are rotating something about its own center, or rotating about some other point in the plane, different matrix constructions are used. It is not possible to take any numbers you like and then call that a rotation matrix and use it to perform a rotation. Rotation of an image about its own center uses a 2x2 rotation matrix. Working with `https://github.com/FugroRoames/Rotations.jl` (`pkg> add Rotations`) will simplify this.

---

<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:** [November 16, 2018, 1:30pm UTC](https://discourse.julialang.org/t/rotate-a-image/17586/10 "2018-11-16T13:30:03Z")

</div>

One way you can tell your matrix isn’t a rotation is that entry with value `23.244938`; for any rotation matrix, all the entries will have absolute value \<= 1, and both rows and columns will have a sum-of-squares equal to 1. In particular, if `U` is a rotation matrix then `U'*U == U*U' == I` where `I` is the identity. (The converse isn’t true; there are matrices that satisfy this which are not rotations.) So, that matrix is not even close to being a rotation matrix.

It _is_ a linear transformation which can still be used to warp an image. However, as @JeffreySarnoff points out, you’ve got an issue with the dimensionality. If you really intended to do a 3d transformation on a 2d image, you need to first add a 3rd dimension to the image:

```julia
img3 = reshape(img, size(img)..., 1);

```

Now when you perform the transformation you’ll get a “slice” that lives in a 3d space, kind of like the single slices [in this image](https://i.stack.imgur.com/YemhD.png). You probably want to use `warp(img, tfm, Constant())` (where `Constant` is defined in Interpolations) since that last dimension is of size 1.

Second, when supplying

```julia
tfm = LinearMap([0.999788 0.003629 -0.020254; 23.244938 -0.003697 0.999988 ; -0.003359 -0.077094 0.020242])

```

currently it’s necessary to convert that to a [StaticArray](https://github.com/JuliaArrays/StaticArrays.jl); thanks to your post here, this will be fixed in [https://github.com/JuliaImages/ImageTransformations.jl/pull/63](https://github.com/JuliaImages/ImageTransformations.jl/pull/63).

Third, I really don’t think you mean to be doing this. Check out the boundaries of the array that will be created by this transformation:

```julia
julia> ImageTransformations.autorange(img3, tfm)
(0:503, 21:11624, -54:0)

```

That’s a third of a gigabyte.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 16, 2018, 3:17pm UTC](https://discourse.julialang.org/t/rotate-a-image/17586/11 "2018-11-16T15:17:56Z")

</div>

> [@zsz00](#):
>
> I want to rotate a image with camara extri parameters matrix. but have error, can you help me amend it ? please!!!

I would recommend being more precise with your question and/or giving more background when asking in a forum that’s not specialized in your field. Specifically “camara extri” is likely incomprehensible for people who do other kinds of image analysis than computer vision.

So what this is probably about is “camera extrinsic parameters”, which relate to the 3D geometry of a camera, and I assume the problem is to find out what happens to a 2D image taken by the camera when the camera is rotated (in 3D) and possibly translated.

(No, I can’t help solve the problem because it would take me too much time to refresh this stuff but at least I think I know enough to help reduce the confusion I’m sensing in this thread. Besides I suspect there’s insufficient information available and the people pointing out that `rot` is not a rotation matrix are entirely correct about 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:** [November 16, 2018, 4:03pm UTC](https://discourse.julialang.org/t/rotate-a-image/17586/12 "2018-11-16T16:03:05Z")

</div>

Thanks for figuring that out, @GunnarFarneback. @zsz00, check out the [Perspective transformations](https://github.com/FugroRoames/CoordinateTransformations.jl#perspective-transformations) and see if that fixes your problem.
