原理见图:
代码如下:
void Ctry::OnTryTyr1()
{
//TODO: 在此添加命令处理程序代码
IplImage *img = cvLoadImage("C:\\Users\\Administrator\\Desktop\\3838.jpg");
IplImage *dst = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);
int i, j; //目标图像坐标
int u, v; //源图像坐标
int ang = 30; //旋转角度
CvScalar pixel;
for (i = 0; i < dst->width; i++)
{
for ( j = 0; j < dst->height; j++)
{
u = int(i*cos(ang*PI / 180) + j*sin(ang*PI / 180) + 0.5);
v = int(j*cos(ang*PI / 180) - i*sin(ang*PI / 180) + 0.5);
if ((u < img->width)&&(v < img->height)&&(u >= 0) && (v >= 0))
{
pixel = cvGet2D(img, v, u);
cvSet2D(dst, j, i, pixel);
}
else
{
cvSet2D(dst, j, i, RGB(0,0,0));
}
}
}
cvSaveImage("C:\\Users\\Administrator\\Desktop\\dst.jpg",dst);
cvNamedWindow(" dst", CV_WINDOW_AUTOSIZE);
cvShowImage(" dst", dst);
cvNamedWindow("img", CV_WINDOW_AUTOSIZE);
cvShowImage("img", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&dst);
cvDestroyWindow(" dst");
cvDestroyWindow("img");
}
效果如图: