4,448
社区成员
发帖
与我相关
我的任务
分享
// ImageWarping.cpp : 定义控制台应用程序的入口点。
//程序用来处理图片的变换问题
#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void RotateImage(IplImage* img, IplImage* img_rotate, int degree);
int main()
{
IplImage* IplI = cvLoadImage("Monkey.jpg", 1);
if (IplI == NULL)
{
fprintf(stderr, "Can not load image.\n");
return -1;
}
IplImage* IplI_rotate = cvCreateImage(IplI->nSize, IplI->depth, IplI->nChannels);
RotateImage(IplI, IplI_rotate, 30);
//显示图像
cvNamedWindow("Initial Image");
cvNamedWindow("Rotated Image");
cvShowImage("Initial Image", IplI);
cvShowImage("Rotated Image", IplI_rotate);
//cvSaveImage("F:\\C++\\opencv\\Figures\\Monkey1.jpg", IplI);
cvWaitKey();
cvReleaseImage(&IplI);
cvReleaseImage(&IplI_rotate);
cvDestroyWindow("Initial Image");
return 0;
}
//图像旋转变换函数
void RotateImage(IplImage* img, IplImage* img_rotate, int degree)
{
//确定旋转中心,此处为图像中心
CvPoint2D32f center;
center.x = float(img->width / 2.0 + 0.5);
center.y = float(img->height / 2.0 + 0.5);
//计算二维旋转的仿射变换矩阵
float m[6];
CvMat M = cvMat(2, 3, CV_32F, m); // 将m转换为2*3的矩阵
cv2DRotationMatrix(center, degree, 1, &M); //计算旋转degree情况下的仿射矩阵
//仿射变换函数
//CV_INTER_LINEAR:线性差值
//CV_WARP_FILL_OUTLIERS:填充所有输出图像的象素。
//如果部分象素落在输入图像的边界外,那么它们的值设定为 fillval.
//默认情况:CvScalar fillval = cvScalarAll(0);
cvWarpAffine(img, img_rotate, &M, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
}
IplImage* IplI_rotate = cvCreateImage(cvSize(IplI->width,IplI->height), IplI->depth, IplI->nChannels);