如何使用OpenCv中的矩阵转换图像? [英] how to transform an image with a transformation Matrix in OpenCv?

查看:979
本文介绍了如何使用OpenCv中的矩阵转换图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个角度( arcTan(a))根据直线的大小(y = ax + b)仅适用于所有点的y轴。



我想使用 warpAffine(...)



方法,但是我能够使用这个方法的工作是在图像中使用点(通常为3),这样 warpAffine(.. 。)可以自己找出角度并转换图像的那一部分,这并不是我所需要的,因为我想将整个图像转换成不只是一块。



如果有办法通过 warpAffine(...)或其他任何方法执行此操作,请让我知道

  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 ::尺寸大小(imgb1.cols,imgb1.rows);
warpAffine(imgb1,dest,t,size,INTER_LINEAR,BORDER_CONSTANT);

imshow(outputImage.jpg,dest);

这是我现在可以实现的目标,我的转换矩阵是这样的:

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


解决方案>

warpAffine 需要2x3矩阵;只是离开你的变形的底部行。 (我也改变了矩阵初始化。)这对我有效:

$ $ p code 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 ::尺寸大小(smiley_image.cols,smiley_image.rows);
warpAffine(smiley_image,dest,t,size,INTER_LINEAR,BORDER_CONSTANT);

imshow(outputImage,dest);

以下是我得到的结果:

< img src =https://i.stack.imgur.com/IAbTK.pngalt =原始图像>



编辑



要在Y方向上应用剪切,您需要更改转换矩阵到:

  1 0 0 
-tan(角度)1 0
0 0 1

(实际上,剪切变换通常是 cot(angle)这是 tan(角度)的倒数,但是如果它给了你想要的结果,那就用它。)



以下是使用新转换矩阵的输出图像:


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

解决方案

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:

这篇关于如何使用OpenCv中的矩阵转换图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆