对于我的项目,我想使用python从特定角度的1024x720大小的图像中提取大小为224x224的补丁。给出了图像上贴片中心的像素位置,并给出了贴片所形成的角度。 我知道如何使用数组切片在0(度)角度提取补丁,但我想以一定角度切割图像。 任何有关相同的帮助将不胜感激。 谢谢!
答案 0 :(得分:1)
from scipy import ndimage
import numpy as np
import math as m
import cv2
def patchmaker(img,height,width,center_y,center_x,angle):
theta = angle/180*3.14
img_shape = np.shape(img)
print(img_shape)
x = [[i for i in range(0,img_shape[1])] for y in range(img_shape[0])]
y = [[j for i in range(img_shape[1])] for j in range(0,img_shape[0])]
x = np.asarray(x)
y = np.asarray(y)
rotatex = x[center_y-m.floor(height/2):center_y+m.floor(height/2),center_x-m.floor(width/2):center_x+m.floor(width/2)]
rotatey = y[center_y-m.floor(height/2):center_y+m.floor(height/2),center_x-m.floor(width/2):center_x+m.floor(width/2)]
coords = [rotatex.reshape((1,height*width))-center_x,rotatey.reshape((1,height*width))-center_y]
coords = np.asarray(coords)
coords = coords.reshape(2,height*width)
roatemat = [[m.cos(theta),m.sin(theta)],[-m.sin(theta),m.cos(theta)]]
rotatedcoords = np.matmul(roatemat,coords)
patch = ndimage.map_coordinates(img,[rotatedcoords[1]+center_y,rotatedcoords[0]+center_x], order=1, mode='nearest').reshape(height,width)
return patch