Rotate a image

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:

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> 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);

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

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.

1 Like

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

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

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 …

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.

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:

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. 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

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; thanks to your post here, this will be fixed in 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> ImageTransformations.autorange(img3, tfm)
(0:503, 21:11624, -54:0)

That’s a third of a gigabyte.

4 Likes

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.)

1 Like

Thanks for figuring that out, @GunnarFarneback. @zsz00, check out the Perspective transformations and see if that fixes your problem.