对于一个项目,我试图创建图像的透视失真以匹配 DVD 盒正面模板。所以我想使用 ImageMagick (CLI) 自动执行此操作,但我很难理解此转换的数学方面。
convert \
-verbose mw2.png \
-alpha set \
-virtual-pixel transparent \
-distort Perspective-Projection '0,0 0,0 0,0 0,0' \
box.png
这段代码是一组空坐标,我已经彻底阅读了文档,但我似乎无法理解什么参数代表什么点。文档给了我变量和名称,我不知道它们的实际含义(可能对数学策划者更有用)。因此,如果有人可以在这个主题上向我解释(视觉上更喜欢,或者给我一个有用信息的链接),因为我不知道我在做什么。只是玩弄参数对这项工作不起作用,我需要计算这些点。
在这里,您会找到我想要实现的目标的简单图像(使用 CLI 工具):
更新:
convert \
-virtual-pixel transparent \
-size 159x92 \
-verbose \
cd_empty.png \
\(mw2.png -distort Perspective '7,40 4,30 4,124 4,123 85,122 100,123 85,2 100,30'\) \
-geometry +3+20 \
-composite cover-after.png
给我作为输出:
cd_empty.png PNG 92x159 92x159+0+0 8-bit sRGB 16.1KB 0.000u 0:00.000
convert: unable to open image `(mw2.png': No such file or directory @ error/blob.c/OpenBlob/2641.
convert: unable to open file `(mw2.png' @ error/png.c/ReadPNGImage/3741.
convert: invalid argument for option Perspective : 'require at least 4 CPs' @ error/distort.c/GenerateCoefficients/807.
convert: no images defined `cover-after.png' @ error/convert.c/ConvertImageCommand/3044.
Kurt Pfeifle 的更正:
该命令有语法错误,因为它没有将
\(
括起来和 \)
根据 ImageMagick 的要求,在每一侧用(至少一个)空白分隔符!由于没有提供指向源图像的链接,我无法测试此更正命令的结果:
convert \
-virtual-pixel transparent \
-size 159x92 \
-verbose \
cd_empty.png \
\( \
mw2.png -distort Perspective '7,40 4,30 4,124 4,123 85,122 100,123 85,2 100,30' \
\) \
-geometry +3+20 \
-composite \
cover-after.png
最佳答案
你看到ImageMagick的失真算法的this very detailed explanation了吗?它还带有相当多的插图。
通过查看您的示例图像,我的猜测是您将使用 Four Point Distortion Method 到达那里。
当然,您给出的带有 0,0 0,0 0,0 0,0
参数的示例并没有达到您的要求。
ImageMagick 中可用的许多失真方法的工作方式如下:
示例:
Sx1,Sy1 Dx1,Dy1 Sx2,Sy2 Dx2,Dy2 Sx3,Sy3 Dx3,Dy3 ... Sxn,Syn Dxn,Dyn
x
用于表示 X 坐标。y
用于表示 Y 坐标。1
, 2
, 3
, ... n
用于表示第 1、2、3、...第 n 个像素。S
在这里用于源像素。D
在这里用于目标像素。第一:方法
-distort perspective
perspective
失真方法将确保源图像中的直线在目标图像中保持直线。其他方法,如 barrel
或 bilinearforward
不会:它们会将直线扭曲成曲线。-distort perspective
需要一组至少 4 个预先计算的像素坐标对(其中最后一个可能为零)。超过 4 对像素坐标提供更准确的失真。因此,如果您使用例如:-distort perspective '1,2 3,4 5,6 7,8 9,10 11,12 13,14 15,16'
(出于可读性原因,在映射对之间使用比所需更多的 {optional} 空格)意味着:
您可能已经看到照片图像中的垂直线(如建筑物墙角)看起来根本不垂直(由于拍摄时相机倾斜)。
-distort perspective
方法可以解决这个问题。它甚至可以实现这样的事情,“拉直”或“矫正”以原始照片“正确”视角出现的建筑物的一张脸:
==>
用于此失真的控制点由绘制在原始图像上的红色(源控件)和蓝色矩形(目标控件)的角表示:
==>
这种特殊的失真使用
-distort perspective '7,40 4,30 4,124 4,123 85,122 100,123 85,2 100,30'
完成复制粘贴乐趣的命令:
convert \
-verbose \
http://i.stack.imgur.com/SN7sm.jpg \
-matte \
-virtual-pixel transparent \
-distort perspective '7,40 4,30 4,124 4,123 85,122 100,123 85,2 100,30' \
output.png
第二:方法
-distort perspective-projection
-distort perspective-projection
方法源自更容易理解的 perspective
方法。它实现了与 -distort perspective
完全相同的失真结果,但不使用(至少)4 对坐标值(至少 16 个整数)作为参数,而是使用 8 个浮点系数。它用...
-distort perspective
不同,其中仅允许整数值); sx ry tx
rx sy ty
px py
它用于根据以下公式从源像素计算目标像素:
X-of-destination = (sx*xs + ry+ys +tx) / (px*xs + py*ys +1)
Y-of-destination = (rx*xs + sy+ys +ty) / (px*xs + py*ys +1)
(TO BE DONE --
I've no time right now to find out how to
properly format + put formulas into the SO editor)
为避免(更难)计算可重用
-distort perspective-projection
方法所需的 8 个系数,您可以... -distort perspective
, _0x1045679 的坐标-distort perspective
与 -verbose
添加参数 x1067919(上面引用的)完整命令示例会输出以下信息:
Perspective Projection:
-distort PerspectiveProjection \
'1.945622, 0.071451, -12.187838, 0.799032,
1.276214, -24.470275, 0.006258, 0.000715'
关于imagemagick - 了解透视投影失真 ImageMagick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12276098/