OpenCV函数cvWarpAffine仿射变换实例

#include
#include

#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")

int main()
{
	CvPoint2D32f srcTri[3], dstTri[3];
	CvMat*       rot_mat = cvCreateMat (2, 3, CV_32FC1);
	CvMat*       warp_mat = cvCreateMat (2, 3, CV_32FC1);
	IplImage*    src = NULL;
	IplImage*    dst = NULL;

	src = cvLoadImage ("test.png", 1);
	cvNamedWindow("src", 1);
	cvShowImage("src", src);
	dst = cvCloneImage (src);
	dst->origin = src->origin;
	cvZero (dst);

	srcTri[0].x = 0;
	srcTri[0].y = 0;
	srcTri[1].x = src->width - 1;
	srcTri[1].y = 0;
	srcTri[2].x = 0;
	srcTri[2].y = src->height - 1;

	dstTri[0].x = src->width * 0.0;
	dstTri[0].y = src->height * 0.33;
	dstTri[1].x = src->width * 0.85;
	dstTri[1].y = src->height * 0.25;
	dstTri[2].x = src->width * 0.15;
	dstTri[2].y = src->height * 0.7;

	cvGetAffineTransform (srcTri, dstTri, warp_mat);
	cvWarpAffine (src, dst, warp_mat);
	cvCopy (dst, src);

	CvPoint2D32f center = cvPoint2D32f (src->width / 2, src->height / 2);
	double angle = -50.0;
	double scale = 0.6;
	cv2DRotationMatrix (center, angle, scale, rot_mat);
	cvWarpAffine (src, dst, rot_mat);

	cvNamedWindow ("Affine_Transform", 1);
	cvShowImage ("Affine_Transform", dst);

	cvWaitKey (0);

	cvReleaseImage (&src);
	cvReleaseImage (&dst);
	cvReleaseMat (&warp_mat);
	cvReleaseMat (&rot_mat);

	return 0;
}

OpenCV函数cvWarpAffine仿射变换实例_第1张图片

你可能感兴趣的:(OpenCV)