【Python-随机旋转】图像随机旋转及坐标进行旋转原理
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                【Python-随机旋转】图像随机旋转及坐标进行旋转原理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                在做人臉或物體檢測的任務中,往往需要對訓練集進行隨機旋轉,做數據增強,增加模型的魯棒性。
在進行圖像隨機旋轉的同時,相應的label值坐標也要進行相應的旋轉。
如人臉關鍵點檢測中人臉對應的關鍵點坐標,物體檢測任務中的物體所在box坐標。
(1).原理-------坐標旋轉變換公式
原理參照博客:
https://blog.csdn.net/u012686154/article/details/88854386
最終變換公式如下:
坐標(x,y)順時針旋轉 β 度,則經過矩陣相乘,轉換成新的坐標(x‘,y')
(2).實現-------python代碼實現
import math from PIL import Image#box為0~1的值,為[ymin, xmin, ymax, xmax] #landmarks為0~1的值,為[y0,x0,y1,x1,y2,x2......yn,xn] def random_rotation(image, box, landmarks, max_angle=90):#為隨機旋轉的角度-90~90度angle = np.random.uniform(-max_angle,max_angle)theta = angle*(math.pi/180.0)#獲取旋轉的中心坐標#也即是圖像的中心坐標image_height = image.shape[0]image_width = image.shape[1]scaler = np.stack([image_height, image_width], axis=0)center = np.reshape(0.5*scaler, [1, 2])#求旋轉矩陣rotation = np.stack([np.cos(theta), np.sin(theta),-np.sin(theta), np.cos(theta)], axis=0)rotation_matrix = np.reshape(rotation, [2, 2])#旋轉方框ymin, xmin, ymax, xmax = boxh, w = ymax - ymin, xmax - xminbox = np.stack([ymin, xmin, ymin, xmax,ymax, xmax, ymax, xmin], axis=0)box = np.matmul(np.reshape(box, [4, 2])*scaler - center, rotation_matrix) + centerbox = box/scalery ,x = box[:,0],box[:,1]ymin, ymax = np.min(y), np.max(y)xmin, xmax = np.min(x), np.max(x)box = np.stack([ymin, xmin, ymax, xmax], axis=0)#旋轉坐標landmarks = np.matmul(landmarks*scaler - center, rotation_matrix) + centerlandmarks = landmarks/scaler#旋轉圖像image = Image.fromarray(image)im_rotate = image.rotate(angle)im_rotate = np.array(im_rotate)return im_rotate, box, landmarks?
版權聲明:本文為qq_16564093原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/qq_16564093/article/details/106000209
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【Python-随机旋转】图像随机旋转及坐标进行旋转原理的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: LTE切换与TAU问题
- 下一篇: 【cudnn】cudnn 安装
