日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Opencv-Python學(xué)習(xí)筆記五

系統(tǒng) 1762 0
原文鏈接: https://www.jianshu.com/p/ef67cacf442c

本篇筆記主要記錄Opencv里的圖像翻轉(zhuǎn),平移,旋轉(zhuǎn),仿射及透視功能,主要是下面幾個API:

  • cv2.flip() # 圖像翻轉(zhuǎn)
  • cv2.warpAffine() #圖像仿射
  • cv2.getRotationMatrix2D()?。H〉眯D(zhuǎn)角度的Matrix
  • cv2.GetAffineTransform(src, dst, mapMatrix) #取得圖像仿射的matrix
  • cv2.getPerspectiveTransform(src, dst) #取得圖像透視的4個點起止值
  • cv2.warpPerspective()?。D像透視

圖像翻轉(zhuǎn) cv2.flip()

cv2.flip(src, flipCode[, dst]) → dst

  • src: 原始圖像矩陣;
  • dst: 變換后的矩陣;
  • flipMode: 翻轉(zhuǎn)模式,有三種模式
    • 0 --- 垂直方向翻轉(zhuǎn); 1----- 水平方向翻轉(zhuǎn); -1:水平、垂直方向同時翻轉(zhuǎn)

    flipCode==0垂直翻轉(zhuǎn)(沿X軸翻轉(zhuǎn)),flipCode>0水平翻轉(zhuǎn)(沿Y軸翻轉(zhuǎn)),flipCode<0水平垂直翻轉(zhuǎn)(先沿X軸翻轉(zhuǎn),再沿Y軸翻轉(zhuǎn),等價于旋轉(zhuǎn)180°)

            
              %matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
import cv2

image = cv2.imread("aier.jpg")
# Flipped Horizontally 水平翻轉(zhuǎn)
h_flip = cv2.flip(image, 1)
# Flipped Vertically 垂直翻轉(zhuǎn)
v_flip = cv2.flip(image, 0)
# Flipped Horizontally & Vertically 水平垂直翻轉(zhuǎn)
hv_flip = cv2.flip(image, -1)

plt.figure(figsize=(8,8))

plt.subplot(221)
plt.imshow(image[:,:,::-1])
plt.title('original')

plt.subplot(222)
plt.imshow(h_flip[:,:,::-1])
plt.title('horizontal flip')

plt.subplot(223)
plt.imshow(v_flip[:,:,::-1])
plt.title(' vertical flip')

plt.subplot(224)
plt.imshow(hv_flip[:,:,::-1])
plt.title('h_v flip')
# 調(diào)整子圖間距
# plt.subplots_adjust(wspace=0.5, hspace=0.1)
plt.subplots_adjust(top=0.8, bottom=0.08, left=0.10, right=0.95, hspace=0,
                    wspace=0.35)
# plt.tight_layout()
plt.show()

            
          

Opencv-Python學(xué)習(xí)筆記五_第1張圖片

cv2.flip()

圖像平移,旋轉(zhuǎn),仿射  cv2.warpAffine()

cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])

            
              src input image.
dst output image that has the size dsize and the same type as src .
M   2×3 transformation matrix.
dsize   size of the output image.
flags   combination of interpolation methods (see InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( dst→src ).
borderMode  pixel extrapolation method (see BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the "outliers" in the source image are not modified by the function.
borderValue value used in case of a constant border; by default, it is 0.

            
          

cv2.getRotationMatrix2D(center, angle, scale)

函數(shù)有三個參數(shù):

  • center:圖片的旋轉(zhuǎn)中心
  • angle:旋轉(zhuǎn)角度
  • scale:縮放比例,該例中0.5表示我們縮小一半
            
              %matplotlib inline
from matplotlib import pyplot as plt
import cv2
import numpy as np

img = cv2.imread('aier.jpg')
rows,cols = img.shape[:2]

# 定義平移矩陣,需要是numpy的float32類型
# x軸平移200,y軸平移100, 2*3矩陣
M = np.float32([[1, 0, 200], [0, 1, 100]])
# 用仿射變換實現(xiàn)平移
img_s = cv2.warpAffine(img, M, (cols, rows), borderValue=(155, 150, 200))

# 第一個參數(shù)旋轉(zhuǎn)中心,第二個參數(shù)旋轉(zhuǎn)角度,第三個參數(shù):縮放比例, 生成一2*3的矩陣
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
M1 = cv2.getRotationMatrix2D((cols/2,rows/2),180,1)
M2 = cv2.getRotationMatrix2D((cols/2,rows/2),60,1)
print(M)
'''
[[ 6.123234e-17  1.000000e+00  1.500000e+02]
 [-1.000000e+00  6.123234e-17  6.500000e+02]]
'''
# 第三個參數(shù):變換后的圖像大小
img_tra = cv2.warpAffine(img,M,(cols,rows))
img_tra1 = cv2.warpAffine(img,M1,(cols,rows))
img_tra2 = cv2.warpAffine(img,M2,(cols,rows), borderValue=(155, 100, 155))

plt.figure(figsize=(8,8))
plt.subplot(221)
plt.imshow(img[:,:,::-1])

plt.subplot(222)
plt.imshow(img_s[:,:,::-1])

plt.subplot(223)
plt.imshow(img_tra[:,:,::-1])

plt.subplot(224)
plt.imshow(img_tra2[:,:,::-1])

plt.subplots_adjust(top=0.8, bottom=0.08, left=0.10, right=0.95, hspace=0,
                    wspace=0.35)
plt.show()

            
          

Opencv-Python學(xué)習(xí)筆記五_第2張圖片

平移,旋轉(zhuǎn)

圖像仿射

圖像的旋轉(zhuǎn)加上拉升就是 圖像仿射變換 ,仿射變化也是需要一個M矩陣就可以,但是由于仿射變換比較復(fù)雜,一般直接找很難找到這個矩陣,opencv提供了根據(jù)變換前后三個點的對應(yīng)關(guān)系來自動求解M。這個函數(shù)是
M=cv2.getAffineTransform(pos1,pos2),其中兩個位置就是變換前后的對應(yīng)位置關(guān)系。輸出的就是仿射矩陣M。然后在使用函數(shù)cv2.warpAffine()。

cv.GetAffineTransform(src, dst, mapMatrix) → None

  • Parameters: 變換前的三個點與其對應(yīng)的變換后的點.
    • src – Coordinates of triangle vertices in the source image.
    • dst – Coordinates of the corresponding triangle vertices in the destination image.

The function calculates the 2*3 matrix of an affine transform.

AffineMatrix = cv2.getAffineTransform(np.array(SrcPointsA),
np.array(CanvasPointsA))

Opencv-Python學(xué)習(xí)筆記五_第3張圖片

圖像仿射示例圖

            
              %matplotlib inline
from matplotlib import pyplot as plt
import cv2
import numpy as np

img = cv2.imread('aier.jpg')
rows,cols = img.shape[:2]
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,20],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
#第三個參數(shù):變換后的圖像大小
res = cv2.warpAffine(img,M,(rows,cols))
plt.subplot(121)
plt.imshow(img[:,:,::-1])

plt.subplot(122)
plt.imshow(res[:,:,::-1])

plt.show()

            
          

透視 Perspective

視角變換,需要一個3*3變換矩陣。在變換前后要保證直線還是直線。
構(gòu)建此矩陣需要在輸入圖像中找尋 4個點 ,以及在輸出圖像中對應(yīng)的位置。這四個點中的任意三個點不能共線。

            
              ## pts1 ==> pts2
pts1=np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2=np.float32([[0,0],[300,0],[0,300],[300,300]])
M=cv2.getPerspectiveTransform(pts1,pts2)

            
          

cv2.getPerspectiveTransform(np.array(SrcPointsA), np.array(CanvasPointsA))

cv2.getPerspectiveTransform(src, dst) → retval

cv2.warpPerspective(Img, PerspectiveMatrix, (300, 300))

cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) → dst

            
              src – input image.
dst – output image that has the size dsize and the same type as src .
M – 3*3 transformation matrix.
dsize – size of the output image.
flags – combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation ( dst ---> src ).
borderMode – pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).
borderValue – value used in case of a constant border; by default, it equals 0.

            
          

Opencv-Python學(xué)習(xí)筆記五_第4張圖片

4個點前后映射示例圖

            
              %matplotlib inline
from matplotlib import pyplot as plt
import cv2
import numpy as np

img=cv2.imread('aier.jpg')
rows,cols,ch=img.shape
pts1=np.float32([[56,5],[368,5],[28,387],[389,390]])
pts2=np.float32([[0,0],[300,0],[0,300],[300,300]])
M=cv2.getPerspectiveTransform(pts1,pts2)
print(M)
dst=cv2.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

            
          

Opencv-Python學(xué)習(xí)筆記五_第5張圖片

圖像透視

本文主要內(nèi)容就是這些,其他更深入功能待后續(xù)繼續(xù)完善.....
[參考]
Geometric Transformations of Images



作者:深思海數(shù)_willschang
鏈接:https://www.jianshu.com/p/ef67cacf442c
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 乐清市| 黄大仙区| 福海县| 黎城县| 怀远县| 林口县| 许昌市| 琼海市| 达拉特旗| 石棉县| 普陀区| 小金县| 凌源市| 华池县| 甘南县| 平度市| 安多县| 靖江市| 乐安县| 和政县| 商丘市| 澎湖县| 景宁| 屏南县| 永昌县| 镇原县| 彭阳县| 深圳市| 拉萨市| 石柱| 普宁市| 永顺县| 嘉善县| 阿瓦提县| 个旧市| 新巴尔虎左旗| 马尔康县| 衡山县| 会东县| 承德市| 井研县|