python - 将 python image.transform 转换为 C++ cv::transform

标签 python c++ opencv

我正在尝试翻译代码 in the end of that page从 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 docs我们看到:

Data is a 6-tuple (a, b, c, d, e, f) which contain the first two rows from an affine transform matrix. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c, d x + e y + f)

并且来自 OpenCV docs :

function warpAffine transforms the source image using the specified matrix:

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 填充条目

关于python - 将 python image.transform 转换为 C++ cv::transform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35417531/

相关文章:

c++ - cv::adaptiveBilingualFilter 消失在哪里?

c++ - 无法按索引访问 vector 值

c++ - 如果通过lambda函数对象返回对局部变量的引用,会发生什么情况?

c++ - 如何使用 zlib.h 解压缩 m MYSQL_RES 中的 COMPRESSED BLOB,而不在 mysql 查询中使用 UNCOMPRESS

c++ - 在opencv中创建透明图像

c++ - OpenCV/C++ - 如何在 vector 而不是 Mat 上使用自适应阈值?

python - 正则表达式中的字符串操作似乎有太多异常(exception)

python - 将日期范围行拆分为年(取消分组) - Python Pandas

python - 在 RMarkdown html 文档中显示 python plotly 图形

python - 什么时候用==,什么时候用is?