0

Graphics32 提供了一个 Image Wrapping 示例 (\Transformation\ImgWarping_Ex),用户可以在其中使用画笔来包裹 bitmap32 的一部分。然而,这个示例真的很难理解,因为它混合了许多复杂的概念。

假设我需要一个环绕变换,它将多边形区域从一种形状转换为另一种形状。我怎么能完成这样的任务?

4

1 回答 1

1

正如评论中的那样,我不确定多面多边形。但是,要将一个四边形转换为另一个,可以使用投影变换。这会将一个由四个点组成的四边形映射到另一个由四个点组成的四边形。(当我问这个 SO question时,我最初发现了这一点。)

你:

  • 使用变换方法
  • 传入目标和源位图
  • 传入一个投影变换对象,用目标点初始化
  • 传入一个光栅器,它控制输出像素如何由一个或多个输入像素组成

光栅化器至少是可选的,但是通过指定除默认值之外的高质量(较慢)光栅化器链,您将获得更高质量的输出。还有其他可选参数。

他们的关键是TProjectiveTransformation对象。不幸的是,Graphics32 文档似乎缺少 的条目Transform method,因此我无法链接到该条目。但是,这里有一些未经测试的示例代码,基于我的一些代码。它将矩形源图像转换为目标图像上的凸四边形:

var
  poProjTransformation : TProjectiveTransformation;
  poRasterizer : TRegularRasterizer; 
  oTargetRect: TRect;
begin
  // Snip some stuff, e.g. that 
  // poSourceBMP and poTargetBMP are TBitmap32-s.
  poProjTransformation := TProjectiveTransformation.Create();
  poRasterizer := TRegularRasterizer.Create();

  // Set up the projective transformation with:
  // the source rectangle
  poProjTransformation.SrcRect = FloatRect(TRect(...));
  // and the destination quad
  // Point 0 is the top-left point of the destination
  poProjTransformation.X0 = oTopLeftPoint.X();
  poProjTransformation.Y0 = oTopLeftPoint.Y();
  // Continue this for the other three points that make up the quad
  // 1 is top-right
  // 2 is bottom-right
  // 3 is bottom-left
  // Or rather, the points that correspond to the (e.g.) top-left input point; in the
  // output the point might not have the same relative position!
  // Note that for a TProjectiveTransformation at least, the output quad must be convex

  // And transform!
  oTargetRect := TTRect(0, 0, poTarget.Width, poTarget.Height)
  Transform(poTargetBMP, poSourceBMP, poProjTransformation, poRasterizer, oTargetRect);

  // Don't forget to free the transformation and rasterizer (keep them around though if
  // you're going to be doing this lots of times, e.g. on many images at once)
  poProjTransformation.Free;
  poRasterizer.Free;

光栅化器是图像质量的一个重要参数——上面的代码会给你一些有用但不漂亮的东西,因为它会使用最近邻采样。您可以构建一个对象链以提供更好的结果,例如将 aTSuperSamplerTRegularRasterizer. 您可以在此处阅读有关采样器和光栅器以及如何组合它们的信息。

该方法还有大约五种不同的重载,Transform值得在文件界面中略读一下它们,GR32_Transforms.pas这样您就知道可以使用哪些版本。

于 2012-08-07T13:51:57.427 回答