OpenCV 透视变换实例


#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <iomanip>
using namespace std;

// Load the source image. HighGUI use.
IplImage *image = 0, *gray = 0, *out=0;
CvMat source, destination, homograghy;

int main( int argc, char** argv )
{
    char* filename = argc == 2 ? argv[1] : (char*)"warpin.bmp";
    
    if( (image = cvLoadImage(filename, 1)) == 0 )
        return -1;
    
	gray = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
	out = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
    cvCvtColor(image, gray, CV_BGR2GRAY);

	double	a[] = {75, 169, 118, 165, 62, 222, 148, 216};
	double	b[] = {103, 19, 192, 19, 105, 205, 192, 205};
	double  c[9];
	
	//create and initialize source and destionation points matrics
	source = cvMat(4, 2,  CV_64FC1, a);
	destination = cvMat( 4, 2, CV_64FC1, b);

	//create homograghy matrix
    homograghy = cvMat( 3, 3, CV_64FC1, c);

	//calculate homograghy matrix
	cvFindHomography(&source, &destination, &homograghy);

    //output homograghy matrix
	cout<<"The homograghy matrix is:"<<endl;
	//cout<<setiosflags(ios::fixed)<<setiosflags(ios::left);
	for ( int i = 0; i < 3; i++ ) {
		for ( int j = 0; j < 3; j++ ) {
			cout <<setw(12)<<cvmGet(&homograghy, i, j);
		}
		cout<<endl;
	}

	//calculate warpout image
	cvWarpPerspective(gray, out, &homograghy);

	// Create windows.
    cvNamedWindow("Source", 1);
    cvNamedWindow("Result", 1);

    // Show the images.
    cvShowImage("Source", image);
    cvShowImage("Result", out);
    
    // Wait for a key stroke
    cvWaitKey(0);
    cvReleaseImage(&image);
    cvReleaseImage(&gray);
	cvReleaseImage(&out);
 
    cvDestroyWindow("Source");
    cvDestroyWindow("Result");

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值