日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

【Python-随机旋转】图像随机旋转及坐标进行旋转原理

發(fā)布時(shí)間:2023/12/15 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python-随机旋转】图像随机旋转及坐标进行旋转原理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在做人臉或物體檢測(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。