【问题标题】:Where to get pseudo code algorithm for projecting a RGB parallelogram into an RGB triangle从哪里获得将 RGB 平行四边形投影到 RGB 三角形的伪代码算法
【发布时间】:2011-01-08 05:03:42
【问题描述】:

什么是伪代码算法将平行四边形((RGB)点的二维数组)投影到三角形((RGB)点的二维数组)(在我的特殊情况下,将矩形投影到具有相同边长的直角三角形(等腰),在我的情况下,斜边的大小与矩形的最大边相同)质量可能会丢失。那么如何在伪代码中执行这样的事情呢?

所以通常我们有例如 300x200

我们想把它扭曲成 150 高和 200 宽的三角形

使用烟花 CS5 - 对我来说结果不正确

用photoshop CS5正确结果

所以我想知道 Photoshop 的转换伪代码是什么?

【问题讨论】:

  • 您尝试完成的一些图片可能有助于我们理解问题。
  • 啊,我明白了,它实际上是将其投影到矩形的上边缘无限远的角度……或者至少足够远,以至于它显示为一个像素。跨度>
  • 能否在矩形和三角形中标记顶点集合(A,B,C),使映射更清晰?

标签: algorithm geometry pseudocode projection


【解决方案1】:

我不确定我是否误解了您的问题,但这是我的想法。我假设投影将使得三角形的斜边与矩形最长边的方向相同,因为您说它们的大小相同。一些粗略的图片(未按比例):

矩形 H=4,W=10

---------
|       |
|       |
---------

三角 Hyp=10, S1=8, S2=6

      .
   .   |
.      |
--------

所以我的建议是进行映射,使矩形的“块”等同于三角形的点,每个三角形点是相关矩形块的 RGB 平均值,注意块可能重叠,具体取决于原始对象的比例。

更具体地说,回到上面的例子,首先是比率,高度比率将是固定的,矩形是高度 4,三角形是高度 6,所以对于三角形中的每个像素垂直考虑 6/4,或 1.5 in矩形。现在有两个选项来处理“.5”,您可以考虑向上舍入或向下舍入并仅使用整个块,或者您可以对小数部分使用权重。由于后一种情况不那么简单,我们将进一步研究它。

当我们垂直移动时,任何小数部分都将转换为该行像素的小数权重,因此如果我们垂直平均并且我们的像素为 128 和 137(为简单起见仅查看一个组件),那么我们的平均值将为

(128+(0.5*137))/1.5 = (128+68.5)/1.5 = 196.5/1.5 = 131

现在,由于我们正在查看小数部分,因此我们需要跟踪未使用的小数部分,因此如果上面的下一个像素是 100,我们将要查看

((137*0.5)+100)/1.5 = (68.5+100)/1.5 = 168.5/1.5 = 112.3

现在我们遵循类似的策略逐行垂直向上移动三角形,随着三角形宽度的减小调整比率,因此对于斜边=矩形的底边,这将是 1。再往上,您可能会有这样的比率为 1.23 并且可以进行上述计算。

最后是一些粗略的伪代码:

map(rectangle, triangle) {

  decimal yRatio = triangle.height / rectangle.height
  decimal lastY = 0;//we need decimal indeices for fractional averages

  for each line in dest height (yIndex, 0 based) {
    //here you could even find the average width of the rectangle
    //over the block height, but we won't bother
    decimal xRatio = triangle[yIndex].width / rectangle[floor(yIndex*yRatio)].width
    decimal lastX = 0;    //again a decimal for fractional averages

    for each pixel in dest line width (xIndex, 0 based) {

        decimal pixelAverage = 0;
        decimal tempYRatio = yRatio;
        decimal destY = yRatio * yIndex;

        //Here we calculate the source pixel block average
        while(tempYRatio > 0) {
          //the portion of this row of pixels we use is the minimum
          //of the distance to the next integer, and what we need
          decimal yFraction = minimum(tempYRatio, nextInt(destY) - destY);

          decimal tempXRatio = xRatio;
          decimal destX = xRatio * xIndex;
          while(tempXRatio > 0) {
            decimal xFraction = minimum(tempXRatio, nextInt(destX) - destX);
            //now add the weighted pixel to the average
            average += rectangle[floor(destY)][floor(destX)]*xFraction*yFraction;

            tempXRatio -= xFraction;  //reduce the block size
            destX += xFraction;       //and shift the source index
          }

          tempYRatio -= yFraction;  //reduce the block size
          destY += yFraction;       //and shift the source index
        }

        destination[yIndex][xIndex] = average / (xRatio*yRatio);
    }
  }
}
//a helper function to find the next integer value
integer nextInt(decimal d) {
  integer ret = ceiling(d);
  return d == ret ? d+1 : ret;
}

这不是我的想法,所以我不能保证它是完全正确的,但它应该是一个好的开始,至少对各个像素的每个 RGB 分量进行适当的平均处理。

【讨论】:

    猜你喜欢
    • 2012-12-22
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2021-08-17
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多