change grayscale to pseudo colouring using colormap in Matlab


In matlab you can view a grayscale image with:


imshow(im)

Which for my image im shows:

And you can also view this grayscale image using pseudocolors from a given colormap with something like:


imshow(im,'Colormap',jet(255))

Which shows:

But it’s not obvious how to use the colormap to actually retrieve the RGB values we see in the plot. Here’s a simple way to convert a grayscale image to a red, green, blue color image using a given colormap:


rgb = ind2rgb(gray2ind(im,255),jet(255));

Replace the 255 with the number of colors in your grayscale image. If you don’t know the number of colors in your grayscale image you can easily find out with:


n = size(unique(reshape(im,size(im,1)*size(im,2),size(im,3))),1);

It’s a little overly complicated to handle if im is already a RGB image.

If you don’t mind if the rgb image comes out as a uint8 rather than double you can use the following which is an order of magnitude faster:


rgb = label2rgb(gray2ind(im,255),jet(255));

Then with your colormaped image stored in rgb you can do anything you normally would with a rgb color image, like view it:


imshow(rgb);

which shows the same as above:

Possible function names include real2rgb, gray2rgb.

Tags: colorgrayscaleimage processingmatlab


———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

What you are asking is to perform a pseudo colouring of an image. Doing this in MATLAB is actually quite easy. You can use the grayscale intensities as an index into a colour map, and each intensity would generate a unique colour. First, what you need to do is create a colour map that is 256 elements long, then use ind2rgb to create your colour image given the grayscale intensities / indices of your image.

There are many different colour maps that are available to you in MATLAB. Here are the current available colour maps in MATLAB without the recently added Parula colour map that was introduced in R2014:

How the colour maps work is that lower indices / grayscale values have colours that move toward the left side of the spectrum and higher indices / grayscale values have colours that move toward the right side of the spectrum.

If you want to create a colour map with 256 elements, you simply use any one of those colour maps as a function and specify 256 as the input parameter to generate a 256 element colour map for you. For example, if you wanted to use the HSV colour map, you would do this in MATLAB:

cmap = hsv(256);

Now, given your grayscale image in your MATLAB workspace is stored in imageArray, simply use ind2rgb this way:

colourArray = ind2rgb(double(imageArray)+1, cmap);

The first argument is the grayscale image you want to pseudocolour, and the second input is the colour map produced by any one of MATLAB's colour mapping functions. colourArray will contain your pseudo coloured image. Take note that we offset the grayscale image by 1 and also cast to double. The reason for this is because MATLAB is a 1-indexed programming language, so we have to start indexing into arrays / matrices starting at 1. Because your intensities range from [0,255], and we want to use this to index into the colour map, we must make this go from [1,256] to allow the indexing. In addition, you are most likely using uint8 images, and so adding 1 to a uint8 will simply saturate any values that are already at 255 to 255. We won't be able to go to 256. Therefore, you need to cast the image temporarily to double so that we can increase the precision of the image and then add 1 to allow the image to go to 256 if merited.

Here's an example using the cameraman.tif image that's part of the image processing toolbox. This is what it looks like:

So we can load in that image in MATLAB like so:

imageArray = imread('cameraman.tif');

Next, we can use the above image, generate a HSV colour map then pseudocolour the image:

cmap = hsv(256);
colourArray = ind2rgb(imageArray+1, cmap);

We get:


Take note that you don't have to use any of the colour maps that MATLAB provides. In fact, you can create your own colour map. All you have to do is create a 256 x 3 matrix where each column denotes the proportion of red (first column), green (second column) and blue (third column) values per intensity. Therefore, the first row gives you the colour that is mapped to intensity 0, the second row gives you the colour that is mapped to intensity 1 and so on. Also, you need to make sure that the intensities are floating-point and range from [0,1]. For example, these are the first 10 rows of the HSV colour map generated above:

>> cmap(1:10,:)

ans =1.0000         0         01.0000    0.0234         01.0000    0.0469         01.0000    0.0703         01.0000    0.0938         01.0000    0.1172         01.0000    0.1406         01.0000    0.1641         01.0000    0.1875         01.0000    0.2109         0

You can then use this custom colour map into ind2rgb to pseudocolour your image.


Good luck and have fun!

change grayscale to pseudo colouring using colormap in Matlab的更多相关文章

  1. colormap是MATLAB里面用来设定和获取当前色图的函数。

    下面将举例.描述MATLAB内建的色图.用户除了可以编程指定MATLAB内建的色图,还可以使用Plot Tools图形用具界面的Figure Properties面板中的Colormap菜单来选择一种 ...

  2. How to Change the Size of a Box-Plot Label in MATLAB

    Type "load carsmall" to load a sample data set included with MATLAB. Type "boxplot(Ho ...

  3. 利用matlab编写实现显示fmri切片slice图像 混合显示 不同侧面显示 可叠加t检验图显示 by DR. Rajeev Raizada

    1.参考 reference 1. tutorial主页:http://www.bcs.rochester.edu/people/raizada/fmri-matlab.htm. 2.speech_b ...

  4. linux安装Tesseract-OCR

    安装Tesseract-OCR 1. leptonica 需要源码编译安装http://www.leptonica.org/ leptonica 包: leptonica-1.73.tar.gz  解 ...

  5. 【论文速读】Fangfang Wang_CVPR2018_Geometry-Aware Scene Text Detection With Instance Transformation Network

    Han Hu--[ICCV2017]WordSup_Exploiting Word Annotations for Character based Text Detection 作者和代码 caffe ...

  6. matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色

    一起来学matlab-matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 < ...

  7. matplotlib绘制热力图

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/jin_tmac/article/deta ...

  8. 利用Matlab快速绘制栅格地图

    代码演示 % 基于栅格地图的机器人路径规划算法 % 第1节:利用Matlab快速绘制栅格地图 clc clear close all %% 构建颜色MAP图 cmap = [1 1 1; ... % ...

  9. V-rep学习笔记:机器人逆运动学数值解法(The Pseudo Inverse Method)

    There are two ways of using the Jacobian matrix to solve kinematics. One is to use the transpose of ...

随机推荐

  1. Spring+SpringMVC+Mybatis整合redis

    SSM整合redis redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列. 这里用的是ssm框架+maven构建的项目 ...

  2. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...

  3. Spring事务杂谈

    1. 什么是事务 事务就是以一种可控的方式,对资源进行的一组操作,保证了资源在事务前后,始终应处于被期待的正确的状态.比如不会受到宕机等原因的影响.事务本身,具有如下4种属性-ACID.(所以说事务是 ...

  4. [转] Elasticsearch 6.1官方入门教程

    一篇比较简要又全面的elasticsearch教程. https://blog.csdn.net/hololens/article/details/78932628

  5. 南昌 Max answer

    https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a interva ...

  6. 【Three.js】如何选中外部模型

    1.问题 three.js中模型选中使用的是射线法,根据摄像机角度,鼠标点击位置和模型选中的distance参数判断来选中模型.对于原生的矢量模型完全没有问题,但是当遇到导入的外部模型,如obj.st ...

  7. net与树莓派的情缘(二)

    虽然我们可以很方便的通过ssh譬如putty或者vnc连接操控树莓派,但是毕竟树莓派资源没那么高,在上面编程,调试要吃力的多.所以还是想在pc上编程上传到树莓派或者最好,文件共享,可以直接读写共同的文 ...

  8. spring boot快速入门 8: 异常处理

    异常处理简单样例: 第一步:创建result实体类 package com.payease.domain; /** * http请求返回的最外层对象 * Created by liuxiaoming ...

  9. 【软件】chrome设置默认字体

    安装stylish插件 新建样式,加入代码 * { font-family: "Microsoft YaHei", "微软雅黑" !important; }

  10. C# 委托的一些使用上的小技巧

    1.委托是一种数据类型,我们可以在任何定义类的地方定义委托,在任何声明类的地方声明委托 2.初始化委托有两种方式,代码如下: (1).像类一样初始化委托 public delegate void Sa ...