欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python opencv旋轉(zhuǎn)圖片的使用方法

 更新時間:2021年06月04日 10:22:55   作者:uncle_ll  
在圖像處理中,有的時候會有對圖片進行角度旋轉(zhuǎn)的處理,尤其是在計算機視覺中對于圖像擴充,旋轉(zhuǎn)角度擴充圖片是一種常見的處理。本文就詳細的介紹一下,感興趣的可以了解一下

背景

在圖像處理中,有的時候會有對圖片進行角度旋轉(zhuǎn)的處理,尤其是在計算機視覺中對于圖像擴充,旋轉(zhuǎn)角度擴充圖片是一種常見的處理。這種旋轉(zhuǎn)圖片的應用場景也比較多,比如用戶上傳圖片是豎著的時候,不好進行處理,也需要對其進行旋轉(zhuǎn),以便后續(xù)算法處理。常見的旋轉(zhuǎn)處理有兩種方式,一種是轉(zhuǎn)化為numpy矩陣后,對numpy矩陣進行處理,另外一種是使用opencv自帶的函數(shù)進行各種變換處理,以實現(xiàn)旋轉(zhuǎn)角度的結(jié)果。

原始圖像:

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Fo40V1TN-1592548330569)(C:\Users\DELL-3020\AppData\Roaming\Typora\typora-user-images\image-20200618180728108.jpg)]

opencv函數(shù)

旋轉(zhuǎn)中常用的函數(shù)有以下幾個函數(shù)

cv2.transpose: 對圖像矩陣進行轉(zhuǎn)置處理

img = cv2.imread(origin_img_path)
img_transpose = cv2.transpose(img)
cv2.imshow('transpose', img_transpose)
cv2.waitKey(0)

在這里插入圖片描述

cv2.flip : 對圖像矩陣進行翻轉(zhuǎn)處理,參數(shù)可以設置為1,0,-1,分別對應著水平翻轉(zhuǎn)、垂直翻轉(zhuǎn)、水平垂直翻轉(zhuǎn)。

img = cv2.imread(origin_img_path)
img_flip = cv2.flip(img, 1)
cv2.imshow('flip', img_flip)
cv2.waitKey(0)

在這里插入圖片描述

cv2.getRotationMatrix2D: 構(gòu)建旋轉(zhuǎn)矩陣M,后續(xù)旋轉(zhuǎn)時候只需要與旋轉(zhuǎn)矩陣進行乘積即可完成旋轉(zhuǎn)操作

旋轉(zhuǎn)矩陣M

img

img = cv2.imread(origin_img_path)
rows, cols = img.shape
# 這里的第一個參數(shù)為旋轉(zhuǎn)中心,第二個為旋轉(zhuǎn)角度,第三個為旋轉(zhuǎn)后的縮放因子
# 可以通過設置旋轉(zhuǎn)中心,縮放因子以及窗口大小來防止旋轉(zhuǎn)后超出邊界的問題
M = cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)

cv2.warpAffine: 對圖像進行仿射變換,一般進行平移或者旋轉(zhuǎn)操作

img = cv2.imread(origin_img_path)
cv2.warpAffine(img, M,(lengh,lengh),borderValue=(255,255,255))  # M為上面的旋轉(zhuǎn)矩陣

numpy函數(shù)

numpy實現(xiàn)旋轉(zhuǎn)一般是使用numpy.rot90對圖像進行90度倍數(shù)的旋轉(zhuǎn)操作

官方介紹:

numpy.rot90(m, k=1, axes=(0, 1))[source]

Rotate an array by 90 degrees in the plane specified by axes.

Rotation direction is from the first towards the second axis.

k: Number of times the array is rotated by 90 degrees.

關鍵參數(shù)k表示旋轉(zhuǎn)90度的倍數(shù),k的取值一般為1、2、3,分別表示旋轉(zhuǎn)90度、180度、270度;k也可以取負數(shù),-1、-2、-3。k取正數(shù)表示逆時針旋轉(zhuǎn),取負數(shù)表示順時針旋轉(zhuǎn)。

旋轉(zhuǎn)90度

逆時針

  • 使用opencv函數(shù)的轉(zhuǎn)置操作+翻轉(zhuǎn)操作實現(xiàn)旋轉(zhuǎn)
  • 使用numpy.rot90實現(xiàn)
def rotateAntiClockWise90(img_file):  # 逆時針旋轉(zhuǎn)90度
	img = cv2.imread(img_file)
    trans_img = cv2.transpose(img)
    img90 = cv2.flip(trans_img, 0)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90
    
def totateAntiClockWise90ByNumpy(img_file):  # np.rot90(img, -1) 逆時針旋轉(zhuǎn)90度
    img = cv2.imread(img_file)
    img90 = np.rot90(img, -1)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90

在這里插入圖片描述

順時針

def rotateClockWise90(self, img_file):
	img = cv2.imread(img_file)
    trans_img = cv2.transpose( img )
    img90 = cv2.flip(trans_img, 1)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90

def totateClockWise90ByNumpy(img_file):  # np.rot90(img, 1) 順時針旋轉(zhuǎn)90度
    img = cv2.imread(img_file)
    img90 = np.rot90(img, 1)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90

在這里插入圖片描述

旋轉(zhuǎn)180度、270度

使用numpy.rot90實現(xiàn)旋轉(zhuǎn)180度、270度

180度

img180 = np.rot90(img, 2)
cv2.imshow("rotate", img180)
cv2.waitKey(0)

在這里插入圖片描述

270 度

img270 = np.rot90(img, 3)
cv2.imshow("rotate", img270)
cv2.waitKey(0)

在這里插入圖片描述

旋轉(zhuǎn)任意角度,以任意色值填充背景

import cv2
from math import *
import numpy as np
 
# 旋轉(zhuǎn)angle角度,缺失背景白色(255, 255, 255)填充
def rotate_bound_white_bg(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)
    # -angle位置參數(shù)為角度參數(shù)負值表示順時針旋轉(zhuǎn); 1.0位置參數(shù)scale是調(diào)整尺寸比例(圖像縮放參數(shù)),建議0.75
    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
    # borderValue 缺失背景填充色彩,此處為白色,可自定義
    return cv2.warpAffine(image, M, (nW, nH),borderValue=(255,255,255))
    # borderValue 缺省,默認是黑色(0, 0 , 0)
    # return cv2.warpAffine(image, M, (nW, nH))
 
img = cv2.imread("dog.png")
imgRotation = rotate_bound_white_bg(img, 45)
 
cv2.imshow("img",img)
cv2.imshow("imgRotation",imgRotation)
cv2.waitKey(0)

45度

在這里插入圖片描述

60度

在這里插入圖片描述

參考

cv2.getRotationMatrix2D博客介紹

cv2.warpAffine 博客介紹

numpy.rot90

旋轉(zhuǎn)任意角度

到此這篇關于python opencv旋轉(zhuǎn)圖片的使用方法的文章就介紹到這了,更多相關python opencv旋轉(zhuǎn)圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • OpenCV學習之圖像的分割與修復詳解

    OpenCV學習之圖像的分割與修復詳解

    圖像分割本質(zhì)就是將前景目標從背景中分離出來。在當前的實際項目中,應用傳統(tǒng)分割的并不多,大多是采用深度學習的方法以達到更好的效果。本文將詳細介紹一下OpenCV中的圖像分割與修復,需要的可以參考一下
    2022-01-01
  • Python利用pandas和matplotlib實現(xiàn)繪制柱狀折線圖

    Python利用pandas和matplotlib實現(xiàn)繪制柱狀折線圖

    這篇文章主要為大家詳細介紹了如何使用?Python?中的?Pandas?和?Matplotlib?庫創(chuàng)建一個柱狀圖與折線圖結(jié)合的數(shù)據(jù)可視化圖表,感興趣的可以了解一下
    2023-11-11
  • python獲取本機外網(wǎng)ip的方法

    python獲取本機外網(wǎng)ip的方法

    這篇文章主要介紹了python獲取本機外網(wǎng)ip的方法,可實現(xiàn)從外網(wǎng)顯示IP的網(wǎng)站獲取本機IP的功能,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Python之lxml安裝失敗的解決

    Python之lxml安裝失敗的解決

    這篇文章主要介紹了Python之lxml安裝失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python編程中內(nèi)置的NotImplemented類型的用法

    Python編程中內(nèi)置的NotImplemented類型的用法

    這篇文章主要介紹了Python編程中內(nèi)置的NotImplemented類型的用法,NotImplemented 是Python在內(nèi)置命名空間中的六個常數(shù)之一,下文更多詳細內(nèi)容需要的小伙伴可以參考一下
    2022-03-03
  • python實現(xiàn)過濾敏感詞

    python實現(xiàn)過濾敏感詞

    這篇文章主要介紹了python如何實現(xiàn)過濾敏感詞,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
    2021-05-05
  • python讀取Android permission文件

    python讀取Android permission文件

    python解析json文件讀取Android permission,同時可以學習到json的知識。
    2013-11-11
  • 如何在python中實現(xiàn)隨機選擇

    如何在python中實現(xiàn)隨機選擇

    這篇文章主要介紹了如何在python中實現(xiàn)隨機選擇,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • Python面向?qū)ο缶幊剃P鍵深度探索類與對象

    Python面向?qū)ο缶幊剃P鍵深度探索類與對象

    這篇文章主要為大家介紹了Python面向?qū)ο缶幊剃P鍵深度探索類與對象示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • python讀取一個目錄下所有txt里面的內(nèi)容方法

    python讀取一個目錄下所有txt里面的內(nèi)容方法

    今天小編就為大家分享一篇python讀取一個目錄下所有txt里面的內(nèi)容方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論