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;
}


你可能感兴趣的:(OpenCV 透视变换实例)