I've previously solved this problem using a cython script (as need to update at high frame rate) to achieve this. Here the input cmap
to the funciton is a flattened array of a matrix where each row corresponds to a colour and coloumns to RGB values. I used a website to generate the gradient though can't remember which one. The image was flattened for speed and scaled between 0 and 255 int values.
To be able to import and use the cython function you will need to run the setup script from command line, once you have install cython using pip ie
pip install cython
pyhton setup.py build_ext --inplace
This should then produce ac file and .so file.
Cython code:
import numpy as np
cimport numpy as np
cimport cython
DTYPE1 = np.float
ctypedef np.float_t DTYPE1_t
DTYPE2 = np.int
ctypedef np.int_t DTYPE2_t
@cython.boundscheck(False)
@cython.wraparound(False)
def mat_to_im(np.ndarray[DTYPE2_t, ndim=1] data, np.ndarray[DTYPE2_t, ndim=1] cmap):
cdef int wid = data.size
cdef int x, x1, y
cdef np.ndarray[DTYPE2_t, ndim=1] im = np.zeros([wid*3], dtype=DTYPE2)
for x in range(wid):
x1 = x*3
y = data[x]*3
im[x1] = cmap[y]
im[x1+1] = cmap[y+1]
im[x1+2] = cmap[y+2]
return im
Setup file:
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
name='image convert',
version='1',
description='color map images',
author='scooper',
install_requires=['numpy'],
ext_modules=cythonize([
Extension("image_convert", ["image_convert.pyx"], include_dirs=[numpy.get_include()])])
)
This should help with any issues (I've cut the setup from larger code file and haven't tested it): http://cython.readthedocs.io/en/latest/src/quickstart/build.html
Just use LinearSegmentedColormap :
# make a cmap
mycm=matplotlib.colors.LinearSegmentedColormap.from_list('',['#582f91', '#00aeef'])
# apply on a canal
imgrad=mycm(image[:,:,0])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.