采用的是最邻近插值,代码如下:
void Ctry::OnTryTyr1()
{
//TODO: 在此添加命令处理程序代码
IplImage *img = cvLoadImage("C:\\Users\\Administrator\\Desktop\\3838.jpg");
double times = 0.8; //比例因子
int nWidth = times * (img->width);
int nHeight = times * (img->height);
IplImage *dst = cvCreateImage(cvSize(nWidth, nHeight), 8, 3);
CvScalar pixel;
for (int i = 0; i < dst->width; i++)
{
for (int j = 0; j < dst->height; j++)
{
if (int(i * 1 / times + 0.5) < img->width && int(j * 1 / times + 0.5) < img->height)
{
pixel = cvGet2D(img, int(j * 1 / times + 0.5), int(i * 1 / times + 0.5));
cvSet2D(dst, j, i, pixel);
}
else
{
cvSet2D(dst, j, i, RGB(0,0,0,));
}
}
}
cvSaveImage("C:\\Users\\Administrator\\Desktop\\TransPose.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");
}
效果图: