题外话:一直是打算把这本书的全部课后编程题写完的,中间断了几个月,一直忙于其他事。现在开始补上。
这道题我不清楚我理解的题意是不是正确的,这道题可以练习用OpenCV实现透视变换(可以用于矫正在3维环境下的图像失真),b&c部分则是实现图像放大缩小插值,旋转图像。所有的功能都和键盘事件处理联系起来,纯OpenCV实现,和OS无关。不过,在处理SHIFT键时,我取得是其在键盘上对应字符的ASCII码,好像OpenCV对键盘事件的支持不如对鼠标事件的支持。所以SHIFT键+小键盘上的数字键是不行的。
代码:
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv_libs.h> /*
*《学习OpenCV》第四章第七题
* 完成时间:0:23 10/4 星期五 2013
*/ #define WARPSTEPSIZE 0.01 // 透视变换矩阵每次的变化率 #define RESIZESTEPSIZE 0.1 // 放大、缩小每次的变化率 #define ROTATESTEPSIZE 10 // 旋转图像时每次旋转的角度(逆时针) // 透视变换目标变换矩阵
CvPoint2D32f g_dstQuad[]; // image width & height
int g_width = ;
int g_height = ; // rotate degree
int g_RotateDegree = ; /*
* function: Zoom in or zoom out an image.
* param: inorout -- indicate in or out(0 means zoom in; 1 means zoom out)
* param: the destination size.
*/
CvSize getZoomDstSize(int inorout)
{
if(inorout != && inorout != )
{
return cvSize(-, -);
}
if(g_height == || g_width == )
{
return cvSize(-, -);
} // Zoom in
if(inorout == )
{
g_width += g_width * RESIZESTEPSIZE;
g_height += g_height * RESIZESTEPSIZE;
}
else if(inorout == )
{
g_width -= g_width * RESIZESTEPSIZE;
g_height -= g_height * RESIZESTEPSIZE; if(g_height < )
{
g_height = ;
}
else if(g_width < )
{
g_width = ;
}
} return cvSize(g_width, g_height);
} void rotateImage(IplImage* img, IplImage *img_rotate,float degree)
{
// 旋转中心
CvPoint2D32f center;
center.x=float (img->width/2.0+0.5);
center.y=float (img->height/2.0+0.5);
// 计算二维旋转的仿射变换矩阵
float m[];
CvMat M = cvMat( , , CV_32F, m );
cv2DRotationMatrix( center, degree,, &M);
// 变换图像,并用黑色填充其余值
cvWarpAffine(img,img_rotate, &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll() );
} /*
* function: change the g_dstQuad(minus or plus one of the element)
* param: index -- which element(0 -- 4)
* param: xory -- indicate x or y to change(0 means x; 1 means y)
* param: operation -- indicate which operation(0 means plus; 1 means minus)
* return: 0 means success. -1 means failed.
*/
int changeg_dstQuad(int index, int xory, int operation)
{
// param check
if( index > || index < ||
(xory != && xory != ) ||
(operation != && operation != ) ||
g_width == || g_height == )
{
return -;
} // plus
if(operation == )
{
if(xory == ) // x
{
g_dstQuad[index].x += WARPSTEPSIZE * g_width;
if(g_dstQuad[index].x > g_width - )
{
g_dstQuad[index].x = g_width - ;
}
}
else if(xory == ) // y
{
g_dstQuad[index].y += WARPSTEPSIZE * g_height;
if(g_dstQuad[index].y > g_height - )
{
g_dstQuad[index].y = g_height -;
}
}
} // minus
else if (operation == )
{
if(xory == ) // x
{
g_dstQuad[index].x -= WARPSTEPSIZE * g_width;
if(g_dstQuad[index].x < )
{
g_dstQuad[index].x = ;
}
}
else if(xory == ) // y
{
g_dstQuad[index].y -= WARPSTEPSIZE * g_height;
if(g_dstQuad[index].y < )
{
g_dstQuad[index].y = ;
}
}
} return ;
} int main()
{
const char * FILEPATH = "lena.bmp";
IplImage * src = cvLoadImage(FILEPATH, CV_LOAD_IMAGE_UNCHANGED); if(!src)
{
printf("load image error.\texit\n");
return -;
} CvPoint2D32f srcQuad[];
CvMat* warp_matrix = cvCreateMat(, , CV_32FC1);
IplImage *dst; cvNamedWindow("Source_Image", );
cvNamedWindow("Perspective_Warp", ); dst = cvCloneImage(src);
dst->origin = dst->origin;
cvZero(dst); // image width & height
g_width = src->width;
g_height = src->height; srcQuad[].x = ; // src Top left
srcQuad[].y = ;
srcQuad[].x = g_width - ; // src Top right
srcQuad[].y = ;
srcQuad[].x = ; // src Bottom left
srcQuad[].y = g_height - ;
srcQuad[].x = g_width - ; // src Bottom right
srcQuad[].y = g_height - ; g_dstQuad[].x = ; // dst Top left
g_dstQuad[].y = ;
g_dstQuad[].x = g_width - ; // dst Top right
g_dstQuad[].y = ;
g_dstQuad[].x = ; // dst Bottom left
g_dstQuad[].y = g_height - ;
g_dstQuad[].x = g_width - ; // dst Bottom right
g_dstQuad[].y = g_height - ; while()
{
cvShowImage("Source_Image", src);
char c = cvWaitKey(); int ret = ;
switch(c)
{
// ESC
case :
goto end;
break;
// 0 -- 放大图像
case :
{
cvNamedWindow("Resize", );
CvSize dstSize = getZoomDstSize();
if(dstSize.height == - || dstSize.width == -)
{
goto end;
} IplImage* imageresize = cvCreateImage(dstSize, src->depth, src->nChannels);
if(!imageresize)
{
goto end;
}
cvResize(src, imageresize, CV_INTER_LINEAR);
cvShowImage("Resize", imageresize);
cvReleaseImage(&imageresize);
}
break;
// SHIFT + 0 ')' -- 缩小图像
case :
{
cvNamedWindow("Resize", );
CvSize dstSize = getZoomDstSize();
if(dstSize.height == - || dstSize.width == -)
{
goto end;
} IplImage* imageresize = cvCreateImage(dstSize, src->depth, src->nChannels);
if(!imageresize)
{
goto end;
}
cvResize(src, imageresize, CV_INTER_LINEAR);
cvShowImage("Resize", imageresize);
cvReleaseImage(&imageresize);
}
break;
// 9 -- 旋转图像(逆时针)
case :
{
cvNamedWindow("Rotate", ); IplImage* imagerotate = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
if(!imagerotate)
{
goto end;
}
g_RotateDegree += ROTATESTEPSIZE;
rotateImage(src, imagerotate, g_RotateDegree);
cvShowImage("Rotate", imagerotate);
cvReleaseImage(&imagerotate);
}
break;
// SHIFT + 9 '(' -- 旋转图像(顺时针)
case :
{
cvNamedWindow("Rotate", ); IplImage* imagerotate = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
if(!imagerotate)
{
goto end;
}
g_RotateDegree -= ROTATESTEPSIZE;
rotateImage(src, imagerotate, g_RotateDegree);
cvShowImage("Rotate", imagerotate);
cvReleaseImage(&imagerotate);
}
break;
//
case :
ret = changeg_dstQuad(, , );
break;
//
case :
ret = changeg_dstQuad(, , );
break;
//
case :
ret = changeg_dstQuad(, , );
break;
//
case :
ret = changeg_dstQuad(, , );
break;
//
case :
ret = changeg_dstQuad(, , );
break;
//
case :
ret = changeg_dstQuad(, , );
break;
//
case :
ret = changeg_dstQuad(, , );
break;
//
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 1 '!'
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 2 '@'
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 3 '#'
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 4 '$'
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 5 '%'
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 6 '^'
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 7 '&'
case :
ret = changeg_dstQuad(, , );
break;
// SHIFT + 8 '*'
case :
ret = changeg_dstQuad(, , );
break; default:
break;
} // Error
if(ret != )
{
goto end;
}
cvGetPerspectiveTransform(srcQuad, g_dstQuad, warp_matrix);
cvWarpPerspective( src, dst, warp_matrix,
CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll()); cvShowImage("Source_Image", src);
cvShowImage("Perspective_Warp", dst);
} end:
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseMat(&warp_matrix);
cvDestroyAllWindows(); return ;
}
程序运行截图:
首先是透视变换:
图像放大缩小(b部分):
图像旋转(c部分):