Monday, February 21, 2011
perspective opcv
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <math.h>
using namespace cv;
int main(int argc, char** argv) {
CvPoint2D32f srcQuad[4], dstQuad[4];
CvMat* warp_matrix = cvCreateMat(3,3,CV_32FC1);
IplImage *src1, *dst1, *dst2, *dst3, *dst4, *dst5, *dst7, *dst8;
src1=cvLoadImage("C:\\ENEL\\OpenCV2.1\\samples\\c\\airplane.jpg");
IplImage *src = cvCreateImage( cvSize(800 ,700), src1->depth, src1->nChannels );
IplImage *dst6 = cvCreateImage( cvSize(800 ,700), src1->depth, src1->nChannels );
IplImage *checkerfloor = cvCreateImage( cvSize(200,600), src1->depth, src1->nChannels );
IplImage *endwall = cvCreateImage( cvSize(800,700), src1->depth, src1->nChannels );
//Draw a checkerfloor using OpenCV drawing tools, question 1a
//Mostly using cvFillPoly()
cvZero(checkerfloor); //make image all black
cvNamedWindow( "checkerfloor", CV_WINDOW_AUTOSIZE);
//generate coodinates for white squares
CvPoint square1[]={0,0, 0,100, 100,100, 100,0};//draw topleft white square, array of coordinates
CvPoint square2[]={100,100, 100,200, 200,200, 200,100};//draw second from top right white square, and so on etc...
CvPoint square3[]={0,200, 0,300, 100,300, 100,200};
CvPoint square4[]={100,300, 100,400, 200,400, 200,300};
CvPoint square5[]={0,400, 0,500, 100,500, 100,400};
CvPoint square6[]={100,500, 100,600, 200,600, 200,500};
CvPoint* squareArr[6]={square1,square2,square3,square4,square5,square6};// array of array
int nSquarePts[6]={4,4,4,4,4,4};//number of points for each square array
int nSquares=6;// number of squares
int isPolyLineClosed=1;//use this for cvPolyLine
int lineWidth=1; //use this for cvPolyLine
//cvPolyLine(checkerfloor,squareArr,nSquarePts,nSquares,isPolyLineClosed,cvScalar(0,255,255),lineWidth);
//Draw a set of filled polygons, squares in this case:
cvFillPoly(checkerfloor,squareArr,nSquarePts,nSquares,cvScalar(255,255,255), 8);
//cvLine(checkerfloor, cvPoint(100,100), cvPoint(200,200), CV_RGB(255,255,255),1,8);
//cvLine(checkerfloor, cvPoint(200,200), cvPoint(500,300), CV_RGB(255,255,255),1,8);
cvShowImage("checkerfloor", checkerfloor);
//ends question 1a
//Draw a endwall using OpenCV drawing tools, question 1
//Mostly using cvFillPoly()
CvPoint wall[]={0,0, 0,700, 800,700, 800,0};
CvPoint* wallArr[1]={wall};// array of array
int nWallPts[4]={4};//number of points for each square array
int nWalls=1;// number of squares
//Draw a set of filled polygons, squares in this case:
cvFillPoly(endwall,wallArr,nWallPts,nWalls,cvScalar(123,56,78), 8);
cvNamedWindow( "endwall", CV_WINDOW_AUTOSIZE);
cvShowImage("endwall", endwall);
//end endwall
//This is part is for question 1c)
cvResize(src1, src);
cvResize(checkerfloor, dst6); //clone dst* to intialize
dst1 = cvCloneImage(src);
dst1->origin = src->origin;
dst2 = cvCloneImage(src);
dst2->origin = src->origin;
dst3 = cvCloneImage(src);
dst3->origin = src->origin;
dst4 = cvCloneImage(src);
dst4->origin = src->origin;
dst5 = cvCloneImage(src);
cvZero(dst5);//This sets all values in all channels of the array to zero
dst7 = cvCloneImage(src);
dst7->origin = src->origin;
cvZero(dst7);
//dst for for wall
dst8 = cvCloneImage(src);
dst8->origin = src->origin;
cvZero(dst8);
//srcQuad is parameter for cvGetPerspectiveTransform()
//----------------------------------------------
srcQuad[0].x = 0; //src Top left
srcQuad[0].y = 0;
srcQuad[1].x = src->width - 1; //src Top right
srcQuad[1].y = 0;
srcQuad[2].x = 0; //src Bottom left
srcQuad[2].y = src->height - 1;
srcQuad[3].x = src->width - 1; //src Bot right
srcQuad[3].y = src->height - 1;
//pic1: left side nearest
dstQuad[0].x = 0; //dst Top left
dstQuad[0].y = 0;
dstQuad[1].x = 100; //dst Top right
dstQuad[1].y = 100;
dstQuad[2].x = 0; //dst Bottom left
dstQuad[2].y = 700;
dstQuad[3].x = 100; //dst Bot right
dstQuad[3].y = 700-400/3;
cvGetPerspectiveTransform(
srcQuad,
dstQuad,
warp_matrix
);
cvWarpPerspective( src, dst1, warp_matrix );
//pic2: left side farest
dstQuad[0].x = 100; //dst Top left
dstQuad[0].y = 100;
dstQuad[1].x = 300*6/7; //dst Top right
dstQuad[1].y = 300*6/7;
dstQuad[2].x = 100; //dst Bottom left
dstQuad[2].y = 700-400/3;
dstQuad[3].x = 300*6/7; //dst Bot right
dstQuad[3].y = 300*6/7+100;
cvGetPerspectiveTransform(
srcQuad,
dstQuad,
warp_matrix
);
cvWarpPerspective( src, dst2, warp_matrix );
//pic3 right side farest
dstQuad[0].x = 300+500/7; //dst Top left
dstQuad[0].y = 300*6/7;
dstQuad[1].x = 300+500*2/3; //dst Top right
dstQuad[1].y = 100;
dstQuad[2].x = 300+500/7; //dst Bottom left
dstQuad[2].y = 300*6/7+100;
dstQuad[3].x = 300+500*2/3; //dst Bot right
dstQuad[3].y = 700-400/3;
cvGetPerspectiveTransform(
srcQuad,
dstQuad,
warp_matrix
);
cvWarpPerspective( src, dst3, warp_matrix );
//pic4 right side nearest
dstQuad[0].x = 300+500*2/3; //dst Top left
dstQuad[0].y = 100;
dstQuad[1].x = 800; //dst Top right
dstQuad[1].y = 0;
dstQuad[2].x = 300+500*2/3; //dst Bottom left
dstQuad[2].y = 700-400/3;
dstQuad[3].x = 800; //dst Bot right
dstQuad[3].y = 700;
cvGetPerspectiveTransform(
srcQuad,
dstQuad,
warp_matrix
);
cvWarpPerspective( src, dst4, warp_matrix );
//-------------------------------------------------
//added checker floor, Added march 6, 2011
dstQuad[0].x = 300*6/7; //dst Top left
dstQuad[0].y = 300*6/7+100;
dstQuad[1].x =300+500/7 ; //dst Top right
dstQuad[1].y = 300*6/7+100;
dstQuad[2].x = 0; //dst Bottom left
dstQuad[2].y = 700;
dstQuad[3].x = 800; //dst Bot right
dstQuad[3].y = 700;
cvGetPerspectiveTransform(
srcQuad,
dstQuad,
warp_matrix
);
cvWarpPerspective( dst6, dst7, warp_matrix );
//added endwall, Added march 8, 2011
dstQuad[0].x = 300*6/7; //dst Top left
dstQuad[0].y = 300*6/7;
dstQuad[1].x =300+500/7 ; //dst Top right
dstQuad[1].y = 300*6/7;
dstQuad[2].x = 300*6/7; //dst Bottom left
dstQuad[2].y = 300*6/7+100;
dstQuad[3].x = 300+500/7; //dst Bot right
dstQuad[3].y =300*6/7+100 ;
cvGetPerspectiveTransform(
srcQuad,
dstQuad,
warp_matrix
);
cvWarpPerspective( endwall, dst8, warp_matrix );
//add all pics on dst5
// end question 1c
cvAddWeighted(dst1,1,dst2,1,0,dst5);
cvAddWeighted(dst3,1,dst5,1,0,dst5);
cvAddWeighted(dst4,1,dst5,1,0,dst5);
cvAddWeighted(dst7,1,dst5,1,0,dst5);
cvAddWeighted(dst8,1,dst5,1,0,dst5);
cvNamedWindow( "Perspective_Warp", 1 );
cvShowImage( "Perspective_Warp", dst5 );
int p[3]; p[0] = CV_IMWRITE_JPEG_QUALITY; p[1] = 100; p[2] = 0; cvSaveImage("out2.jpg", dst5, p);
cvWaitKey(0);
cvReleaseImage(&dst1);
cvReleaseImage(&dst2);
cvReleaseImage(&dst3);
cvReleaseImage(&dst4);
cvReleaseImage(&dst5);
cvReleaseImage(&dst6);
cvReleaseImage(&dst7);
cvReleaseImage(&dst8);
cvReleaseImage(&src);
cvReleaseImage(&src1);
cvReleaseImage(&checkerfloor);
cvDestroyWindow("checkerfloor");
cvDestroyWindow("endwall");
cvReleaseMat(&warp_matrix);
cvDestroyWindow( "Perspective_Warp" );
return 0;
}
---------------------------------------------------------------------------------
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment