OpenCV 4.13.0
開源計算機視覺庫 (Open Source Computer Vision)
正在載入...
正在搜尋...
未找到匹配項
幾何影像變換

詳細說明

本節中的函式執行二維影像的各種幾何變換。它們不改變影像內容,而是使畫素網格變形並將此變形後的網格對映到目標影像。實際上,為了避免取樣偽影,對映以相反的順序進行,從目標到源。也就是說,對於目標影像的每個畫素 \((x, y)\),函式計算源影像中相應“捐贈”畫素的座標,並複製畫素值

\[\texttt{dst} (x,y)= \texttt{src} (f_x(x,y), f_y(x,y))\]

如果您指定正向對映 \(\left<g_x, g_y\right>: \texttt{src} \rightarrow \texttt{dst}\),OpenCV 函式首先計算相應的逆對映 \(\left<f_x, f_y\right>: \texttt{dst} \rightarrow \texttt{src}\),然後使用上述公式。

幾何變換的實際實現,從最通用的 `remap` 到最簡單最快的 `resize`,需要解決上述公式的兩個主要問題

注意
幾何變換不適用於 CV_8SCV_32S 影像。

列舉

enum  cv::InterpolationFlags {
  cv::INTER_NEAREST = 0 ,
  cv::INTER_LINEAR = 1 ,
  cv::INTER_CUBIC = 2 ,
  cv::INTER_AREA = 3 ,
  cv::INTER_LANCZOS4 = 4 ,
  cv::INTER_LINEAR_EXACT = 5 ,
  cv::INTER_NEAREST_EXACT = 6 ,
  cv::INTER_MAX = 7 ,
  cv::WARP_FILL_OUTLIERS = 8 ,
  cv::WARP_INVERSE_MAP = 16 ,
  cv::WARP_RELATIVE_MAP = 32
}
 插值演算法 更多...
 
enum  cv::InterpolationMasks {
  cv::INTER_BITS = 5 ,
  cv::INTER_BITS2 = INTER_BITS * 2 ,
  cv::INTER_TAB_SIZE = 1 << INTER_BITS ,
  cv::INTER_TAB_SIZE2 = INTER_TAB_SIZE * INTER_TAB_SIZE
}
 
enum  cv::WarpPolarMode {
  cv::WARP_POLAR_LINEAR = 0 ,
  cv::WARP_POLAR_LOG = 256
}
 指定極座標對映模式。 更多...
 

函式

void cv::convertMaps (InputArray map1, InputArray map2, OutputArray dstmap1, OutputArray dstmap2, int dstmap1type, bool nninterpolation=false)
 將影像變換對映從一種表示形式轉換為另一種。
 
Mat cv::getAffineTransform (const Point2f src[], const Point2f dst[])
 透過三對對應點計算仿射變換矩陣。
 
Mat cv::getAffineTransform (InputArray src, InputArray dst)
 
Mat cv::getPerspectiveTransform (const Point2f src[], const Point2f dst[], int solveMethod=DECOMP_LU)
 
Mat cv::getPerspectiveTransform (InputArray src, InputArray dst, int solveMethod=DECOMP_LU)
 透過四對對應點計算透視變換矩陣。
 
void cv::getRectSubPix (InputArray image, Size patchSize, Point2f center, OutputArray patch, int patchType=-1)
 以亞畫素精度從影像中提取畫素矩形。
 
Mat cv::getRotationMatrix2D (Point2f center, double angle, double scale)
 計算二維旋轉的仿射矩陣。
 
Matx23d cv::getRotationMatrix2D_ (Point2f center, double angle, double scale)
 
void cv::invertAffineTransform (InputArray M, OutputArray iM)
 計算仿射變換的逆變換。
 
void cv::linearPolar (InputArray src, OutputArray dst, Point2f center, double maxRadius, int flags)
 將影像重對映到極座標空間。
 
void cv::logPolar (InputArray src, OutputArray dst, Point2f center, double M, int flags)
 將影像重對映到對數極座標空間。
 
void cv::remap (InputArray src, OutputArray dst, InputArray map1, InputArray map2, int interpolation, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
 對影像應用通用的幾何變換。
 
void cv::resize (InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)
 調整影像大小。
 
void cv::warpAffine (InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
 對影像應用仿射變換。
 
void cv::warpPerspective (InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
 對影像應用透視變換。
 
void cv::warpPolar (InputArray src, OutputArray dst, Size dsize, Point2f center, double maxRadius, int flags)
 將影像重對映到極座標或對數極座標空間。
 

列舉型別文件 (Enumeration Type Documentation)

◆ InterpolationFlags

#include <opencv2/imgproc.hpp>

插值演算法

列舉值 (Enumerator)
INTER_NEAREST 
Python: cv.INTER_NEAREST

最近鄰插值

INTER_LINEAR 
Python: cv.INTER_LINEAR

雙線性插值

INTER_CUBIC 
Python: cv.INTER_CUBIC

雙三次插值

INTER_AREA 
Python: cv.INTER_AREA

使用像素面積關係進行重取樣。這可能是影像縮減的首選方法,因為它能產生無莫爾條紋的結果。但當影像放大時,它類似於 INTER_NEAREST 方法。

INTER_LANCZOS4 
Python: cv.INTER_LANCZOS4

8x8 鄰域的 Lanczos 插值

INTER_LINEAR_EXACT 
Python: cv.INTER_LINEAR_EXACT

位精確雙線性插值

INTER_NEAREST_EXACT 
Python: cv.INTER_NEAREST_EXACT

位精確最近鄰插值。這將產生與 PIL、scikit-image 或 Matlab 中的最近鄰方法相同的結果。

INTER_MAX 
Python: cv.INTER_MAX

插值程式碼掩碼

WARP_FILL_OUTLIERS 
Python: cv.WARP_FILL_OUTLIERS

標誌,填充所有目標影像畫素。如果其中一些對應於源影像中的離群值,則它們被設定為零

WARP_INVERSE_MAP 
Python: cv.WARP_INVERSE_MAP

標誌,逆變換

例如,linearPolarlogPolar 變換

  • 標誌未設定: \(dst( \rho , \phi ) = src(x,y)\)
  • 標誌已設定: \(dst(x,y) = src( \rho , \phi )\)
WARP_RELATIVE_MAP 
Python: cv.WARP_RELATIVE_MAP

◆ InterpolationMasks

#include <opencv2/imgproc.hpp>

列舉值 (Enumerator)
INTER_BITS 
Python: cv.INTER_BITS
INTER_BITS2 
Python: cv.INTER_BITS2
INTER_TAB_SIZE 
Python: cv.INTER_TAB_SIZE
INTER_TAB_SIZE2 
Python: cv.INTER_TAB_SIZE2

◆ WarpPolarMode

#include <opencv2/imgproc.hpp>

指定極座標對映模式。

另請參閱
warpPolar
列舉值 (Enumerator)
WARP_POLAR_LINEAR 
Python: cv.WARP_POLAR_LINEAR

將影像重新對映到/從極座標空間。

WARP_POLAR_LOG 
Python: cv.WARP_POLAR_LOG

將影像重新對映到/從半對數極座標空間。

函式文件 (Function Documentation)

◆ convertMaps()

void cv::convertMaps ( InputArray map1,
InputArray map2,
OutputArray dstmap1,
OutputArray dstmap2,
int dstmap1type,
bool nninterpolation = false )
Python
cv.convertMaps(map1, map2, dstmap1type[, dstmap1[, dstmap2[, nninterpolation]]]) -> dstmap1, dstmap2

#include <opencv2/imgproc.hpp>

將影像變換對映從一種表示形式轉換為另一種。

該函式將用於 `remap` 的一對對映從一種表示形式轉換為另一種。支援以下選項 ( (map1.type(), map2.type()) \(\rightarrow\) (dstmap1.type(), dstmap2.type()) )

  • \(\texttt{(CV_32FC1, CV_32FC1)} \rightarrow \texttt{(CV_16SC2, CV_16UC1)}\)。這是最常用的轉換操作,其中原始浮點對映(參見 remap)轉換為更緊湊、更快的定點表示。第一個輸出陣列包含四捨五入的座標,第二個陣列(僅當 `nninterpolation=false` 時建立)包含插值表中的索引。
  • \(\texttt{(CV_32FC2)} \rightarrow \texttt{(CV_16SC2, CV_16UC1)}\)。與上面相同,但原始對映儲存在一個雙通道矩陣中。
  • 反向轉換。顯然,重建的浮點對映不會與原始對映完全相同。
引數
map1型別為 CV_16SC2、CV_32FC1 或 CV_32FC2 的第一個輸入對映。
map2型別為 CV_16UC1、CV_32FC1 或無(如果 `map1` 為空矩陣)的第二個輸入對映。
dstmap1型別為 `dstmap1type` 且大小與 `src` 相同的第一個輸出對映。
dstmap2第二個輸出對映。
dstmap1type第一個輸出對映的型別,應為 CV_16SC2、CV_32FC1 或 CV_32FC2。
nninterpolation標誌,指示定點對映是用於最近鄰插值還是更復雜的插值。
另請參閱
remap, undistort, initUndistortRectifyMap

◆ getAffineTransform() [1/2]

Mat cv::getAffineTransform ( const Point2f src[],
const Point2f dst[] )
Python
cv.getAffineTransform(src, dst) -> retval

#include <opencv2/imgproc.hpp>

透過三對對應點計算仿射變換矩陣。

該函式計算仿射變換的 \(2 \times 3\) 矩陣,使得

\[\begin{bmatrix} x'_i \\ y'_i \end{bmatrix} = \texttt{map_matrix} \cdot \begin{bmatrix} x_i \\ y_i \\ 1 \end{bmatrix}\]

其中

\[dst(i)=(x'_i,y'_i), src(i)=(x_i, y_i), i=0,1,2\]

引數
src源影像中三角形頂點的座標。
dst目標影像中相應三角形頂點的座標。
另請參閱
warpAffine, transform

◆ getAffineTransform() [2/2]

Mat cv::getAffineTransform ( InputArray src,
InputArray dst )
Python
cv.getAffineTransform(src, dst) -> retval

#include <opencv2/imgproc.hpp>

◆ getPerspectiveTransform() [1/2]

Mat cv::getPerspectiveTransform ( const Point2f src[],
const Point2f dst[],
int solveMethod = DECOMP_LU )
Python
cv.getPerspectiveTransform(src, dst[, solveMethod]) -> retval

#include <opencv2/imgproc.hpp>

這是一個過載的成員函式,為方便起見而提供。它與上述函式的區別僅在於所接受的引數不同。

◆ getPerspectiveTransform() [2/2]

Mat cv::getPerspectiveTransform ( InputArray src,
InputArray dst,
int solveMethod = DECOMP_LU )
Python
cv.getPerspectiveTransform(src, dst[, solveMethod]) -> retval

#include <opencv2/imgproc.hpp>

透過四對對應點計算透視變換矩陣。

該函式計算透視變換的 \(3 \times 3\) 矩陣,使得

\[\begin{bmatrix} t_i x'_i \\ t_i y'_i \\ t_i \end{bmatrix} = \texttt{map_matrix} \cdot \begin{bmatrix} x_i \\ y_i \\ 1 \end{bmatrix}\]

其中

\[dst(i)=(x'_i,y'_i), src(i)=(x_i, y_i), i=0,1,2,3\]

引數
src源影像中四邊形頂點的座標。
dst目標影像中相應四邊形頂點的座標。
solveMethod傳遞給 cv::solve (DecompTypes) 的方法
另請參閱
findHomography, warpPerspective, perspectiveTransform

◆ getRectSubPix()

void cv::getRectSubPix ( InputArray 影像,
Size patchSize,
Point2f center,
OutputArray patch,
int patchType = -1 )
Python
cv.getRectSubPix(image, patchSize, center[, patch[, patchType]]) -> patch

#include <opencv2/imgproc.hpp>

以亞畫素精度從影像中提取畫素矩形。

函式 `getRectSubPix` 從 `src` 中提取畫素

\[patch(x, y) = src(x + \texttt{center.x} - ( \texttt{dst.cols} -1)*0.5, y + \texttt{center.y} - ( \texttt{dst.rows} -1)*0.5)\]

其中非整數座標處的畫素值使用雙線性插值檢索。多通道影像的每個通道獨立處理。影像應為單通道或三通道影像。雖然矩形的中心必須在影像內部,但矩形的部分可能在影像外部。

引數
影像源影像。
patchSize提取的補丁大小。
center源影像中提取矩形中心的浮點座標。中心必須在影像內部。
patch提取的補丁,其大小為 `patchSize`,通道數與 `src` 相同。
patchType提取畫素的深度。預設情況下,它們與 `src` 具有相同的深度。
另請參閱
warpAffine, warpPerspective

◆ getRotationMatrix2D()

Mat cv::getRotationMatrix2D ( Point2f center,
double angle (角度),
double scale )
內聯
Python
cv.getRotationMatrix2D(center, angle, scale) -> retval

#include <opencv2/imgproc.hpp>

計算二維旋轉的仿射矩陣。

該函式計算以下矩陣

\[\begin{bmatrix} \alpha & \beta & (1- \alpha ) \cdot \texttt{center.x} - \beta \cdot \texttt{center.y} \\ - \beta & \alpha & \beta \cdot \texttt{center.x} + (1- \alpha ) \cdot \texttt{center.y} \end{bmatrix}\]

其中

\[\begin{array}{l} \alpha = \texttt{scale} \cdot \cos \texttt{angle} , \\ \beta = \texttt{scale} \cdot \sin \texttt{angle} \end{array}\]

該變換將旋轉中心對映到自身。如果這不是目標,請調整平移。

引數
center源影像中的旋轉中心。
angle (角度)旋轉角度,單位為度。正值表示逆時針旋轉(座標原點假定為左上角)。
scale各向同性縮放因子。
另請參閱
getAffineTransform, warpAffine, transform
此函式的呼叫圖

◆ getRotationMatrix2D_()

Matx23d cv::getRotationMatrix2D_ ( Point2f center,
double angle (角度),
double scale )

#include <opencv2/imgproc.hpp>

另請參閱
getRotationMatrix2D

◆ invertAffineTransform()

void cv::invertAffineTransform ( InputArray M,
OutputArray iM )
Python
cv.invertAffineTransform(M[, iM]) -> iM

#include <opencv2/imgproc.hpp>

計算仿射變換的逆變換。

該函式計算由 \(2 \times 3\) 矩陣 M 表示的逆仿射變換

\[\begin{bmatrix} a_{11} & a_{12} & b_1 \\ a_{21} & a_{22} & b_2 \end{bmatrix}\]

結果也是與 M 相同型別的 \(2 \times 3\) 矩陣。

引數
M原始仿射變換。
iM輸出逆仿射變換。

◆ linearPolar()

void cv::linearPolar ( InputArray src,
OutputArray dst,
Point2f center,
double maxRadius,
int flags (標誌) )
Python
cv.linearPolar(src, center, maxRadius, flags[, dst]) -> dst

#include <opencv2/imgproc.hpp>

將影像重對映到極座標空間。

已棄用
此函式產生與 cv::warpPolar(src, dst, src.size(), center, maxRadius, flags) 相同的結果。

◆ logPolar()

void cv::logPolar ( InputArray src,
OutputArray dst,
Point2f center,
double M,
int flags (標誌) )
Python
cv.logPolar(src, center, M, flags[, dst]) -> dst

#include <opencv2/imgproc.hpp>

將影像重對映到對數極座標空間。

已棄用
此函式產生與 cv::warpPolar(src, dst, src.size(), center, maxRadius, flags+WARP_POLAR_LOG) 相同的結果;

◆ remap()

void cv::remap ( InputArray src,
OutputArray dst,
InputArray map1,
InputArray map2,
int interpolation,
int borderMode = BORDER_CONSTANT,
const Scalar & borderValue = Scalar() )
Python
cv.remap(src, map1, map2, interpolation[, dst[, borderMode[, borderValue]]]) -> dst

#include <opencv2/imgproc.hpp>

對影像應用通用的幾何變換。

函式 `remap` 使用指定的對映變換源影像

\[\texttt{dst} (x,y) = \texttt{src} (map_x(x,y),map_y(x,y))\]

帶有 WARP_RELATIVE_MAP 標誌時

\[\texttt{dst} (x,y) = \texttt{src} (x+map_x(x,y),y+map_y(x,y))\]

其中具有非整數座標的畫素值使用可用的插值方法之一計算。\((map_x)\) 和 \((map_y)\) 可以分別編碼為 \(map_1\) 和 \(map_2\) 中的獨立浮點對映,或者 \(map_1\) 中的交錯浮點 \((x,y)\) 對映,或者透過使用 convertMaps 建立的定點對映。您可能希望從浮點表示轉換為定點表示的原因是它們可以產生更快(2 倍)的重新對映操作。在轉換的情況下,\(map_1\) 包含對 (cvFloor(x), cvFloor(y)),\(map_2\) 包含插值係數表中的索引。

此函式不能原地操作。

引數
src源影像。
dst目標影像。它與 `map1` 具有相同的大小,與 `src` 具有相同的型別。
map1第一個對映,可以是 (x,y) 點或僅 x 值,型別為 CV_16SC2、CV_32FC1 或 CV_32FC2。有關將浮點表示轉換為定點以提高速度的詳細資訊,請參閱 convertMaps
map2y 值的第二個對映,型別為 CV_16UC1、CV_32FC1 或無(如果 `map1` 是 (x,y) 點,則為空對映)。
interpolation插值方法(參見 InterpolationFlags)。此函式不支援 INTER_AREAINTER_LINEAR_EXACTINTER_NEAREST_EXACT 方法。可以將額外標誌 WARP_RELATIVE_MAP 與插值方法進行或運算(例如 INTER_LINEAR | WARP_RELATIVE_MAP)
borderMode畫素外推方法(參見 BorderTypes)。當 borderMode=BORDER_TRANSPARENT 時,表示目標影像中對應於源影像中“離群值”的畫素不會被函式修改。
borderValue在恆定邊界情況下使用的值。預設值為 0。
注意
由於當前的實現限制,輸入和輸出影像的大小應小於 32767x32767。

◆ resize()

void cv::resize ( InputArray src,
OutputArray dst,
Size dsize,
double fx = 0,
double fy = 0,
int interpolation = INTER_LINEAR )
Python
cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst

#include <opencv2/imgproc.hpp>

調整影像大小。

函式 `resize` 將影像 `src` 縮小或放大到指定大小。請注意,不考慮初始 `dst` 型別或大小。相反,大小和型別是從 srcdsizefxfy 派生的。如果您想調整 `src` 的大小以使其適合預先建立的 `dst`,您可以按如下方式呼叫函式

// 明確指定 dsize=dst.size();fx 和 fy 將由此計算。
resize(src, dst, dst.size(), 0, 0, interpolation);
void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)
調整影像大小。

如果您想在每個方向上將影像縮小 2 倍,您可以按以下方式呼叫函式

// 指定 fx 和 fy,讓函式計算目標影像大小。
resize(src, dst, Size(), 0.5, 0.5, interpolation);
Size2i Size
定義 types.hpp:370

縮小影像時,使用 INTER_AREA 插值通常效果最好,而放大影像時,使用 INTER_CUBIC(慢)或 INTER_LINEAR(更快但看起來仍然不錯)通常效果最好。

引數
src輸入影像。
dst輸出影像;它具有 `dsize` 大小(當它非零時)或從 `src.size()`、`fx` 和 `fy` 計算的大小;`dst` 的型別與 `src` 相同。
dsize輸出影像大小;如果它等於零(Python 中為 None),則計算為

\[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\]

`dsize` 或 `fx` 和 `fy` 必須都非零。
fx沿水平軸的比例因子;當它等於 0 時,它計算為

\[\texttt{(double)dsize.width/src.cols}\]

fy沿垂直軸的比例因子;當它等於 0 時,它計算為

\[\texttt{(double)dsize.height/src.rows}\]

interpolation插值方法,參見 InterpolationFlags
另請參閱
warpAffine, warpPerspective, remap

◆ warpAffine()

void cv::warpAffine ( InputArray src,
OutputArray dst,
InputArray M,
Size dsize,
int flags = INTER_LINEAR,
int borderMode = BORDER_CONSTANT,
const Scalar & borderValue = Scalar() )
Python
cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) -> dst

#include <opencv2/imgproc.hpp>

對影像應用仿射變換。

當設定了 WARP_INVERSE_MAP 標誌時,函式 `warpAffine` 使用指定的矩陣變換源影像

\[\texttt{dst} (x,y) = \texttt{src} ( \texttt{M} _{11} x + \texttt{M} _{12} y + \texttt{M} _{13}, \texttt{M} _{21} x + \texttt{M} _{22} y + \texttt{M} _{23})\]

否則,轉換首先使用 invertAffineTransform 反轉,然後放入上述公式中代替 M。該函式不能原地操作。

引數
src輸入影像。
dst輸出影像,其大小為 `dsize`,型別與 `src` 相同。
M\(2\times 3\) 變換矩陣。
dsize輸出影像的大小。
flags (標誌)插值方法(參見 InterpolationFlags)和可選標誌 WARP_INVERSE_MAP 的組合,表示 M 是逆變換( \(\texttt{dst}\rightarrow\texttt{src}\) )。
borderMode畫素外推方法(參見 BorderTypes);當 borderMode=BORDER_TRANSPARENT 時,表示目標影像中對應於源影像中“離群值”的畫素不會被函式修改。
borderValue在恆定邊界情況下使用的值;預設情況下為 0。
另請參閱
warpPerspective, resize, remap, getRectSubPix, transform

◆ warpPerspective()

void cv::warpPerspective ( InputArray src,
OutputArray dst,
InputArray M,
Size dsize,
int flags = INTER_LINEAR,
int borderMode = BORDER_CONSTANT,
const Scalar & borderValue = Scalar() )
Python
cv.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) -> dst

#include <opencv2/imgproc.hpp>

對影像應用透視變換。

當設定了 WARP_INVERSE_MAP 標誌時,函式 `warpPerspective` 使用指定的矩陣變換源影像

\[\texttt{dst} (x,y) = \texttt{src} \left ( \frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}} , \frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}} \right )\]

否則,轉換首先使用 `invert` 反轉,然後放入上述公式中代替 M。該函式不能原地操作。

引數
src輸入影像。
dst輸出影像,其大小為 `dsize`,型別與 `src` 相同。
M\(3\times 3\) 變換矩陣。
dsize輸出影像的大小。
flags (標誌)插值方法(INTER_LINEARINTER_NEAREST)和可選標誌 WARP_INVERSE_MAP 的組合,該標誌將 M 設定為逆變換( \(\texttt{dst}\rightarrow\texttt{src}\) )。
borderMode畫素外推方法(BORDER_CONSTANTBORDER_REPLICATE)。
borderValue在恆定邊界情況下使用的值;預設情況下為 0。
另請參閱
warpAffine, resize, remap, getRectSubPix, perspectiveTransform

◆ warpPolar()

void cv::warpPolar ( InputArray src,
OutputArray dst,
Size dsize,
Point2f center,
double maxRadius,
int flags (標誌) )
Python
cv.warpPolar(src, dsize, center, maxRadius, flags[, dst]) -> dst

#include <opencv2/imgproc.hpp>

將影像重對映到極座標或對數極座標空間。

使用以下變換變換源影像

\[ dst(\rho , \phi ) = src(x,y) \]

其中

\[ \begin{array}{l} \vec{I} = (x - center.x, \;y - center.y) \\ \phi = Kangle \cdot \texttt{angle} (\vec{I}) \\ \rho = \left\{\begin{matrix} Klin \cdot \texttt{magnitude} (\vec{I}) & default \\ Klog \cdot log_e(\texttt{magnitude} (\vec{I})) & if \; semilog \\ \end{matrix}\right. \end{array} \]

\[ \begin{array}{l} Kangle = dsize.height / 2\Pi \\ Klin = dsize.width / maxRadius \\ Klog = dsize.width / log_e(maxRadius) \\ \end{array} \]

線性與半對數對映

極座標對映可以是線性的或半對數的。將 WarpPolarMode 之一新增到 flags 以指定極座標對映模式。

線性是預設模式。

半對數對映模擬了人類的“中央凹”視覺,允許在視線(中央視覺)上具有非常高的視力,而外圍視覺的視力較差。

dsize 選項
  • 如果 dsize <= 0 中的兩個值都為零(預設值),則目標影像將具有(幾乎)與源邊界圓相同的面積

    \[\begin{array}{l} dsize.area \leftarrow (maxRadius^2 \cdot \Pi) \\ dsize.width = \texttt{cvRound}(maxRadius) \\ dsize.height = \texttt{cvRound}(maxRadius \cdot \Pi) \\ \end{array}\]

  • 如果只有 dsize.height <= 0,則目標影像面積將與邊界圓面積成比例,但按 Kx * Kx 縮放

    \[\begin{array}{l} dsize.height = \texttt{cvRound}(dsize.width \cdot \Pi) \\ \end{array} \]

  • 如果 dsize > 0 中的兩個值都大於零,則目標影像將具有給定大小,因此邊界圓的面積將縮放到 dsize
反向對映

您可以將 WARP_INVERSE_MAP 新增到 flags 以獲取反向對映

// 直接變換
warpPolar(src, lin_polar_img, Size(),center, maxRadius, flags); // 線性極座標
warpPolar(src, log_polar_img, Size(),center, maxRadius, flags + WARP_POLAR_LOG); // 半對數極座標
// 逆變換
warpPolar(lin_polar_img, recovered_lin_polar_img, src.size(), center, maxRadius, flags + WARP_INVERSE_MAP);
warpPolar(log_polar_img, recovered_log_polar, src.size(), center, maxRadius, flags + WARP_POLAR_LOG + WARP_INVERSE_MAP);

此外,為了從極座標對映座標 \((rho, phi)->(x, y)\) 計算原始座標

double angleRad, magnitude;
double Kangle = dst.rows / CV_2PI;
angleRad = phi / Kangle;
if (flags & WARP_POLAR_LOG)
{
double Klog = dst.cols / std::log(maxRadius);
magnitude = std::exp(rho / Klog);
}
else
{
double Klin = dst.cols / maxRadius;
magnitude = rho / Klin;
}
int x = cvRound(center.x + magnitude * cos(angleRad));
int y = cvRound(center.y + magnitude * sin(angleRad));
引數
src源影像。
dst目標影像。它將與 `src` 具有相同的型別。
dsize目標影像大小(參見描述中的有效選項)。
center變換中心。
maxRadius要變換的邊界圓的半徑。它也決定了逆幅度比例引數。
flags (標誌)插值方法 InterpolationFlags + WarpPolarMode 的組合。
注意
  • 此函式不能原地操作。
  • 為了計算幅度和角度(以度為單位),內部使用了 cartToPolar,因此角度測量範圍為 0 到 360 度,精度約為 0.3 度。
  • 此函式使用 remap。由於當前的實現限制,輸入和輸出影像的大小應小於 32767x32767。
另請參閱
cv::remap