对角线非线性梯度算法

时间:2015-02-21 13:27:13

标签: image algorithm gradient

我正在寻找一种算法来生成以下图像:

Desired output

我只对广场左上半部分(对角线)显示的数据感兴趣。


我目前处于起点,我有以下简单的线性函数:

double GetColorAt(double x, double y)
{
   return 1 - (x + y);
}

其中x在[0 ... 1]的范围内从左到右,y从上到下。返回值是RGB通道的颜色分量,范围为[0 ... 1]。它让我产生了这个:

Current output

2 个答案:

答案 0 :(得分:2)

你很幸运,这是我无法抗拒的那种谜题。

分析图像时,有两件事情会跳出来。

  • r = g = b,即这是纯灰度图像。
  • r + a = 255。

这极大地简化了事情,这意味着您只需要分析一个通道即可获得渐变的完整特征。

这是图像的第0行和第128行的R值​​图:

enter image description here

这个情节非常类似于子弹在重力作用下被拉下来的弧度。因此,我将基本方程式插入Excel,并要求它针对255 - (a*(b*x)^2)a的不同值,针对b的第0行求解最接近的匹配。它提出了a=6.998191873b=0.023556823的值。当我替换x/2时,这些值也密切跟踪第128行的值,因此我知道我在正确的轨道上。

这是Python中的算法:

for y in range(height):
    scale = (256 - y) / 256.0
    for x in range(width):
        r = int(round(255 - (6.998191873 * (0.023556823 * x/scale) ** 2)))
        r = max(r, 0)
        ld2[x, y] = (r, r, r, 255-r)

它不是很精确,这在图像的底部最明显,它似乎是一个像素关闭。但它在视觉上是相同的。

enter image description here

答案 1 :(得分:1)

如果有人喜欢通过查看十六进制而不是图像来测试他们如何生成渐变的理论,以下内容可能有所帮助。它的大小调整为10到25px,25px。

0000000: ff fe fd fa f7 f3 ed e8 e1 d9 d1 c8 be b2 a7 9a 8c 7e 6e 5e 4d 3c 2a 12 18  .................~n^M<*..
0000019: ff fe fc fa f6 f1 ec e6 de d6 cd c3 b8 ac 9f 91 83 73 62 51 3e 2c 15 08 1e  .................sbQ>,...
0000032: ff fe fc f9 f5 f0 ea e3 db d2 c8 bd b1 a4 96 87 77 66 54 41 2d 17 02 06 20  ................wfTA-... 
000004b: ff fe fc f9 f4 ef e8 e1 d8 ce c3 b7 aa 9c 8c 7c 6a 58 44 2f 18 04 00 06 20  ...............|jXD/.... 
0000064: ff fe fb f8 f3 ed e6 de d4 c9 bd b0 a1 92 81 6f 5b 47 31 1a 04 00 00 06 20  ...............o[G1..... 
000007d: ff fe fb f7 f2 eb e3 da cf c4 b6 a8 98 86 74 60 4a 34 1b 04 00 01 00 06 20  ..............t`J4...... 
0000096: ff fe fb f6 f0 e9 e0 d6 ca bd ae 9e 8c 79 64 4e 37 1c 05 00 00 00 00 06 20  .............ydN7....... 
00000af: ff fd fa f5 ef e7 dd d1 c4 b5 a5 93 7f 69 52 3a 1d 03 00 00 00 00 00 06 20  .............iR:........ 
00000c8: ff fd f9 f4 ed e4 d9 cc bd ac 9a 85 6f 57 3d 20 05 00 00 00 00 00 00 06 20  ............oW= ........ 
00000e1: ff fd f9 f3 ea e0 d3 c5 b4 a1 8c 75 5c 41 22 05 00 00 00 00 00 00 00 06 20  ...........u\A"......... 
00000fa: ff fd f8 f1 e7 dc cd bd a9 94 7c 61 45 25 06 00 00 00 00 00 00 00 00 06 20  ..........|aE%.......... 
0000113: ff fc f7 ef e4 d6 c6 b2 9c 84 68 4a 28 07 00 00 00 00 00 00 00 00 00 06 20  ..........hJ(........... 
000012c: ff fc f5 ec df cf bc a6 8c 6f 4f 2b 08 00 00 00 00 00 00 00 00 00 00 06 20  .........oO+............ 
0000145: ff fb f4 e9 da c7 b0 96 77 56 2e 08 00 00 00 00 00 00 00 00 00 00 00 06 20  ........wV.............. 
000015e: ff fa f1 e4 d2 bc a1 81 5d 33 09 00 01 00 00 00 00 00 00 00 00 00 00 06 20  ........]3.............. 
0000177: fe f9 ee de c8 ad 8c 66 38 0a 00 01 00 00 00 00 00 00 00 00 00 00 00 06 20  .......f8............... 
0000190: fe f8 ea d6 bb 99 70 3f 0c 00 01 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ......p?................ 
00001a9: fe f6 e5 cb a8 7d 47 0d 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 06 1f  .....}G..................
00001c2: fe f3 dc ba 8d 51 0f 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  .....Q.................. 
00001db: fd ee ce a0 5e 12 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ....^................... 
00001f4: fc e6 b9 70 16 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 1f  ...p.....................
000020d: fa d9 89 1d 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ........................ 
0000226: f9 b4 26 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ..&..................... 
000023f: e6 40 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  .@...................... 
0000258: 74 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  t.......................