how to transform an image with a transformation Matrix in OpenCv?

这一生的挚爱 提交于 2019-12-11 16:34:15

问题


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:



来源:https://stackoverflow.com/questions/24809142/how-to-transform-an-image-with-a-transformation-matrix-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!