본문으로 바로가기

Affine Transformtaion


 KScRgbImage2d* tempimage;

KScScalarImage2dUint8* grayimage;

cv::Point2f srcTri[3];

        cv::Point2f dstTri[3];

cv::Mat rot_mat( 2, 3, CV_32FC1 );

        cv::Mat warp_mat( 2, 3, CV_32FC1 );


CRect rect;

GetDlgItem(IDC_STATICSRC)->GetClientRect(&rect);

cv::Size winSize(rect.right, rect.bottom);


cv::Mat cvImgTmp;

cv::resize(mImg, cvImgTmp, cv::Size(rect.Width(), rect.Height()));

cv::Mat affinedst;

cv::Mat warp_rotate_dst;


affinedst = cv::Mat::zeros(cvImgTmp.rows,cvImgTmp.cols,cvImgTmp.type());

// Affine Transform: As we explained lines above, we need two sets of 3 points

// to derive the affine transform relation. 


 srcTri[0] = cv::Point2f( 0,0 );

    srcTri[1] = cv::Point2f( cvImgTmp.cols - 1, 0 );

    srcTri[2] = cv::Point2f( 0, cvImgTmp.rows - 1 );


    dstTri[0] = cv::Point2f( cvImgTmp.cols*0.0, cvImgTmp.rows*0.33 );

    dstTri[1] = cv::Point2f( cvImgTmp.cols*0.85, cvImgTmp.rows*0.25 );

    dstTri[2] = cv::Point2f( cvImgTmp.cols*0.15, cvImgTmp.rows*0.7 );


warp_mat = getAffineTransform( srcTri, dstTri );


/// Apply the Affine Transform just found to the src image

warpAffine( cvImgTmp, affinedst, warp_mat, affinedst.size() );

cv::Point center = cv::Point( affinedst.cols/2, affinedst.rows/2 );

    double angle = -50.0;

    double scale = 0.6;

rot_mat = getRotationMatrix2D( center, angle, scale );


//apply the Affine transformation

warpAffine( affinedst, warp_rotate_dst, rot_mat, affinedst.size() );

cv::imshow( "affinedst", affinedst );

cv::imshow( "warp_rotate_dst", warp_rotate_dst );

cv::waitKey(0);



출처 : http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html





Perspective Transformation



     cv::Point2f inputQuad[4]; 

// Output Quadilateral or World plane coordinates

cv::Point2f outputQuad[4];


// Lambda Matrix

cv::Mat lambda( 2, 4, CV_32FC1 );

//Input and Output Image;

cv::Mat perspectivedst;


//Load the image

CRect rect;

GetDlgItem(IDC_STATICSRC)->GetClientRect(&rect);

cv::Size winSize(rect.right, rect.bottom);

cv::Mat cvImgTmp;

cv::resize(mImg, cvImgTmp, cv::Size(rect.Width(), rect.Height()));

// Set the lambda matrix the same type and size as input

lambda = cv::Mat::zeros( cvImgTmp.rows, cvImgTmp.cols, cvImgTmp.type() );


// The 4 points that select quadilateral on the input , from top-left in clockwise order

// These four pts are the sides of the rect box used as input 


inputQuad[0] = cv::Point2f( 0-(abs(perspectx[0] - perspectx[4] + cvImgTmp.cols)),0-abs(perspecty[0] - perspecty[4]) );

inputQuad[1] = cv::Point2f( cvImgTmp.cols+(abs(perspectx[1] - perspectx[5] + cvImgTmp.cols)),0-abs(perspecty[1] - perspecty[5]));

inputQuad[2] = cv::Point2f( cvImgTmp.cols+(abs(perspectx[2] - perspectx[6] + cvImgTmp.cols)),cvImgTmp.rows+abs(perspecty[2] -                 perspecty[6]));

inputQuad[3] = cv::Point2f( -(abs(perspectx[3] - perspectx[7] + cvImgTmp.cols)),cvImgTmp.rows+abs(perspecty[3] - perspecty[7]));

// The 4 points where the mapping is to be done , from top-left in clockwise order

outputQuad[0] = cv::Point2f( perspectx[0],perspecty[0] );

outputQuad[1] = cv::Point2f( perspectx[1],perspecty[1] );

outputQuad[2] = cv::Point2f( perspectx[2],perspecty[2] );

outputQuad[3] = cv::Point2f( perspectx[3],perspecty[3] );

// Get the Perspective Transform Matrix i.e. lambda 

lambda = getPerspectiveTransform( inputQuad, outputQuad );

// Apply the Perspective Transform just found to the src image

cv::warpPerspective(cvImgTmp,perspectivedst,lambda,perspectivedst.size() );


//Display input and output

cv::imshow("Input",cvImgTmp);

cv::imshow("Output",perspectivedst);


cv::waitKey(0);

flag = 1;

mImg = perspectivedst;

Invalidate();



출처 : http://opencvexamples.blogspot.com/2014/01/perspective-transform.html

'2016-1 > Image Processing' 카테고리의 다른 글

HSV Color model (RGB to HSV)  (0) 2016.10.02
Canny Edge  (0) 2016.04.18