In this post, I am demonstrtintg how OpenCV warpAffine can do Affine Transformation, here I used two function one is getAffineTransform and the second is warpAffine. The function getAffineTransform calculates an affine transform for the three pairs of the corresponding source and destination points.The warpAffine perform the Affine Transformation using the output of getAffineTransform on source image.
C++ Source Code
Here I took source points and destination points manually, where source points are the top-left,top-right,and bottom-right. And the destination points are top-left,top-right and bottom middle. To prevent the cropping after warpAffine, I have scaled destination image to larger width, and corresponding destination points also shifted.
Mat src = imread("obj1.jpg", 1);
std::vector < Point2f > srcPts;
std::vector < Point2f > dstPts;
srcPts.push_back(Point(0, 0));
srcPts.push_back(Point(src.cols, 0));
srcPts.push_back(Point(src.cols, src.rows));
dstPts.push_back(Point(src.cols/2, 0));
dstPts.push_back(Point(src.cols+src.cols/2, 0));
dstPts.push_back(Point(src.cols, src.rows));
for(int i=0;i < srcPts.size();i++){
circle(src,srcPts.at(i),15,Scalar(0,200,0),5);
circle(src,dstPts.at(i),8,Scalar(0,0,200),5);
}
Mat dst = Mat(src.rows,src.cols*1.5,CV_8UC3,Scalar::all(0));
Mat Transform = getAffineTransform(srcPts, dstPts);
warpAffine(src,dst,Transform,dst.size());
imshow("dst", dst);
imshow("src", src);
imwrite("AffineTransform31.jpg",dst);
waitKey();
No comments:
Post a Comment