了解透视投影失真ImageMagick [英] Understanding Perspective Projection Distortion ImageMagick

查看:390
本文介绍了了解透视投影失真ImageMagick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我试图创建一个图像的透视扭曲,以匹配DVD盒正面模板。所以我想使用ImageMagick(CLI)自动执行此操作,但我很难理解这种转换的数学方面。

For a project I am trying to create a perspective distortion of an image to match a DVD case front template. So I want to automate this using ImageMagick (CLI) but I have a hard time understanding the mathematical aspects of this transformation.

convert \
  -verbose mw2.png \
  -alpha set \
  -virtual-pixel transparent \
  -distort Perspective-Projection '0,0 0,0   0,0 0,0' \
   box.png

此代码为空集坐标,我已经彻底阅读了文档,但我似乎无法理解什么参数代表什么点。文档给了我变量和名称,我不知道它们实际意味着什么(对数学策划者来说更有用)。因此,如果有人可以解释我(视觉上优先,或给我一个有用信息的链接)这个主题,因为我不知道我在做什么。只是玩这个工作的参数就不用了,我需要计算这些点。

This code is en empty set of coordinates, I have read the documentation thoroughly but I can't seem to understand what parameter represents what point. The documentation gives me variables and names where I have no clue what they actually mean (more useful for a mathematical mastermind maybe). So if someone could explain me (visually prefered, or give me a link to useful information) on this subject because I have no clue on what I am doing. Just playing around with the parameters just wont do for this job and I need to calculate these points.

在这里你会发现我想要实现的简单图像(使用CLI工具):

Here you will find an easy image of what I am trying to achieve (with CLI tools):

输入示例图片http:/ /img707.imageshack.us/img707/5419/objecttoachieve.jpg

更新:

   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的要求,每边至少有一个空白!

The command has a syntax error, because it does not surround the \( and \) delimiters by (at least one) blank on each side as required by ImageMagick!

由于没有提供源图像的链接,我无法测试此更正命令的结果:

Since there are no links to the source images provided, I cannot test the outcome of this corrected command:

   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的失真算法?它还附带了不少插图。

Did you see this very detailed explanation of ImageMagick's distortion algorithms? It comes with quite a few illustrations as well.

通过查看示例图像,我的猜测是你将使用 四点失真方法

From looking at your example image, my guess is that you'll get there using a Four Point Distortion Method.

当然,您使用 0,0 0,0 0,0 0,0 参数提供的示例无法执行您想要的操作。

Of course, the example you gave with the 0,0 0,0 0,0 0,0 parameter does not do what you want.

ImageMagick中提供的许多失真方法都是这样的:

Many of the distortion methods available in ImageMagick work like this:


  • 该方法使用一组控制点对

  • 值是数字(可能是浮点数,不仅是整数)。

  • 每对控制点代表一个像素坐标。

  • 每组四个值代表一个源图像坐标,紧接着是目标图像坐标。

  • 将每个源图像控制点的坐标转换为相应的完全由相应参数给出的目标图像控制点。

  • 根据给定的失真方法传输所有其他像素的坐标。

  • The method uses a set of pairs of control points.
  • The values are numbers (may be floating point, not only integer).
  • Each pair of control points represents a pixel coordinate.
  • Each set of four values represent a source image coordinate, followed immediately by the destination image coordinate.
  • Transfer the coordinates for each source image control point into the respective destination image control point exactly as given by the respective parameters.
  • Transfer all the other pixel's coordinates according to the distortion method given.

示例:


S x1 , S y1 D x1 ,D y1  
S x2 ,S y2 D x2 ,D y2  
S x3 ,S y3 D x3 ,D y3  
... 
S xn ,S yn D xn ,D yn  

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 此处用于目标像素。

x is used to represent an X coordinate.
y is used to represent an Y coordinate.
1, 2, 3, ... n is used to represent the 1st, 2nd, 3rd, ... nth pixel.
S is used here for the source pixel.
D is used here for the destination pixel.

失真方法 perspective 将确保源图像中的直线在目标图像中保持直线。其他方法,如 bilinearforward 不会:它们会将直线扭曲成曲线。

The distortion method perspective will make sure that straight lines in the source image will remain straight lines in the destination image. Other methods, like barrel or bilinearforward do not: they will distort straight lines into curves.

-distort perspective 需要一组至少 4个预先计算的像素坐标对(其中最后一个可能是零)。超过4对像素坐标提供更准确的失真。因此,如果您使用例如:

The -distort perspective requires a set of at least 4 pre-calculated pairs of pixel coordinates (where the last one may be zero). More than 4 pairs of pixel coordinates provide for more accurate distortions. So if you used for example:

-distort perspective '1,2  3,4     5,6  7,8     9,10  11,12     13,14  15,16'

(出于可读性原因,使用更多{可选}空白超出要求的映射对)意味着:

(for readability reasons using more {optional} blanks between the mapping pairs than required) would mean:


  1. 从源图像中获取坐标(1,2)处的像素并将其绘制在坐标处( 3,4)在目标图像中。

  2. 从源图像中获取坐标(5,6)处的像素,并在目标图像中的坐标(7,8)处绘制它。

  3. 从源图像中获取坐标(9,10)处的像素,并将其绘制在目标图像中的坐标(11,12)处。

  4. 来自源图像在坐标(13,14)处获取像素并在目标图像中的坐标(15,16)处绘制它。

  1. From the source image take pixel at coordinate (1,2) and paint it at coordinate (3,4) in the destination image.
  2. From the source image take pixel at coordinate (5,6) and paint it at coordinate (7,8) in the destination image.
  3. From the source image take pixel at coordinate (9,10) and paint it at coordinate (11,12) in the destination image.
  4. From the source image take pixel at coordinate (13,14) and paint it at coordinate (15,16) in the destination image.

您可能已经看过照片图像,其中垂直线(如建筑物墙壁的角落)看起来根本不垂直(由于相机在羚牛时有些倾斜快照)。方法 -distort perspective 可以解决这个问题。

You may have seen photo images where the vertical lines (like the corners of building walls) do not look vertical at all (due to some tilting of the camera when taking the snap). The method -distort perspective can rectify this.

它甚至可以达到这样的目的,拉直或纠正在原始照片的'正确'视角中出现的建筑物的一面:

It can even achieve things like this, 'straightening' or 'rectifying' one face of a building that appears in the 'correct' perspective of the original photo:

  ==>  

 ==> 

用于此失真的控制点由红色的角(源控件)和蓝色矩形(目标控件):

The control points used for this distortion are indicated by the corners of the red (source controls) and blue rectangles (destination controls) drawn over the original image:

==>  

 ==> 

使用此特殊失真

-distort perspective '7,40 4,30   4,124 4,123   85,122 100,123   85,2 100,30'

为copy'n'完成命令粘贴乐趣:

Complete command for your copy'n'paste pleasure:

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 派生自更容易理解的透视方法。它实现了与 -distort perspective 完全相同的失真结果,但不使用(至少)4对坐标值(至少16个整数)作为参数,但8个浮点系数。

Second: method -distort perspective-projection

The method -distort perspective-projection is derived from the easier understandable perspective method. It achieves the exactly same distortion result as -distort perspective does, but doesn't use (at least) 4 pairs of coordinate values (at least 16 integers) as parameter, but 8 floating point coefficients.

它使用...


  1. 一套正好8个预先计算的系数;

  2. 这些系数中的每一个都是浮点值(与不同) -distort perspective ,其中只允许整数的值;)

  3. 这8个值代表表单的矩阵

  1. A set of exactly 8 pre-calculated coefficients;
  2. Each of these coefficients is a floating point value (unlike with -distort perspective, where for values only integers are allowed);
  3. These 8 values represent a matrix of the form

  s x     r y     ;  t x

  r x     s y      t y

  p x     p y

 sx   ry   tx
 rx   sy   ty
 px   py

用于根据以下公式计算源像素的目标像素:

which is used to calculate the destination pixels from the source pixels according to this formula:

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透视图的坐标

  • 第二,运行此 -distort perspective 添加了 -verbose 参数,

  • 最后,从打印到 stderr 的输出中读取8个系数。

  • FIRST, (more easily) calculate the coordinates for a -distort perspective ,
  • SECOND, run this -distort perspective with a -verbose parameter added,
  • LAST, read the 8 coefficients from the output printed to stderr .

(上面引用的)完整的指挥官例子会吐出这个信息:

The (above quoted) complete command example would spit out this info:

Perspective Projection:
  -distort PerspectiveProjection \
    '1.945622, 0.071451, -12.187838, 0.799032, 
     1.276214, -24.470275, 0.006258, 0.000715'

这篇关于了解透视投影失真ImageMagick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆