首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用Graphics32包装变换变换多边形区域?

如何利用Graphics32包装变换变换多边形区域?
EN

Stack Overflow用户
提问于 2012-08-02 16:12:15
回答 1查看 1.4K关注 0票数 0

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

假设我需要一个包装变换,它将一个多边形区域从一个形状转换到另一个形状。我怎样才能完成这项工作呢?

EN

回答 1

Stack Overflow用户

发布于 2012-08-07 13:51:57

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

你:

  • 使用变换方法
  • 传入目标和源位图
  • 传入以目标点初始化的投影转换对象。
  • 输入一个栅格,它控制如何从一个或多个输入像素生成输出像素。

栅格至少是可选的,但是您可以通过指定一个高质量(慢一点)的光栅器链来获得高质量的输出,而不是默认的。还有其他可选参数。

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

代码语言:javascript
复制
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;

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

对于Transform方法,还有五种不同的重载,值得在GR32_Transforms.pas文件的接口中浏览它们,这样您就可以知道可以使用哪些版本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11781603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档