当前位置:网站首页>Use of OpenCV polar transformation function warppolar
Use of OpenCV polar transformation function warppolar
2022-07-19 08:21:00 【jndingxin】
OPenCV edition :4.4
IDE:VS2019
Function description
Remap the image to polar or semi logarithmic polar space , This function is used to realize the polar transformation of the image .
Use the following transformations to convert images :
d s t ( ρ , ϕ ) = s r c ( x , y ) dst(\rho , \phi ) = src(x,y) dst(ρ,ϕ)=src(x,y)
here :
I ⃗ = ( x − c e n t e r . x , y − c e n t e r . y ) ϕ = K a n g l e ⋅ angle ( I ⃗ ) ρ = { K l i n ⋅ magnitude ( I ⃗ ) d e f a u l t K l o g ⋅ l o g e ( magnitude ( I ⃗ ) ) i f s e m i l o g \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} I=(x−center.x,y−center.y)ϕ=Kangle⋅angle(I)ρ={ Klin⋅magnitude(I)Klog⋅loge(magnitude(I))defaultifsemilog
also :
K a n g l e = d s i z e . h e i g h t / 2 Π K l i n = d s i z e . w i d t h / m a x R a d i u s K l o g = d s i z e . w i d t h / l o g e ( m a x R a d i u s ) \begin{array}{l} Kangle = dsize.height / 2\Pi \\ Klin = dsize.width / maxRadius \\ Klog = dsize.width / log_e(maxRadius) \\ \end{array} Kangle=dsize.height/2ΠKlin=dsize.width/maxRadiusKlog=dsize.width/loge(maxRadius)
Linear and semilogarithmic mapping
Polar mapping can be linear or semilogarithmic , add to WarpPolarMode One of them arrives flags To determine the polar mapping mode ,.
Linear is the default mode .
Semilogarithmic mapping simulates human “ Fovea ” Vision , Allow in sight ( Central vision ) It has a very high sharpness , The sharpness of the surrounding vision is smaller .
dsize The option to :
- If dsize Both values in <=0( Default ), The target image will have ( almost ) The same source boundary circle area
d s i z e . a r e a ← ( m a x R a d i u s 2 ⋅ Π ) d s i z e . w i d t h = cvRound ( m a x R a d i u s ) d s i z e . h e i g h t = cvRound ( m a x R a d i u s ⋅ Π ) \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.area←(maxRadius2⋅Π)dsize.width=cvRound(maxRadius)dsize.height=cvRound(maxRadius⋅Π) - If it's just dsize.height <= 0, The target image area will be proportional to the boundary circle area Kx * Kx: The zoom .
d s i z e . h e i g h t = cvRound ( d s i z e . w i d t h ⋅ Π ) \begin{array}{l} dsize.height = \texttt{cvRound}(dsize.width \cdot \Pi) \\ \end{array} dsize.height=cvRound(dsize.width⋅Π) - If dsize The values of all members are > 0, The target image will have a given size , Therefore, the area of the boundary circle will be scaled to dsize.
Reverse mapping
You can add WARP_INVERSE_MAP To flags Get reverse mapping .
// Direct transformation
warpPolar(src, lin_polar_img, Size(),center, maxRadius, flags);
// Linear polar coordinates
warpPolar(src, log_polar_img, Size(),center, maxRadius, flags + WARP_POLAR_LOG);
// Semi logarithmic polar coordinates
// Reverse transformation
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);
In programming , adopt (ρ,φ)−>(x,y) Calculate the original coordinates from polar coordinates :
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));
The function prototype
void cv::warpPolar ( InputArray src,
OutputArray dst,
Size dsize,
Point2f center,
double maxRadius,
int flags
)
Parameters
- src The source image .
- dst Target image , The type and src identical .
- dsize Target image size (see description for valid options).
- center Conversion center The transformation center.
- maxRadius The radius of the boundary circle to be transformed , It also determines the inverse magnitude proportional parameter .
flags A combination of interpolation methods , InterpolationFlags + WarpPolarMode. - add to WARP_POLAR_LINEAR Select linear polar mapping ( Default )
- add to WARP_POLAR_LOG Select semi logarithmic polar mapping .
- add to WARP_INVERSE_MAP Select reverse mapping
Note
- This function does not support in place conversion .
- To calculate amplitude and angle , For internal use cartToPolar, Therefore, the measurement range of the angle is 0 To 360 degree , The accuracy is about 0.3 degree .
- This function uses remap. Due to the limitations of the current implementation , The size of input and output images should be less than 32767x32767.
See also
cv::remap
Sample code
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture capture;
Mat log_polar_img, lin_polar_img, recovered_log_polar, recovered_lin_polar_img;
CommandLineParser parser(argc, argv, "{@input|0| camera device number or video file path}");
parser.about("\nThis program illustrates usage of Linear-Polar and Log-Polar image transforms\n");
parser.printMessage();
std::string arg = parser.get<std::string>("@input");
//if (arg.size() == 1 && isdigit(arg[0]))
// capture.open(arg[0] - '0');
//else
// capture.open(samples::findFileOrKeep(arg));
capture.open("D:\\OpenCVtest\\video1.mp4");
if (!capture.isOpened())
{
fprintf(stderr, "Could not initialize capturing...\n");
return -1;
}
namedWindow("Linear-Polar", WINDOW_AUTOSIZE);
namedWindow("Log-Polar", WINDOW_AUTOSIZE);
namedWindow("Recovered Linear-Polar", WINDOW_AUTOSIZE);
namedWindow("Recovered Log-Polar", WINDOW_AUTOSIZE);
moveWindow("Linear-Polar", 20, 20);
moveWindow("Log-Polar", 700, 20);
moveWindow("Recovered Linear-Polar", 20, 350);
moveWindow("Recovered Log-Polar", 700, 350);
int flags = INTER_LINEAR + WARP_FILL_OUTLIERS;
Mat src;
for (;;)
{
capture >> src;
if (src.empty())
break;
Point2f center((float)src.cols / 2, (float)src.rows / 2);
double maxRadius = 0.7 * min(center.y, center.x);
#if 0 //deprecated
double M = frame.cols / log(maxRadius);
logPolar(frame, log_polar_img, center, M, flags);
linearPolar(frame, lin_polar_img, center, maxRadius, flags);
logPolar(log_polar_img, recovered_log_polar, center, M, flags + WARP_INVERSE_MAP);
linearPolar(lin_polar_img, recovered_lin_polar_img, center, maxRadius, flags + WARP_INVERSE_MAP);
#endif
// direct transform
warpPolar(src, lin_polar_img, Size(), center, maxRadius, flags); // linear Polar
warpPolar(src, log_polar_img, Size(), center, maxRadius, flags + WARP_POLAR_LOG); // semilog Polar
// inverse transform
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);
// Below is the reverse transformation for (rho, phi)->(x, y) :
Mat dst;
if (flags & WARP_POLAR_LOG)
dst = log_polar_img;
else
dst = lin_polar_img;
//get a point from the polar image
int rho = cvRound(dst.cols * 0.75);
int phi = cvRound(dst.rows / 2.0);
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));
drawMarker(src, Point(x, y), Scalar(0, 255, 0));
drawMarker(dst, Point(rho, phi), Scalar(0, 255, 0));
imshow("Src frame", src);
imshow("Log-Polar", log_polar_img);
imshow("Linear-Polar", lin_polar_img);
imshow("Recovered Linear-Polar", recovered_lin_polar_img);
imshow("Recovered Log-Polar", recovered_log_polar);
if (waitKey(10) >= 0)
break;
}
return 0;
}
Running results
Original picture :
Linear polar mapping :
Semilogarithmic polar mapping 
Restored linear polar mapping :
Restored semilog polar mapping 
边栏推荐
- Address monitoring API: how to trace and monitor uniswap hacker addresses
- With this "programmer code interview guide" from Zuo Chengyun (Zuo Shen), I joined byte
- 经典通用的Pbootcms花卉网站模板源码,自适应手机端,带后台管理
- be vigilant! Another phishing attack: uniswap stolen $8.1 million
- Obtain the home location through IP
- 面试题:外边距折叠问题 (块级元素在普通文档流中的BUG)
- openpyxl跨工作簿复制sheet页
- By voting for the destruction of STI by Dao, seektiger is truly community driven
- Visual studio production environment configuration scheme: slowcheetah
- 【Kernel】驱动开发学习之字符设备
猜你喜欢

Classic general pbootcms flower website template source code, adaptive mobile terminal, with background management

Complete square number

面试题:外边距折叠问题 (块级元素在普通文档流中的BUG)

DP动态规划企业级模板分析(数字三角,上升序列,背包,状态机,压缩DP)

Practice of online problem feedback module (V): realize the automatic filling function of general field content

Discussion on risc-v Technology

3D激光SLAM:ALOAM---帧间里程计代码解读

Database review -- database recovery technology

写代码遇到Qt相关问题

Demo collection injection
随机推荐
行为型模式之策略模式
the max_ iter was reached which means the coef_ did not converge “the coef_ did not converge“
DP dynamic planning enterprise level template analysis (Digital triangle, rising sequence, knapsack, state machine, compressed DP)
Bean、
sudo pip install gevent 安装失败的解决办法
LeetCode 每日一题 2021/7/11-2021/7/17
Semiconductor material technology
Unity: WebGL发布后在浏览器上运行时窗口大小自适应
如何将读取列表中的str转化为float
History and value of forked coins | eth, BCH, BSV 2020-03-08
QT related problems encountered when writing code
WVPPRO-ZLM-GB21818-摄像头
数据库写入优化:分库分表及相关问题
812. Maximum triangle area
Textview text up and down
The core problem of concurrent programming
依赖注入方式
[C # variable constant keyword] - variable constants and keywords in C #
Is it necessary to buy pension insurance? What are the pension products suitable for the elderly?
[C language] user defined type details: structure, enumeration, union