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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。
; o7 {8 L8 a+ L& W) f首先我们读取图片:) u8 n. J0 R  F1 ?& a
clear all;
2 I5 i3 g6 P* C4 }- w* ~& Wclose all;1 S& _; j3 G+ C0 W* h4 }% @3 ~
clc;
2 `+ M5 Y6 [- j% C; e+ {5 ^%%
5 e! e1 B9 n3 b& j, @* T  g6 Bimgname='.\frog.bmp';
7 B4 P0 ?+ O& o5 `7 w' f3 G[imagRGB]     = imread( imgname );
$ j* H3 ^; t& F- t5 O# i% O$ F8 l7 ^! u: D! Y* }
if (length(size(imagRGB)) > 2)- z# |2 F$ s( W& B# M- G% e7 ?; V
    imagGray = rgb2gray(imagRGB);" i0 |8 A& Z5 X* h2 @3 ]
else, y( H# E% N, p1 ?8 t
    imagGray = imagRGB;# }2 Y# b& S8 A8 n% s
end
* H' X5 K- W+ c& X6 \7 N5 gclear('imagRGB');0 y' X. T9 t; T/ _% N; |- e. u
data =  double(imagGray);3 e. x9 ^( }; Q. X! L) K" n
data =flipdim(data,1);  5 i9 o% ]6 D" ~; Y2 f* s7 A% D
   % Flip linear dimensions (image coordinate system starts at upper left% k) i9 `$ h" B6 [6 o
   % corner )# x8 f) h9 M6 \4 t) e" R0 l
$ y* F3 i/ V, b+ W% k- F

0 M2 W$ b% O, u! x) ?方法1: 我们生成两个figure,分开画两个图
& Q# e5 `. s* K# _fig1=figure;4 ?1 a2 W% `0 @! N
plotHandles(1) = pcolor( data );
0 U6 J( j. l" |" z" Cset( plotHandles(1) , 'EdgeColor' , 'none');! [- p/ d9 m, U3 U9 A  Z; P/ B$ U
caxis([0,255]);) g7 v/ X0 C: [" O+ ~# g! Y7 i- U, u
colormap gray;
6 ?. {! s' `8 \1 g1 P; I 6 c2 Y7 C5 j0 n
fig2=figure;2 U7 X( i" A# p: Y+ _' ~7 @
plotHandles(1) = pcolor( data );" u0 c' c+ o" A8 }# V& W
set( plotHandles(1) , 'EdgeColor' , 'none');3 A# S# {+ s9 g, i- Z
caxis([0,255])
4 H; ~0 Y* o( [+ V% Z& Vcolormap jet;; v( r) x. a2 I. C6 U

3 y/ _4 P2 |. D" n; ?5 |
/ g) u2 S3 M+ z1 K( Y- z( ^" z# t
( r. O, R* l6 G! `1 d结果如下:6 E. A5 u2 \( r6 J
& X: x) `9 Y& p+ S1 Z
注意:这是两个figure,每个figure可以使用一个唯一的colormap( R/ t8 D) W* V0 A7 u0 e
0 u$ z) c2 U; v& U

' w  t5 o: B8 `3 u( D5 v5 d  i方法二: 一个figure,subplot
4 v6 B8 T9 a+ j. d( kfig3=figure;
7 N; R' ^: f4 T/ k, O( x3 e/ e0 }6 t. b) T$ M. L' p6 b
subplot(1,2,1);
9 R, c) u8 G8 y6 @plotHandles(1) = pcolor( data );
, t0 F2 n+ @% M  e5 l! L8 Kset( plotHandles(1) , 'EdgeColor' , 'none');# p) a( K: }3 ]* o  s" o9 Y& Y
caxis([0,255]);
3 f' R( ~/ ]: Lcolormap gray;
( K; o2 l3 M$ x- h6 I 6 l$ z* ?, k: g1 m6 T1 W
subplot(1,2,2);0 ?4 }8 ~5 D6 S" z4 d5 L9 g: Y% X
plotHandles(1) = pcolor( data );
& O0 s. F6 U  R6 {& l/ zset( plotHandles(1) , 'EdgeColor' , 'none');- R1 E: x2 M4 `% a6 m
caxis([0,255])5 _$ z' K. t: m4 m
colormap jet;
" M& @! p1 o# P) l  b, Y. `! v6 [# m" P* i3 r2 E
1 b3 ]& n4 X! B. ]
结果如何呢?
4 ~# F8 o4 O0 E4 y! K3 Y# w3 [! h) M) T+ L- ~# D4 c- x( S

6 @- l8 {7 D1 b  B 3 D1 A# e. o5 L7 |" }! y& f
后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。
, j* I, |3 @4 _" P
8 G  U- G' r4 J: {/ e方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图
% I: y% D1 T) c: D0 `( [3 l4 Qoptions.colormaps{1}.offset     = 0;' S9 f! N2 g; n* _. N1 l$ Y
options.colormaps{1}.num        = 256;  % Number of colors/ v' y8 w( t1 i) [, y$ k6 o) `
options.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;
9 r. @+ I7 s! J2 G4 Poptions.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;3 j% I. D9 @/ o" A
options.colormaps{1}.map        = gray(256);
/ [& ]& D& w/ b# K! K$ M' c# J% O& [' o- {) E0 G: j
options.colormaps{2}.offset     = 256;
0 e4 |% \$ Z  h4 s8 ?) K2 Moptions.colormaps{2}.num        = 256;  % Number of colors# p3 S* T3 M5 z
options.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;9 M; u7 C3 `& A
options.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;: @0 N/ a% ~* W4 B. @% E6 G
options.colormaps{2}.map        = jet(256);1 N' O; X& D2 p+ y, C7 t$ K) T1 O

4 H/ F  W$ z" [for iCMap = 1 : length(options.colormaps): c5 T" O6 F% r
    if iCMap == 1: s2 m8 l. u2 P; l
        cmap = options.colormaps{iCMap}.map;% o  K- Q6 p. T9 `$ s8 t& O" z& c
    else. \2 `* o# E1 g' I
        cmap = [cmap ; options.colormaps{iCMap}.map ];4 \* J& J# {  c# I/ e' h. D6 E1 _: ?
    end1 A! I, h7 f9 X9 [$ I8 W8 p
end
$ I# {$ H5 ]( n: T4 t
* v/ N* G* v" c2 X$ [6 e* d; |, F6 \# R  ^( r4 c& V
我们定义了两个色图,然后把两个色图拼装起来。
0 \( H- P3 ^3 w
/ z9 C6 g1 b' Q0 c, {3 \fig4 = figure;; v8 x0 \; x. l5 V
colormap(cmap);5 I, U  L4 T$ F0 N+ Z
9 W/ A) a$ }  ?( d) O
subplot(1,2,1);4 S: C6 G5 E+ z, X
data1 = data +options.colormaps{1}.offset;  e# U( O# w1 U4 N  ^0 V* E
plotHandles(1) = pcolor( data1 );
$ f9 i  R6 c( a. w4 v1 N1 Kset( plotHandles(1) , 'EdgeColor' , 'none');- I% U; j. J" _5 r9 `9 l
caxis([0 , size(cmap,1) - 1]);# A5 o5 k" G5 L: K0 q5 r1 M. k

6 v7 n% c6 I: ^' R# f9 v( g6 J( z- s( c  j0 N2 b  [# W
subplot(1,2,2);  E" Y/ V4 a8 N$ U6 b# K' X
data2 = data + options.colormaps{2}.offset;2 `  V# W+ X% Z
plotHandles(2) = pcolor( data2 );5 Q& j4 U- C$ ?5 \% {7 \* h3 N
* w7 c4 W, h! `# P$ Y
set( plotHandles(2) , 'EdgeColor' , 'none');6 F% E6 Z+ S% I' Q- F6 M
caxis([0 , size(cmap,1) - 1]);9 F" s9 y( ^' Z) ?( C

, f4 ?3 K, k8 m# a( V
8 q3 h+ d; t- }! N结果如下:
: F/ J7 d; J: a, E2 z) v
0 Y  r6 o0 z& ^0 q' ^& n+ {- e0 Z% N, v9 k1 M  n+ U. h' ]+ `( s+ E
实际上我们只有一个colormap,但达到了多个colormap的效果。
) J0 S. b% R# ^/ w( I5 G; g& H% @- {4 Q9 y2 P( N3 w3 v

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-11-6 04:59 , Processed in 0.156250 second(s), 26 queries , Gzip On.

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

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

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