核心函数:cvRemap
原理:
dst(x,y)<-src(mapx(x,y),mapy(x,y))
如果src(mapx(x,y),mapy(x,y))在src中不存在,那么用0代替
程序:
#include "cv.h" #include "cxcore.h" #include "highgui.h" #include <iostream> int Remap(int argc,char** argv) { uchar DataSrc[]={1,2,3, 4,5,6, 7,8,9 }; CvMat MatSrc; cvInitMatHeader(&MatSrc,3,3,CV_8UC1,DataSrc); //初始化矩阵 CvMat *MatDst=cvCreateMat(3,3,CV_8UC1); //创建矩阵 //cvInitMatHeader(&MatDst,3,3,CV_8UC1); float DataMapx[]={1,2,0, //cvRemap函数的mapx一定要为float型 1,2,0, 1,2,0 }; CvMat mapx; cvInitMatHeader(&mapx,3,3,CV_32FC1,DataMapx); //注意类型是CV_32FC1 float DataMapy[]={0,0,1, 1,1,2, 2,2,0 }; CvMat mapy; cvInitMatHeader(&mapy,3,3,CV_32FC1,DataMapy); cvRemap(&MatSrc,MatDst,&mapx,&mapy,CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS); //打印原矩阵 std::cout<<"MatSrc=\‘#\‘" //打印mapx std::cout<<"mapx:"<<std::endl; for(int rows=0;rows<mapx.height;rows++) { float *DataPt=(float*)(mapx.data.ptr+rows*mapx.step); for(int cols=0;cols<mapx.width;cols++) { float data=*(DataPt+cols); std::cout<<(float)data<<" "; } std::cout<<std::endl; } //打印mapy std::cout<<"mapy:"<<std::endl; for(int rows=0;rows<mapy.height;rows++) { float *DataPt=(float*)(mapy.data.ptr+rows*mapy.step); for(int cols=0;cols<mapy.width;cols++) { float data=*(DataPt+cols); std::cout<<(float)data<<" "; } std::cout<<std::endl; } //打印结果矩阵 std::cout<<"MatDst:"<<std::endl; for(int rows=0;rows<MatDst->height;rows++) { uchar *DataPt=(uchar*)(MatDst->data.ptr+rows*MatDst->step); for(int cols=0;cols<MatDst->width;cols++) { uchar data=*(DataPt+cols); std::cout<<(int)data<<" "; } std::cout<<std::endl; } return 0; }
本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539817
原文地址:http://flyclc.blog.51cto.com/1385758/1539817