Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:03:17+00:00 2026-05-29T09:03:17+00:00

I’m trying to get some code that will perform a perspective transformation (in this

  • 0

I’m trying to get some code that will perform a perspective transformation (in this case a 3d rotation) on an image.

import os.path
import numpy as np
import cv

def rotation(angle, axis):
    return np.eye(3) + np.sin(angle) * skew(axis) \
               + (1 - np.cos(angle)) * skew(axis).dot(skew(axis))

def skew(vec):
    return np.array([[0, -vec[2], vec[1]],
                     [vec[2], 0, -vec[0]],
                     [-vec[1], vec[0], 0]])

def rotate_image(imgname_in, angle, axis, imgname_out=None):
    if imgname_out is None:
        base, ext = os.path.splitext(imgname_in)
        imgname_out = base + '-out' + ext
    img_in = cv.LoadImage(imgname_in)
    img_size = cv.GetSize(img_in)
    img_out = cv.CreateImage(img_size, img_in.depth, img_in.nChannels)
    transform = rotation(angle, axis)
    cv.WarpPerspective(img_in, img_out, cv.fromarray(transform))
    cv.SaveImage(imgname_out, img_out)

When I rotate about the z-axis, everything works as expected, but rotating around the x or y axis seems completely off. I need to rotate by angles as small as pi/200 before I start getting results that seem at all reasonable. Any idea what could be wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:03:18+00:00Added an answer on May 29, 2026 at 9:03 am

    First, build the rotation matrix, of the form

        [cos(theta)  -sin(theta)  0]
    R = [sin(theta)   cos(theta)  0]
        [0            0           1]
    

    Applying this coordinate transform gives you a rotation around the origin.

    If, instead, you want to rotate around the image center, you have to first shift the image center
    to the origin, then apply the rotation, and then shift everything back. You can do so using a
    translation matrix:

        [1  0  -image_width/2]
    T = [0  1  -image_height/2]
        [0  0   1]
    

    The transformation matrix for translation, rotation, and inverse translation then becomes:

    H = inv(T) * R * T
    

    I’ll have to think a bit about how to relate the skew matrix to the 3D transformation. I expect the easiest route is to set up a 4D transformation matrix, and then to project that back to 2D homogeneous coordinates. But for now, the general form of the skew matrix:

        [x_scale 0       0]
    S = [0       y_scale 0]
        [x_skew  y_skew  1]
    

    The x_skew and y_skew values are typically tiny (1e-3 or less).

    Here’s the code:

    from skimage import data, transform
    import numpy as np
    import matplotlib.pyplot as plt
    
    img = data.camera()
    
    theta = np.deg2rad(10)
    tx = 0
    ty = 0
    
    S, C = np.sin(theta), np.cos(theta)
    
    # Rotation matrix, angle theta, translation tx, ty
    H = np.array([[C, -S, tx],
                  [S,  C, ty],
                  [0,  0, 1]])
    
    # Translation matrix to shift the image center to the origin
    r, c = img.shape
    T = np.array([[1, 0, -c / 2.],
                  [0, 1, -r / 2.],
                  [0, 0, 1]])
    
    # Skew, for perspective
    S = np.array([[1, 0, 0],
                  [0, 1.3, 0],
                  [0, 1e-3, 1]])
    
    img_rot = transform.homography(img, H)
    img_rot_center_skew = transform.homography(img, S.dot(np.linalg.inv(T).dot(H).dot(T)))
    
    f, (ax0, ax1, ax2) = plt.subplots(1, 3)
    ax0.imshow(img, cmap=plt.cm.gray, interpolation='nearest')
    ax1.imshow(img_rot, cmap=plt.cm.gray, interpolation='nearest')
    ax2.imshow(img_rot_center_skew, cmap=plt.cm.gray, interpolation='nearest')
    plt.show()
    

    And the output:

    Rotations of cameraman around origin and center+skew

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.