首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用python从图像中提取一定角度和给定中心的一定大小的补丁

用python从图像中提取一定角度和给定中心的一定大小的补丁
EN

Stack Overflow用户
提问于 2018-04-18 05:58:24
回答 1查看 524关注 0票数 3

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-18 19:57:00

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49892205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档