当前位置:文档之家› 实验4 图像几何变换 哈哈镜制作

实验4 图像几何变换 哈哈镜制作


x0 r cos b y0 r sin b
旋转 a 角度后:
(4)
x1 r cos b a r cos b cos a r sin b sin a x0 cona y0 sin a y1 r sin b a r sin b cos a r cos b sin a x0 sin a y0 cos a
x0 1 c 0 y0 0 1 d 1 0 0
0 x1 0 y1 1 1
(9)
三.实验仪器
1. 计算机; 2. VC++程序; 3. 移动式存储器(软盘、U 盘等) 。 4. 记录用的笔、纸。
以矩阵的形式表示为:
(5)
yⅡ r
(x0, y0) (x1, y1) r a b xⅡ
O 图 2 旋转示意图
x1 cos a sin a 0 x0 y1 sin a cos a 0 y0 1 0 0 1 1
b=(bUpLeft*(1-float_srcX)*(1-float_srcY)+bUpRight*float_srcX*(1-float_srcY)+ bDownLeft*(1-float_srcX)*float_srcY+bDownRight*float_srcX*float_srcY); g=(gUpLeft*(1-float_srcX)*(1-float_srcY)+gUpRight*float_srcX*(1-float_srcY)+ gDownLeft*(1-float_srcX)*float_srcY+gDownRight*float_srcX*float_srcY); r=(rUpLeft*(1-float_srcX)*(1-float_srcY)+rUpRight*float_srcX*(1-float_srcY)+ rDownLeft*(1-float_srcX)*float_srcY+rDownRight*float_srcX*float_srcY); if(int_srcY>=0 && int_srcY<=pImg->height*2 && int_srcX>=0 && int_srcX<=pImg->width*2) { pImg1->imageData[i*pImg1->widthStep+j*3+0]=b; pImg1->imageData[i*pImg1->widthStep+j*3+1]=g; pImg1->imageData[i*pImg1->widthStep+j*3+2]=r; } } break; // 水平外凹 case HORAO: pImg1 = cvCreateImage(cvGetSize(pImg),pImg->depth,pImg->nChannels); for(i=0;i<pImg1->height;i++) { tmp = RANGE*sin(i*PI/pImg1->height); for(j=tmp;j<pImg1->width-tmp;j++) { int s=(int)((j-tmp)*(pImg->width)/(pImg->width-2*tmp)); for(int k=0;k<pImg->nChannels;k++) { pImg1->imageData[i*pImg1->widthStep+j*pImg->nChannels+k]=pImg->imageData[i*pImg->widthS tep+s*pImg->nChannels+k]; } } } break; // 水平外凸 case HORTU: pImg1 = cvCreateImage(cvGetSize(pImg),pImg->depth,pImg->nChannels); for(i=0;i<pImg1->height;i++)
任课教师:曹丽
《数字图像处理》
(2012-2013 学年第 2 学期)
实 验 报 告
学号:E10640204 姓名: 张慧 班级:10 电科(2)班
实验 4 图像几何变换—哈哈镜制作
一. 实验目的 熟悉图像的基本格式和数据结构。掌握图像几何变换的原理。 二.实验原理
1. 图像平移 将图像中所有的点都按照指定的平移量水平、垂直移动。设(x0, y0)是原图像上的一点, 图像水平平移量为 tx,垂直平移量为 ty,则平移后点(x0, y0)的坐标变为(x1, y1)。 (x0, y0)与(x1, y1)之间的关系为:
(6)
(6)式中,坐标系是以图像的中心为原点,向右为 x 轴正方向,向上为 y 轴正方向。它和 以图像左上角为原点,向右为 x 轴正方向,向下为 y 轴正方向的坐标系之间的转换关系如图 3 所示。
OⅠ (x, y) OⅡ yⅠ 图 3 两种坐标系间的转换关系图 yⅡ xⅠ
xⅡ
设图像的宽度为 w,高度为 h,容易得到:
//载入图像 pImg = cvLoadImage( "gg.bmp", 1); cvNamedWindow( "Image", 1 );//创建窗口 cvShowImage( "Image", pImg );//显示图像 printf("%d,%d",pImg->width,pImg->height); switch(method) { // 最邻近插值图像缩小 case DOWNRESIZE: size = cvSize(q*pImg->width,q*pImg->height); pImg1 = cvCreateImage(size,pImg->depth,pImg->nChannels); for(i=0;i<pImg1->height;i++) for(j=0;j<pImg1->width;j++) { float srcX=(float)(j*((float)pImg->width/(float)pImg1->width)); float srcY=(float)(i*((float)pImg->height/(float)pImg1->height)); int int_srcX=(int)srcX; int int_srcY=(int)srcY; for(int k=0;k<pImg1->nChannels;k++)
{ pImg1->imageData[i*pImg1->widthStep+j*pImg1->nChannels+k]=(uchar)pImg->imageData[int_sr cY*pImg->widthStep+int_srcX*pImg->nChannels+k]; } } break; // 双线性插值图像放大 case UPRESIZE: size=cvSize(z*pImg->width,z*pImg->height); pImg1 = cvCreateImage(size,pImg->depth,pImg->nChannels); for(i=0;i<pImg1->height;i++) for(j=0;j<pImg1->width;j++) { float srcX=(float)(j*((float)pImg->width/(float)pImg1->width)); float srcY=(float)(i*((float)pImg->height/(float)pImg1->height)); int int_srcX=(int)srcX; int int_srcY=(int)srcY; float float_srcX=srcX-int_srcX; float float_srcY=srcY-int_srcY; unsigned char bUpLeft, bUpRight, bDownLeft, bDownRight; unsigned char gUpLeft, gUpRight, gDownLeft, gDownRight; unsigned char rUpLeft, rUpRight, rDownLeft, rDownRight; unsigned char b, g, r; bUpLeft=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+0]; bUpRight=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+0]; bDownLeft=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+0]; bDownRight=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+0]; gUpLeft=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+1]; gUpRight=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+1]; gDownLeft=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+1]; gDownRight=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+1]; rUpLeft=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+2]; rUpRight=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+2]; rDownLeft=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+2]; rDownRight=pImg->imageData[int_srcY*pImg->widthStep+int_srcX*3+2];
相关主题