简体   繁体   English

使用OpenCv通过给定矩阵转换图像

[英]Transform image via a given matrix using OpenCv

what I'm trying to do is transforming an image using an (Matlab) transformation matrix. 我正在尝试做的是使用(Matlab)变换矩阵转换图像。 It is the following 2D transformation matrix with a 3x3 dimension: 它是以下具有3x3维度的2D变换矩阵:

 aaa bbb 0 ccc ddd 0 eee fff 1 

I found a pretty good explanation here: how to transform an image with a transformation Matrix in OpenCv? 我在这里找到了一个很好的解释: 如何在OpenCv中使用转换矩阵转换图像? but I'm not able to fill the 3x3 matrix (and apply it to the image). 但我无法填充3x3矩阵(并将其应用于图像)。 This is my code: 这是我的代码:

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

t.at<double>(0, 0) = aaa;
t.at<double>(1, 0) = bbb;
t.at<double>(2, 0) = 0;

t.at<double>(0, 1) = ccc;
t.at<double>(1, 1) = ddd;
t.at<double>(2, 1) = 0;

t.at<double>(0, 2) = eee;
t.at<double>(1, 2) = fff;
t.at<double>(2, 2) = 1;  

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

imshow("outputImage", outputImage);

Causes the following error: 导致以下错误:

OpenCV Error: Assertion failed ((M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 2 && M0.cols == 3) OpenCV错误:断言失败((M0.type()== CV_32F || M0.type()== CV_64F)&& M0.rows == 2 && M0.cols == 3)

Any idea whats wrong here? 这里有什么想法吗?

The transformation matrix used in warp affine is an affine transformation matrix, which representation is as follows: warp仿射中使用的变换矩阵是仿射变换矩阵,其表示如下:

a11 a12 b1
a21 a22 b2
0   0   1

this means it is a 3x3 matrix indeed. 这意味着它确实是一个3x3矩阵。 However OpenCV doesn't care about the last row, since it will always be the same (look at the link before). 但是OpenCV并不关心最后一行,因为它总是相同的(看之前的链接)。 Then, you can eliminate this row and have the input needed (2x3): 然后,您可以消除此行并输入所需输入 (2x3):

a11 a12 b1
a21 a22 b2

In getAffineTransform you can see why they want it like that (to have an homgeneous point mapped to the new 2d point and have it already in 2d and avoid extra multiplications). getAffineTransform中,你可以看到为什么他们想要它(将一个同质点映射到新的2d点,并且已经在2d中并且避免额外的乘法)。

You have a matrix of the type: 你有一个类型的矩阵:

aaa   bbb   0
ccc   ddd   0
eee   fff   1

This seems to be transpose somehow? 这似乎是以某种方式转置? since I do not know how did you generate the matrix, I can't tell for sure. 因为我不知道你是如何生成矩阵的,所以我无法确定。 Maybe you have transpose it and then assign it to opencv (or do it directly). 也许你已将它转置,然后将其分配给opencv(或直接执行)。 The funny part is that (intentionally or otherwise) you had it in your c++ code the matrix transpose. 有趣的部分是(有意或无意地)你在c ++代码中使用了矩阵转置。 Remember that OpenCV notation is usually (rows, columns) in most of the cases. 请记住,在大多数情况下,OpenCV表示法通常是(行,列)。

Your code should look like: 您的代码应如下所示:

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

t.at<double>(0, 0) = aaa;
t.at<double>(1, 0) = bbb;

t.at<double>(0, 1) = ccc;
t.at<double>(1, 1) = ddd;

t.at<double>(0, 2) = eee;
t.at<double>(1, 2) = fff;

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

imshow("outputImage", outputImage);

I hope this helps you with the problem. 我希望这可以帮助你解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM