本节介绍使用OpenCV 1.x如使 图像映射到极坐标实现。
在opencv中,函数cvLogPolar功能是将图像映射到极坐标。
格式
/* Performs forward or inverse log-polar image transform */ CVAPI(void) cvLogPolar( const CvArr* src, CvArr* dst, CvPoint2D32f center, double M, int flags CV_DEFAULT(CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS));参数
说明
函数cvLogPolar使用如下变换公式对源图像进行变换。
其中, rho =M*log[sqrt(x2+y2)], phi = atan(y/x).
此函数可以用来模拟人类的中央视觉(foveal vision),并可以用于物体跟踪方面的尺度及旋转不变模板的快速匹配。
IplImage *ImagePixel::ImageLogPolar(IplImage* pImg,int flags){ if(!pImg){ printf("Error:Load File in ImageLogPolar(..).\n"); exit(EXIT_FAILURE); } nWidth = pImg->width; nHeight = pImg ->height; IplImage* dst = cvCreateImage(cvGetSize(pImg),pImg->depth,pImg->nChannels); cvLogPolar(pImg,dst,cvPoint2D32f(nWidth/2,nHeight/2),40,flags); return dst; }
(a) 原始图像 (b) Log-Polar正变换 (b) Log-Polar 反变换
import sys def on_mouse( event, x, y, flags, param ): if( not src ): return; if event==CV_EVENT_LBUTTONDOWN: cvLogPolar(src, dst, cvPoint2D32f(x,y), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS ); cvLogPolar(dst, src2, cvPoint2D32f(x,y), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS+CV_WARP_INVERSE_MAP ); cvShowImage( "log-polar", dst ); cvShowImage( "inverse log-polar", src2 ); if __name__ == "__main__": filename = "test.jpg" if len(sys.argv)>1: filename=argv[1] src = cvLoadImage(filename,1) if not src: print "Could not open %s" % filename sys.exit(-1) cvNamedWindow( "Original",0 ); cvNamedWindow( "Log-polar",0 ); cvNamedWindow( "inverse log-polar", 0 ); dst = cvCreateImage( cvSize(256,256), 8, 3 ); src2 = cvCreateImage( cvGetSize(src), 8, 3 ); cvSetMouseCallback( "original", on_mouse ); on_mouse( CV_EVENT_LBUTTONDOWN, src.width/2, src.height/2, None, None) cvShowImage( "original", src ); cvWaitKey();
关于Image Engineering & Computer Vision的更多讨论与交流,敬请关注本博和新浪微博songzi_tea.