how to transform an image with a transformation Ma

2019-09-02 20:04发布

问题:

i want to transform an entire image according to the magnitude of a straight line (y=ax+b) with an angle ( arcTan(a) ) this angle should be applied just to the y axis of all the points.

i wanted to use the warpAffine(...)

method but what I was able to make work with this method is using points (generally 3) in the image so that warpAffine(...) can figure out the angle for itself and transform that part of the image, and that's not what I need because I want to transform the whole image not just a piece.

if there's a way to do this with warpAffine(...) or any other method please let me know

cv::Mat t(3,3,CV_64F);
t=0;

t.at<double>(0,0) = 1;    
t.at<double>(1,1) = 1; 
t.at<double>(0,1) = -tan(0.17);    
t.at<double>(2,2) = 1;


cv::Mat dest;
cv::Size size(imgb1.cols,imgb1.rows);
warpAffine(imgb1, dest, t, size, INTER_LINEAR, BORDER_CONSTANT);

imshow("outputImage.jpg", dest);

that's what I could achieve till now, my transformation matrix is like this :

1 -tan(angle) 0
0     1       0
0     0       1

回答1:

warpAffine takes a 2x3 matrix; just leave off the bottom row of your transform. (I also changed the matrix initialization.) This worked for me:

    cv::Mat t(2,3,CV_64F, cvScalar(0.0));

    t.at<double>(0,0) = 1;    
    t.at<double>(1,1) = 1; 
    t.at<double>(0,1) = -tan(0.17);    
//    t.at<double>(2,2) = 1;


    cv::Mat dest;
    cv::Size size(smiley_image.cols,smiley_image.rows);
    warpAffine(smiley_image, dest, t, size, INTER_LINEAR, BORDER_CONSTANT);

    imshow("outputImage", dest);

Here's what I got:

Edit

To apply shear in the Y-direction you need to change your transformation matrix to:

1               0       0
-tan(angle)     1       0
0               0       1

(Actually, the shear transformation is usually cot(angle) which is the reciprocal of tan(angle), but if it gives you the results you want, then use it.)

Here's the output image using the new transformation matrix: