OPenCV版本:4.4
IDE:VS2019
将图像重新映射到极坐标或半对数极坐标空间,这个函数用于实现图像的极坐标变换。
使用以下变换来转换图像:
d s t ( ρ , ϕ ) = s r c ( x , y ) dst(\rho , \phi ) = src(x,y) dst(ρ,ϕ)=src(x,y)
此处:
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
并且:
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)
线性与半对数映射
极坐标映射可以是线性或半对数,添加WarpPolarMode中的一个到flags来确定极坐标映射模式,。
线性是缺省模式。
半对数映射模拟人类的“中心凹”视觉,允许在视线(中心视觉)上具有非常高的锐度,而周围视觉的锐度较小。
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);
在编程中,通过(ρ,φ)−>(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));
void cv::warpPolar ( InputArray src,
OutputArray dst,
Size dsize,
Point2f center,
double maxRadius,
int flags
)
Note
另请参见
cv::remap
#include
#include
#include
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;
}