使用 python 从图像中提取与给定中心成特定角度的特定大小的补丁
extracting patch of a certain size at certain angle with given center from an image using python
对于我的项目,我想使用 python 从尺寸为 1024x720 的图像中以特定角度提取尺寸为 224x224 的补丁。图像上补丁中心的像素位置已给出,补丁所成的角度也已给出。
我知道如何使用阵列切片以 0(度)角提取补丁,但我想以一定角度切片图像。
任何有关相同的帮助将不胜感激。
谢谢!
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
对于我的项目,我想使用 python 从尺寸为 1024x720 的图像中以特定角度提取尺寸为 224x224 的补丁。图像上补丁中心的像素位置已给出,补丁所成的角度也已给出。 我知道如何使用阵列切片以 0(度)角提取补丁,但我想以一定角度切片图像。 任何有关相同的帮助将不胜感激。 谢谢!
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