佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > python opencv旋轉(zhuǎn)圖片的使用方法

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

熱門標(biāo)簽:智能手機(jī) 鐵路電話系統(tǒng) 呼叫中心市場(chǎng)需求 服務(wù)器配置 美圖手機(jī) 網(wǎng)站文章發(fā)布 檢查注冊(cè)表項(xiàng) 銀行業(yè)務(wù)

背景

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

原始圖像:

opencv函數(shù)

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

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

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

cv2.flip : 對(duì)圖像矩陣進(jìn)行翻轉(zhuǎn)處理,參數(shù)可以設(shè)置為1,0,-1,分別對(duì)應(yīng)著水平翻轉(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)時(shí)候只需要與旋轉(zhuǎn)矩陣進(jìn)行乘積即可完成旋轉(zhuǎn)操作

旋轉(zhuǎn)矩陣M

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

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

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

numpy函數(shù)

numpy實(shí)現(xiàn)旋轉(zhuǎn)一般是使用numpy.rot90對(duì)圖像進(jìn)行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.

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

旋轉(zhuǎn)90度

逆時(shí)針

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

順時(shí)針

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) 順時(shí)針旋轉(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實(shí)現(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ù)負(fù)值表示順時(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 缺省,默認(rèn)是黑色(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)任意角度

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

您可能感興趣的文章:
  • 基于OpenCV和C++ 實(shí)現(xiàn)圖片旋轉(zhuǎn)
  • Python+OpenCV+圖片旋轉(zhuǎn)并用原底色填充新四角的例子
  • Python+OpenCV 實(shí)現(xiàn)圖片無損旋轉(zhuǎn)90°且無黑邊
  • python opencv對(duì)圖像進(jìn)行旋轉(zhuǎn)且不裁剪圖片的實(shí)現(xiàn)方法
  • python opencv實(shí)現(xiàn)圖片旋轉(zhuǎn)矩形分割
  • opencv圖片的任意角度旋轉(zhuǎn)實(shí)現(xiàn)示例

標(biāo)簽:河南 樂山 長(zhǎng)治 新疆 上海 紅河 沈陽(yáng) 滄州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python opencv旋轉(zhuǎn)圖片的使用方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    瓮安县| 卓资县| 武陟县| 耿马| 福海县| 汉阴县| 南召县| 安达市| 托克托县| 九龙城区| 岳阳县| 贵南县| 南郑县| 平度市| 津南区| 永仁县| 瓦房店市| 文昌市| 玉屏| 乌拉特中旗| 板桥市| 芜湖县| 蓬莱市| 永丰县| 靖边县| 临泽县| 民乐县| 甘德县| 紫云| 白水县| 乐昌市| 庄河市| 凉山| 秭归县| 吴川市| 洪洞县| 中卫市| 泌阳县| 陇川县| 承德市| 平南县|