对于我的项目,我想使用python从大小为1024x720的图像中提取一个大小为224x224的补丁。给出了贴片中心在图像上的像素位置,并给出了贴片产生的角度。我知道如何使用数组切片从0(度)角度提取补丁,但我想要以一个角度对图像进行切片。如有任何帮助,我们将不胜感激。谢谢!

发布于 2018-04-18 19:57:00
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 patchhttps://stackoverflow.com/questions/49892205
复制相似问题