Getting a incorrect picture output when running my self-made rotation algorithm

ぃ、小莉子 提交于 2019-12-04 17:36:26

Though I didn't dive in the math part of the domain, but based on the given information I think the matrix rotating formula should work like this:

UPDATE:

As I promised I dived a bit into the domain and got to the solution you can see as follows. The main trick that I've swapped the source and destination indices in the looping too, so the rounding doesn't mean any problem ever:

import cv2
import math
import numpy as np


# Load & Show original image
img = cv2.imread('/home/george/Downloads/lena.png', 0)
cv2.imshow('Original', img)


# Variable declarations
h = img.shape[0]  # Also known as rows
w = img.shape[1]  # Also known as columns

p = 120
h += 2 * p 
w += 2 * p

cX = h / 2 #Image Center X
cY = h / 2 #Image Center Y

theta = math.radians(45) #Change to adjust rotation angle

imgArray = np.zeros_like((img))  

#Add padding in an attempt to prevent image cropping
imgArray = np.pad(imgArray, pad_width=p, mode='constant', constant_values=0)   
img = np.pad(img, pad_width=p, mode='constant', constant_values=0)   

# loop pixel by pixel in image
for TX in range(h + 1):
    for TY in range(w + 1):
        try:
            x = int( +(TX - cX) * math.cos(theta) + (TY - cY) * math.sin(theta) + cX) #Rotation formula 
            y = int( -(TX - cX) * math.sin(theta) + (TY - cY) * math.cos(theta) + cY) #Rotation formula

            imgArray[TX, TY] = img[x, y]

        except IndexError as error:
           pass
#           print(error)

cv2.imshow('Rotated', imgArray)
cv2.waitKey(0)
exit()

Note: See usr2564301 comment too, if you want to dive deeper in the domain.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!