问题描述
下面是我正在创建的函数,该函数使用旋转矩阵公式将图像旋转到指定程度,而没有预先构建的旋转功能。
它显示一个变换后的图像,但是该图像为蓝色。我该如何解决?
关于如何在不使用预建功能的情况下也倾斜图像的任何提示都会有所帮助。
import numpy as np
import cv2
from PIL import Image,ImageDraw
from math import sin,cos
def rotate_skew(img,theta_1):
# Get dimensions of the input image.
(rows,cols) = img.shape[:-1]
# copy the rotation angle and convert to radians
theta_1 = np.radians(theta_1)
R = np.array(((np.cos(theta_1),-np.sin(theta_1)),(np.sin(theta_1),np.cos(theta_1))))
# convert array to PIL image object so draw would work
im_pil = Image.fromarray(img)
input_pixels = im_pil.load()
img_new = Image.new("RGB",(rows,cols))
draw = ImageDraw.Draw(img_new)
center_x = rows / 2
center_y = cols / 2
# Copy pixels
for x in range(rows):
for y in range(cols):
# Compute coordinate in input image
xp = int((x - center_x) * cos(theta_1) - (y - center_y) * sin(theta_1) + center_x)
yp = int((x - center_x) * sin(theta_1) + (y - center_y) * cos(theta_1) + center_y)
if 0 <= xp < rows and 0 <= yp < cols:
draw.point((x,y),input_pixels[xp,yp])
img_new.show()
return
# Read image
img = cv2.imread('face-1.jpg')
cv2.imshow('image',img)
cv2.waitKey(0)
rotate_skew(img,-90)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)