找回密码
 注册
关于网站域名变更的通知
查看: 599|回复: 1
打印 上一主题 下一主题

Matlab的colormap函数详解(多个colormap)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-7-31 14:22 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。
9 l& w, Y5 B* [0 h  K- Z首先我们读取图片:
1 a* w7 F& X7 _& \& Q% R+ A& Y7 iclear all;% V4 T) A; w! ~! \
close all;# h2 S& H! [, w9 R8 `
clc;
# |% Y6 U- M& \%%* V& b; h' l+ d- W5 {
imgname='.\frog.bmp';/ M- E: Q& b. k
[imagRGB]     = imread( imgname );
. g8 [. D0 M- p6 D5 h: `- Q/ m  z0 r' m) |7 ^' D
if (length(size(imagRGB)) > 2)
1 h. l3 _& ]" z8 T3 S# v( R. T* `    imagGray = rgb2gray(imagRGB);6 k: }" h- C' f2 L4 O( D
else
5 a3 s, S, M# s1 }* A: p& O    imagGray = imagRGB;1 j6 ^! h/ |) z9 N/ \
end
/ z$ D: f4 S* Z5 H$ u6 P+ yclear('imagRGB');
1 X7 P0 X& q0 M/ B9 L! @1 Kdata =  double(imagGray);7 t0 U6 E, E3 {7 t# x+ {, V8 u
data =flipdim(data,1);  
  c" `2 ?4 U! y, r8 N6 T" r   % Flip linear dimensions (image coordinate system starts at upper left
& Q9 P# U7 A- `+ ?" g! B   % corner )
7 A3 i; u! g) z" T& w7 ~; O0 \* v% v% C% \
+ Y: I2 C4 ^4 e: U/ g; l+ N% k
方法1: 我们生成两个figure,分开画两个图
" @) k2 `0 ^& Y; O, ]" u& `/ Wfig1=figure;1 n+ M7 w% c& I4 Z* l4 e
plotHandles(1) = pcolor( data );
* U0 |- @/ i7 P: Tset( plotHandles(1) , 'EdgeColor' , 'none');
  P( I' ~; Z$ q8 R. P" U7 Qcaxis([0,255]);
- c& E5 Y' P% acolormap gray;
. W9 x# _2 ]# @
' J( k' c6 K: I# M  Qfig2=figure;
. f2 M+ m1 [6 H- Z  WplotHandles(1) = pcolor( data );
& \1 U  M+ a5 Nset( plotHandles(1) , 'EdgeColor' , 'none');
5 O0 a7 C7 l) @# mcaxis([0,255])# j: p% w+ R/ o3 t8 d) X) v* _* s
colormap jet;  T* y1 J$ N5 e
  x" I" [7 T5 S. h* Z9 m" Z
/ `8 ?' V2 F5 K6 Z- R0 z! t

" W  _$ N# s+ V* j结果如下:
- `( }* T6 G! m" z0 E7 x
  |  A4 Y  a# @- p- Y* T. }注意:这是两个figure,每个figure可以使用一个唯一的colormap
! V8 p& {8 K' C1 Z
& Y6 i7 i$ W# @
: E" I5 ?2 i! `- z方法二: 一个figure,subplot
8 o( U9 X" P4 U6 J& D' T9 kfig3=figure;
- W0 [3 t# K: e3 @* M3 \, I: D0 y) J* U3 y5 v  [* H
subplot(1,2,1);
# O7 Y+ T2 P2 V. O, z2 D3 KplotHandles(1) = pcolor( data );# R' }( W$ k% t+ [8 B) f, M
set( plotHandles(1) , 'EdgeColor' , 'none');
  N# O7 l  F3 ]+ [" x( ?  V# `caxis([0,255]);
) K, z* V, D6 s: j2 t6 gcolormap gray;- L5 G) o+ P! {
0 R# f0 i' _( r  w: _
subplot(1,2,2);5 z/ J8 C3 y6 d+ G4 d; D4 Y
plotHandles(1) = pcolor( data );* Y9 {% L6 M* p8 B0 B! o1 G
set( plotHandles(1) , 'EdgeColor' , 'none');
9 ]: P5 @4 J  |caxis([0,255])
4 b5 j% t3 a( y: S5 pcolormap jet;8 l* ]2 F$ m8 G5 g& z
% g- \- X8 K6 f* d6 F& T4 s2 E0 C, Q
! F6 h& k, F( s+ B7 `- e+ R
结果如何呢?2 {- l, m6 X$ e& l" s) C
4 s  D. i; T5 x# K% u& k3 W+ _

" H; Y2 {: K  r
/ f/ ^  t4 N0 d! X后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。
* K; X6 @# D( y5 A+ r' g" v8 z' d# a, @' i; f
方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图8 F- H7 ?# i9 v8 Z/ Q8 P
options.colormaps{1}.offset     = 0;
" ?) w! i& [% ~& I% R5 coptions.colormaps{1}.num        = 256;  % Number of colors
0 ^/ G8 x. u+ C, q' i) V# Y( k. Ioptions.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;
  T$ j* s5 }5 H* j! Yoptions.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;1 r# V2 {  W; a. r) K8 b
options.colormaps{1}.map        = gray(256);$ Y) a# [( i. c
4 X8 U+ v' t; q: s' u
options.colormaps{2}.offset     = 256;
7 T+ I& R! w" F1 R. ^options.colormaps{2}.num        = 256;  % Number of colors1 L& U8 R  `; x$ C; U6 \2 j0 p
options.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;, g  T  r6 W2 k  }! z% m
options.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;
8 G: r7 V5 ^# Moptions.colormaps{2}.map        = jet(256);
6 V5 B7 x5 M1 @7 }
* v; u# v) Q3 m5 s4 l" afor iCMap = 1 : length(options.colormaps)
4 H) P1 q# p' W; k  b6 q+ m    if iCMap == 1
$ u) Y9 e7 |6 R+ O        cmap = options.colormaps{iCMap}.map;
. u+ a* u  j6 \) v" _    else$ e5 L! p3 G. \* o$ v1 f# F
        cmap = [cmap ; options.colormaps{iCMap}.map ];
& D5 F0 y& ?) d/ H, N$ _    end$ h$ o: D) n" {- O
end
7 N" h9 _+ |. y3 V0 ^3 |9 y" }# K* l6 k; P
* G% Z7 @" x; O
我们定义了两个色图,然后把两个色图拼装起来。
/ b& v: S: ]( U& I/ \! J! U) j5 ]$ Y; I/ v* E& T3 M
fig4 = figure;4 C# L/ L7 g8 I. k
colormap(cmap);: B* [8 ?* I1 p2 \7 X2 K* j5 K

" M& {( W5 d6 ?* y* wsubplot(1,2,1);
9 [0 ~7 q, I" jdata1 = data +options.colormaps{1}.offset;
5 g7 Q4 x& }/ |! L' eplotHandles(1) = pcolor( data1 );4 X. W- F& q$ q  c& }! b6 O
set( plotHandles(1) , 'EdgeColor' , 'none');4 F8 M- s7 E. D$ J  H" C$ r! C
caxis([0 , size(cmap,1) - 1]);. j" N4 I; c& q" d! d' K

$ u2 o  r- Q2 h& n& h" u" x: m; M1 X! B5 l
subplot(1,2,2);+ g# g7 t2 ^( U; c+ [0 V( t
data2 = data + options.colormaps{2}.offset;% T) ]6 V8 X( b/ b$ x2 t$ g8 f
plotHandles(2) = pcolor( data2 );
8 _% O6 S  [4 I7 q& u8 G' I5 e" s
set( plotHandles(2) , 'EdgeColor' , 'none');
$ y% h! ^$ \2 X; k/ kcaxis([0 , size(cmap,1) - 1]);. m, X  @2 @4 C# e, J

8 i3 i/ n: q5 @( P
8 q4 Q' `  H- [结果如下:
$ x9 M3 g: B' \' \
9 k* l5 i4 f& i' Z3 V9 l2 D, z6 R. [  Q
实际上我们只有一个colormap,但达到了多个colormap的效果。4 `5 h8 B/ p2 X: b) {' ?. H5 B( \) R( x
* g6 H- C& l$ n8 X8 E% n

该用户从未签到

2#
发表于 2020-7-31 15:04 | 只看该作者
Matlab的colormap函数详解(多个colormap)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 21:14 , Processed in 0.171875 second(s), 27 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表