采用opencv库函数进行扩展:
void cvCopyMakeBorder( const CvArr* src, CvArr* dst, CvPoint offset, intbordertype, CvScalar value=cvScalarAll(0));注意参数:bordertype:IPL_BORDER_REFLECT ,IPL_BORDER_CONSTANT。
以图像边缘为扩展则采用IPL_BORDER_REFLECT,参数value可以忽略。
以指定的填充颜色扩展采用IPL_BORDER_CONSTANT,则使用value设置填充颜色,不设置默认为黑色。
#include <cv.h> #include <cxcore.h> #include <highgui.h> extern "C"{ #include<stdio.h> #include<stdlib.h> } #define WID 1280 #dedine HIG 800 IplImage *src; IplImage *extendimg; IplImage *dst; int main(int argc,char **argv) { int width=0,height=0; int exwidth=0,exheight=0,exchannels=0; CvRect zone; if(argv[1]==NULL){ printf("usage:program picture\n"); return 0; } src=cvLoadImage(argv[1],1);//1为读取彩色图,0为强制转化为灰度图,-1为默认读取图像的原通道数。 if(!src){ printf("can not find files,please check the path...!"); return 0; } cvNamedWindow("source",1); cvShowImage("source",src); width=src->width; height=src->height; if(width%WID!=0) exwidth=width+WID-width%WID; else exwidth=width; if(height%HIG!=0) exheight=height+HIG-height%HIG; else exheight=height; /*图片扩展*/ //exchannels=src->nChannels; exchannels=3; extendimg=cvCreateImage(cvSize(exwidth,exheight),IPL_DEPTH_8U,exchannels); cvCopyMakeBorder(src,extendimg,cvPoint(1,1),IPL_BORDER_CONSTANT,cvScalar(0,0,0)); printf("origal:%d*%d\nafter extend:%d*%d\nchannels:%d\n",width,height,exwidth,exheight,extendimg->nChannels); cvNamedWindow("externimg",1); cvShowImage("externimg",extendimg); while(y_coodinate<exheight){ while(x_coordinate<exwidth){ zone=cvRect(x_coordinate,y_coodinate,WID,HIG); cvSetImageROI(extendimg,zone); dst=cvCreateImage(cvSize(WID,HIG),IPL_DEPTH_8U,src->nChannels); cvCopy(extendimg,dst,0); cvResetImageROI(extendimg); cvNamedWindow(savename,1); cvShowImage(savename,dst); x_coordinate=x_coordinate+WID; flag++; } y_coodinate=y_coodinate+HIG; x_coordinate=0; } cvWaitKey(0); cvReleaseImage(&src); cvReleaseImage(&extendimg); cvReleaseImage(&dst); return 0; }