欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Opencv-Python學習筆記五

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

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

  • cv2.flip() # 圖像翻轉
  • cv2.warpAffine() #圖像仿射
  • cv2.getRotationMatrix2D() #取得旋轉角度的Matrix
  • cv2.GetAffineTransform(src, dst, mapMatrix) #取得圖像仿射的matrix
  • cv2.getPerspectiveTransform(src, dst) #取得圖像透視的4個點起止值
  • cv2.warpPerspective() #圖像透視

圖像翻轉 cv2.flip()

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

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

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

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

image = cv2.imread("aier.jpg")
# Flipped Horizontally 水平翻轉
h_flip = cv2.flip(image, 1)
# Flipped Vertically 垂直翻轉
v_flip = cv2.flip(image, 0)
# Flipped Horizontally & Vertically 水平垂直翻轉
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')
# 調整子圖間距
# 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學習筆記五_第1張圖片

cv2.flip()

圖像平移,旋轉,仿射  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:圖片的旋轉中心
  • angle:旋轉角度
  • 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ù)旋轉中心,第二個參數(shù)旋轉角度,第三個參數(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學習筆記五_第2張圖片

平移,旋轉

圖像仿射

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

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

  • Parameters: 變換前的三個點與其對應的變換后的點.
    • 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學習筆記五_第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變換矩陣。在變換前后要保證直線還是直線。
構建此矩陣需要在輸入圖像中找尋 4個點 ,以及在輸出圖像中對應的位置。這四個點中的任意三個點不能共線。

            
              ## 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學習筆記五_第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學習筆記五_第5張圖片

圖像透視

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



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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美性黑人极品 hd 无码一区二区三区曰本A片 | 免费国产在线视频 | 日本高清色视频在线观看免费 | 欧美13videosex性极品 | 日本在线高清视频 | 最全精品自拍视频在线 | 伊人狠狠干 | 亚洲一区二区免费视频 | 亚洲成人网在线观看 | 国产你懂得 | 一级视频在线免费观看 | www.热| 国产高清在线观看av | 亚洲精品三级 | 五月天婷婷免费观看视频在线 | 免费一级做a爰片性视频 | 国产欧美在线观看视频 | 国产aaaaa一级毛片 | 色吧综合 | 日韩福利在线 | 中文字幕av一区二区 | 国产成人aaa在线视频免费观看 | 欧美视频在线免费 | 午夜伦理在线播放 | 国产免费小视频 | 日韩激情视频 | 丁香六月激情婷婷 | 人人澡人人澡人人看添欧美 | 91精品久久一区二区三区 | 国产日韩一区在线精品欧美玲 | 久爱视频www在线播放 | 日本国产欧美 | 久久久在线视频 | 日韩城人免费 | 久草手机在线 | 一级特黄aa大片欧美 | 黑人精品欧美一区二区蜜桃 | 国产精品国产精品国产专区不卡 | 精品国产一区二区三区香蕉沈先生 | 中文字幕亚洲精品 | 久久久中文字幕日本 |