OpenCV  4.1.2
Open Source Computer Vision
samples/cpp/polar_transforms.cpp

An example using the cv::linearPolar and cv::logPolar operations

#include <iostream>
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
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 );
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;
}
cv::WARP_FILL_OUTLIERS
Definition: imgproc.hpp:263
cv::Point_< float >
cv::moveWindow
void moveWindow(const String &winname, int x, int y)
Moves window to the specified position.
cv::sin
softdouble sin(const softdouble &a)
Sine.
cv::CommandLineParser::get
T get(const String &name, bool space_delete=true) const
Access arguments by name.
Definition: utility.hpp:898
cv::WARP_INVERSE_MAP
Definition: imgproc.hpp:270
cv::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:603
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::Point_::y
_Tp y
y coordinate of the point
Definition: types.hpp:187
cv::exp
softfloat exp(const softfloat &a)
Exponent.
cv::logPolar
void logPolar(InputArray src, OutputArray dst, Point2f center, double M, int flags)
Remaps an image to semilog-polar coordinates space.
cv::Point_::x
_Tp x
x coordinate of the point
Definition: types.hpp:186
CV_2PI
#define CV_2PI
Definition: cvdef.h:321
cv::linearPolar
void linearPolar(InputArray src, OutputArray dst, Point2f center, double maxRadius, int flags)
Remaps an image to polar coordinates space.
highgui.hpp
cv::min
softfloat min(const softfloat &a, const softfloat &b)
Min and Max functions.
Definition: softfloat.hpp:437
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::Size
Size2i Size
Definition: types.hpp:347
cv::INTER_LINEAR
Definition: imgproc.hpp:248
cv::cos
softdouble cos(const softdouble &a)
Cosine.
cv::Mat::size
MatSize size
Definition: mat.hpp:2108
cv::magnitude
void magnitude(InputArray x, InputArray y, OutputArray magnitude)
Calculates the magnitude of 2D vectors.
cv::VideoCapture::isOpened
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
cvRound
int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition: fast_math.hpp:197
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
cv::Point
Point2i Point
Definition: types.hpp:194
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:791
cv::samples::findFileOrKeep
cv::String findFileOrKeep(const cv::String &relative_path, bool silentMode=false)
Definition: utility.hpp:1281
cv::CommandLineParser::about
void about(const String &message)
Set the about message.
cv::warpPolar
void warpPolar(InputArray src, OutputArray dst, Size dsize, Point2f center, double maxRadius, int flags)
Remaps an image to polar or semilog-polar coordinates space.
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:831
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
imgproc.hpp
cv::CommandLineParser::printMessage
void printMessage() const
Print help message.
cv::WARP_POLAR_LOG
Remaps an image to/from semilog-polar space.
Definition: imgproc.hpp:279
cv::WINDOW_AUTOSIZE
the user cannot resize the window, the size is constrainted by the image displayed.
Definition: highgui.hpp:184
cv::log
softfloat log(const softfloat &a)
Natural logarithm.
videoio.hpp
cv::drawMarker
void drawMarker(InputOutputArray img, Point position, const Scalar &color, int markerType=MARKER_CROSS, int markerSize=20, int thickness=1, int line_type=8)
Draws a marker on a predefined position in an image.
cv::VideoCapture::open
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.