python opencv對圖像進行旋轉且不裁剪圖片的實現方法
更新時間:2019年07月09日 17:57:14 作者:hui3909
今天小編就為大家分享一篇python opencv對圖像進行旋轉且不裁剪圖片的實現方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
最近在做深度學習時需要用到圖像處理相關的操作,在度娘上找到的圖片旋轉方法千篇一律,旋轉完成的圖片都不是原始大小,很苦惱,于是google到歪果仁的網站扒拉了一個方法,親測好用,再次嫌棄天下文章一大抄的現象,雖然我也是抄歪果仁的。
廢話不多說了,直接貼代碼了。
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))
其他的不用多說了吧,第一個參數穿opencv讀取的圖像,第二個參數傳入需要旋轉的角度,enjoy!
以上這篇python opencv對圖像進行旋轉且不裁剪圖片的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在Keras中利用np.random.shuffle()打亂數據集實例
這篇文章主要介紹了在Keras中利用np.random.shuffle()打亂數據集實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python使用列表和字典實現簡單的考試系統(tǒng)詳解
這篇文章主要介紹了Python使用列表和字典實現簡單的考試系統(tǒng),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01