我不确定我是否误解了您的问题,但这是我的想法。我假设投影将使得三角形的斜边与矩形最长边的方向相同,因为您说它们的大小相同。一些粗略的图片(未按比例):
矩形 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 分量进行适当的平均处理。