我正在尝试翻译代码 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/