从4个角落颜色内插的二维颜色渐变(256x256矩阵) [英] Two dimensional color ramp (256x256 matrix) interpolated from 4 corner colors

查看:195
本文介绍了从4个角落颜色内插的二维颜色渐变(256x256矩阵)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是以编程方式创建由256x256颜色值矩阵表示的二维颜色渐变。预期结果可以在附图中看到。我的起点是矩阵的4个角色,其间应该插入剩余的254种颜色。虽然我在插入一个轴的颜色方面取得了一些成功,但二维计算却给我带来了一些不好的麻烦。虽然图像似乎具有非线性颜色渐变,但我会对线性渐变感到满意。



如果你能给我一些提示如何用numpy做这个或其他工具我会非常感激。



解决方案

这是一个使用


What I want to achieve is to programmatically create a two-dimensional color ramp represented by a 256x256 matrix of color values. The expected result can be seen in the attached image. What I have for a starting point are the 4 corner colors of the matrix from which the remaining 254 colors inbetween should be interpolated. While I had some success for interpolating the colors for one axis, the two-dimensional calculation provides me some bad headaches. While the image seems to have a non-linear color gradient, I would be happy with a linear one.

If you could give me some hints how to do this with numpy or other tools I`ll be more than thankful.

解决方案

Here's a super short solution using the zoom function from scipy.ndimage. I define a 2x2 RGB image with the intial colors (here random ones) and simply zoom it to 256x256, order=1 makes the interpolation linear. Here is the code :

import numpy as np
import matplotlib.pyplot as plt 

im=(np.random.rand(2,2,3)*255).astype(np.uint8)

from scipy.ndimage.interpolation import zoom
zoomed=zoom(im,(128,128,1),order=1)

plt.subplot(121)
plt.imshow(im,interpolation='nearest')
plt.subplot(122)
plt.imshow(zoomed,interpolation='nearest')
plt.show()

Output:

这篇关于从4个角落颜色内插的二维颜色渐变(256x256矩阵)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆