openCV幾何變換

2021-10-14 10:02:45 字數 2582 閱讀 9577

#影象的幾何變換:移動,旋轉,仿射變換等

#主要的函式:cv2.getperspectivetransfrom

'''#1.擴充套件縮放

#提供函式是:cv2.resize(src, dst, interpolation = cv_inter_linear)

#在函式cv2.resize()中尺寸大小可以自己設定,也可以使用縮放因子

#縮放因子:

#縮放:cv2.inter_area()

#擴充套件:cv2.inter_cubic()(慢) or cv2.inter_linear()(預設情況)

res = cv2.resize(img, none, fx = 2, fy = 2, interpolation = cv2.inter_cubic)#none已經有了縮放因子,無需尺寸

height, width = img.shape[:2]

res = cv2.resize(img, (2*width, 2*height), interpolation = cv2.inter_cubic)#已經有了尺寸

while(1):

cv2.imshow('res', res)

cv2.imshow('image', img)

cv2.waitkey(0)

cv2.destroyallwindows()

''''''

#2.平移和旋轉

#平移的函式:cv2.warpaffine()

#旋轉的函式:cv2.getrotationmatrix2d(旋轉中心, 旋轉角度, 縮放因子)

import cv2

import numpy as np

res = cv2.imread('test1.png', 0)

rows, cols = res.shape#獲取的屬性

ans = cv2.getrotationmatrix2d((cols/2, rows/2), 90, 0.3)#旋轉,旋轉中心,旋轉角度,縮放因子

dst = cv2.warpaffine(res, ans, (2*cols, 2*rows))#平移,第三個引數是大小,(寬,高)

cv2.imshow('res', res)

cv2.imshow('ans'," ans)

cv2.imshow('dst', dst)

cv2.waitkey(0)

cv2.destroyallwindows()"

'''#3.變換

#對於平面區域,有兩種方式的幾何轉換:一種是基於2×3矩陣進行的變換,叫仿射變換;另一種是基於3×3矩陣進行的變換,叫透視變換或者單應性對映

'''#仿射變換:原圖直線平行,後圖直線也平行,基於2×3矩陣進行的變換。需要從原影象中找到三個點以及他們在輸出影象中的位置。

# 類似於構造矩陣

m = cv2.getaffinetransform(pts1, pts2)

res = cv2.warpaffine(img, m, (cols, rows))

cv2.imshow('原圖', img)

cv2.imshow('res', res)

cv2.waitkey(0)

cv2.destroyallwindows()

'''#2透視變換

#在變換前後直線還是直線,是基於3×3矩陣進行的變換,要在影象中找到4個點及他們在輸出影象上對應位置,這四個點中任意三個點不可以共線

OpenCV 幾何變換

使用cv2.resize 來實現 縮放因子 縮放 cv2.inter area 擴充套件 cv2.inter linera cv2.inter cubic 下面的none本該是輸出的尺寸,但這裡因為後面我們設定了縮放因子 res1 cv2.resize img,none,fx 2,fy 2,inte...

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...