将 Python image.transform 转换为 C++ cv::transform

convert python image.transform to c++ cv::transform

本文关键字:transform C++ cv 转换 Python image      更新时间:2023-10-16

我正在尝试将该页面末尾的代码从python翻译成C++。我被困在这一行:

image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample);

显然我将不得不使用

void cv::transform(InputArray src, OutputArray dst, InputArray m)

它期望"转换 2x2 或 2x3 浮点矩阵"作为 m。我认为我必须将 python 代码的(a,b,c,d,e,f)部分放入其中,但究竟如何?就上述元素而言,所需的 2x3 矩阵的格式是什么?

你想使用cv::WarpAffine而不是cv::transform,因为它是对图像进行仿射变换的函数(cv::transform是点矩阵(。通过检查文档,很容易将PIL中的(a,b,c,d,e,f(与OpenCV想要的内容相匹配。

从 PIL 文档中,我们看到:

数据是一个 6 元组 (a, b, c, d, e, f(,其中包含仿射变换矩阵中的前两行。对于输出图像中的每个像素 (x, y(,新值取自一个位置 (a x + b y + c, d x + e y + f(

从OpenCV文档中:

函数 warpAffine 使用指定的矩阵转换源图像:

dst(x,y( = src( M11 x + M12 y + M13, M21 x + M22 y + M23(

因此,您可以将矩阵初始化为 cv::Mat warp_mat( 2, 3, CV_32FC1 );,然后用 a,b,c,d,e,f 填充条目。