【Python-随机旋转】图像随机旋转及坐标进行旋转原理
在做人臉或物體檢測(cè)的任務(wù)中,往往需要對(duì)訓(xùn)練集進(jìn)行隨機(jī)旋轉(zhuǎn),做數(shù)據(jù)增強(qiáng),增加模型的魯棒性。
在進(jìn)行圖像隨機(jī)旋轉(zhuǎn)的同時(shí),相應(yīng)的label值坐標(biāo)也要進(jìn)行相應(yīng)的旋轉(zhuǎn)。
如人臉關(guān)鍵點(diǎn)檢測(cè)中人臉對(duì)應(yīng)的關(guān)鍵點(diǎn)坐標(biāo),物體檢測(cè)任務(wù)中的物體所在box坐標(biāo)。
(1).原理-------坐標(biāo)旋轉(zhuǎn)變換公式
原理參照博客:
https://blog.csdn.net/u012686154/article/details/88854386
最終變換公式如下:
坐標(biāo)(x,y)順時(shí)針旋轉(zhuǎn) β 度,則經(jīng)過(guò)矩陣相乘,轉(zhuǎn)換成新的坐標(biāo)(x‘,y')
(2).實(shí)現(xiàn)-------python代碼實(shí)現(xiàn)
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):#為隨機(jī)旋轉(zhuǎn)的角度-90~90度angle = np.random.uniform(-max_angle,max_angle)theta = angle*(math.pi/180.0)#獲取旋轉(zhuǎn)的中心坐標(biāo)#也即是圖像的中心坐標(biāo)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])#求旋轉(zhuǎn)矩陣rotation = np.stack([np.cos(theta), np.sin(theta),-np.sin(theta), np.cos(theta)], axis=0)rotation_matrix = np.reshape(rotation, [2, 2])#旋轉(zhuǎn)方框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)#旋轉(zhuǎn)坐標(biāo)landmarks = np.matmul(landmarks*scaler - center, rotation_matrix) + centerlandmarks = landmarks/scaler#旋轉(zhuǎn)圖像image = Image.fromarray(image)im_rotate = image.rotate(angle)im_rotate = np.array(im_rotate)return im_rotate, box, landmarks?
版權(quán)聲明:本文為qq_16564093原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/qq_16564093/article/details/106000209
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【Python-随机旋转】图像随机旋转及坐标进行旋转原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LTE切换与TAU问题
- 下一篇: Python模块:time模块详解(转)