最近在做深度學(xué)習(xí)時(shí)需要用到圖像處理相關(guān)的操作,在度娘上找到的圖片旋轉(zhuǎn)方法千篇一律,旋轉(zhuǎn)完成的圖片都不是原始大小,很苦惱,于是google到歪果仁的網(wǎng)站扒拉了一個(gè)方法,親測(cè)好用,再次嫌棄天下文章一大抄的現(xiàn)象,雖然我也是抄歪果仁的。
廢話不多說了,直接貼代碼了。
def rotate_bound(image, angle): # grab the dimensions of the image and then determine the # center (h, w) = image.shape[:2] (cX, cY) = (w // 2, h // 2) # grab the rotation matrix (applying the negative of the # angle to rotate clockwise), then grab the sine and cosine # (i.e., the rotation components of the matrix) M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0) cos = np.abs(M[0, 0]) sin = np.abs(M[0, 1]) # compute the new bounding dimensions of the image nW = int((h * sin) + (w * cos)) nH = int((h * cos) + (w * sin)) # adjust the rotation matrix to take into account translation M[0, 2] += (nW / 2) - cX M[1, 2] += (nH / 2) - cY # perform the actual rotation and return the image return cv2.warpAffine(image, M, (nW, nH))
其他的不用多說了吧,第一個(gè)參數(shù)穿opencv讀取的圖像,第二個(gè)參數(shù)傳入需要旋轉(zhuǎn)的角度,enjoy!
以上這篇python opencv對(duì)圖像進(jìn)行旋轉(zhuǎn)且不裁剪圖片的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
