OpenCV 幾何變換

2021-10-18 05:09:19 字數 2422 閱讀 2376

使用cv2.resize()來實現

縮放因子:

· 縮放 cv2.inter_area

· 擴充套件 cv2.inter_linera、cv2.inter_cubic

# 下面的none本該是輸出的尺寸,但這裡因為後面我們設定了縮放因子

res1 = cv2.resize(img, none, fx = 2, fy = 2, interpolation = cv2.inter_cubic)

res2 = cv2.resize(img, (2 * width, 2 * height), interpolation = cv2.inter_cubic)

構建矩陣m([1, 0, tx], [0, 1, ty]),型別為float32

tx,ty為要移動的距離

import numpy as np

import cv2

img = cv2.imread(

'a.jpeg'

)cows, rols, channels = img.shape

# 構造矩陣

m = np.float32([[

1,0,

100],[

0,1,

50]])

# 第三個引數為輸出的大小

dst = cv2.warpaffine(img, m,

(cows, rols)

)cv2.imshow(

'dst'

, dst)

cv2.imshow(

'img'

, img)

cv2.waitkey(

0)

旋轉與平移類似

使用cv2.getrotationmatrix2d()函式構造矩陣

import numpy as np

import cv2

img = cv2.imread(

'a.jpeg'

)rows, cols, channels = img.shape

m = cv2.getrotationmatrix2d(

(rows/

2, cols/2)

,45,0.6

)dst = cv2.warpaffine(img, m,

(rows , cols )

)cv2.imshow(

' dst'

, dst)

cv2.waitkey(

0)

使用getaffinetransform函式構造矩陣

import numpy as np

import cv2

img = cv2.imread(

'a.jpeg'

)rows, cols, ch = img.shape

# 起始位置

pts1 = np.float32([[

50,50]

,[200,50]

,[50,

200]])

# 仿射變換後的位置

pts2 = np.float32([[

10,100],[

200,50]

,[100,

250]])

m = cv2.getaffinetransform(pts1, pts2)

dst = cv2.warpaffine(img, m,

(cols, rows)

)cv2.imshow(

'dst'

, dst)

cv2.waitkey(

0)

import numpy as np

import cv2

img = cv2.imread(

'a.jpeg'

)rows, cols, ch = img.shape

pts1 = np.float32([[

120,65]

,[368,52]

,[28,

387],[

389,

390]])

pts2 = np.float32([[

0,0]

,[800,0]

,[0,

800],[

800,

800]])

# 構造矩陣,這裡函式用的和上面的不同

m = cv2.getperspectivetransform(pts1, pts2)

# 變換使用的函式與上面也不同

dst = cv2.warpperspective(img, m,

(800

,800))

cv2.imshow(

'dst'

, dst)

cv2.waitkey(

0)

openCV幾何變換

影象的幾何變換 移動,旋轉,仿射變換等 主要的函式 cv2.getperspectivetransfrom 1.擴充套件縮放 提供函式是 cv2.resize src,dst,interpolation cv inter linear 在函式cv2.resize 中尺寸大小可以自己設定,也可以使用縮...

OpenCV學習筆記 幾何變換

擴充套件縮放只是改變影象的大小。opencv提供函式cv.resize 實現這個功能。在縮放時推薦使用cv.inter area,在擴充套件時推薦使用cv.inter cubic 慢 和cv.inter linear。擴充套件縮放 defresize demo img 使用縮放因子 res cv.r...

Python呼叫OpenCV幾何變換

這一篇就記錄三個 影象縮放 平移和映象。影象縮放用到的函式是cv2.resize 函式,函式原型如下 resize src,dsize,dst none,fx none,fy none,interpolation none opencv提供了幾種縮放方式 cv2.inter area cv2.int...