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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。
. {0 Z( j* P; `& B4 ^1 k首先我们读取图片:; U( ~* ^8 k/ @+ R; b
clear all;) e  C9 Y3 K" m: C
close all;
- m3 w; I& a/ j& p/ X6 P" vclc;5 @! B* K. V2 A" R( N" F
%%6 |& {4 ?. c) W) n4 }8 M3 A
imgname='.\frog.bmp';+ R8 y7 u+ S4 ]' O
[imagRGB]     = imread( imgname );+ T7 Y6 ?1 {. c5 Z  |6 }
. m& d7 Z' P  [$ v
if (length(size(imagRGB)) > 2)
0 k! v, s, k  r5 n# R+ m+ H    imagGray = rgb2gray(imagRGB);
4 h6 k4 ^7 W& `5 l. f% ^else- U* {) v) U  f/ G' s0 j( ~$ d; \
    imagGray = imagRGB;, H1 k* t; P+ a$ a+ O
end
: r+ h! `3 v( Mclear('imagRGB');
% o5 {$ `! _" l) g( B+ Idata =  double(imagGray);
8 }3 S4 U( w. E9 [# ldata =flipdim(data,1);  
4 V3 R' z: N+ y4 B: z6 ~   % Flip linear dimensions (image coordinate system starts at upper left9 B4 d! q$ c8 F. Q1 z# N
   % corner )- c) W; }, H/ ?& F5 K

: s, W" |2 {8 [6 f" W, M0 b, l0 J/ S! e# w7 x0 l5 \1 y" M
方法1: 我们生成两个figure,分开画两个图6 e# r4 t( ^$ {& b# E7 G# t
fig1=figure;7 ?. }) H$ j  P4 K. m5 D
plotHandles(1) = pcolor( data );
" U7 ^$ d0 q6 j0 a' rset( plotHandles(1) , 'EdgeColor' , 'none');# J9 O2 }$ n6 ?  U3 s8 S) e+ X& {
caxis([0,255]);- W5 P( R- N; k" l2 @4 I
colormap gray;  h* `+ @9 V" d: f+ w4 H, W

; @, J/ ~; c: afig2=figure;
  _& S* T6 y/ Z% @$ g: u  @. F" eplotHandles(1) = pcolor( data );1 a3 O/ ]5 P7 x4 W0 W, M, S9 B$ ?
set( plotHandles(1) , 'EdgeColor' , 'none');5 G! }  T% g6 ^1 G; D
caxis([0,255])
( w: w4 G( f, p9 e0 R$ U/ P. e1 A% t$ A* lcolormap jet;
; S1 z- Q& |. k" _+ y( {( a3 y6 B7 x" }% `' n/ x# _2 w: P  ~3 W
) U4 g4 u/ L4 ^8 n) N( M
8 w& z& C$ B5 F$ V. v! G
结果如下:
$ i/ k# s% a; O( I) D: y# g 3 N* B; m3 S# i0 `! e$ l  _
注意:这是两个figure,每个figure可以使用一个唯一的colormap
2 b9 J, n  p8 t6 L# W% D* A- G. u! j5 e0 S( t, f& b/ _& K

: U. Q! d2 l! f& S$ W- r方法二: 一个figure,subplot
7 \  V8 e' ]2 h2 p' l% M7 h' B- Cfig3=figure;2 F: N4 H+ S0 f  g

! C; C7 S' m" I+ I0 @& r) b8 `) @subplot(1,2,1);
0 T" z/ _  _6 P8 U2 u4 HplotHandles(1) = pcolor( data );
( Q' E& p/ T6 h4 a0 Q  gset( plotHandles(1) , 'EdgeColor' , 'none');
% f3 C; e4 X4 K6 N3 Tcaxis([0,255]);
5 p, l+ t* v7 n- _3 X! J$ Ocolormap gray;
( L; u6 ?3 I8 G! X. ?0 S
4 m4 q9 I' D6 A/ G5 ~subplot(1,2,2);
0 ^( Z. Q# b5 G% _plotHandles(1) = pcolor( data );
9 M* J/ J2 i8 {# d1 ]: eset( plotHandles(1) , 'EdgeColor' , 'none');
7 j5 F) F' R- B. Ucaxis([0,255])
% K5 k3 |% g! U# vcolormap jet;
  D+ ?& D, X! d. e$ c7 [- Q
) O7 x, f7 I* U8 P3 ]/ n9 Q
2 h& g* @, Y8 E) K) r结果如何呢?& v2 a! x0 G5 j- S  @
; [! _% r' |; Z, X
/ T$ E/ p9 g4 z( j7 u( R

4 i1 }  T. _3 T9 f3 c后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。
, g' M8 Q, d" v$ n2 j# t5 r# O% ~" p7 u+ y( C0 t6 ^
方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图
, q9 G4 `5 V0 T, Qoptions.colormaps{1}.offset     = 0;
$ `! _8 k( r8 J1 W& ?options.colormaps{1}.num        = 256;  % Number of colors( u( s+ T8 p4 \. n$ F
options.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;3 S% F3 S6 i0 F5 @
options.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;
9 A* @0 G' |1 Goptions.colormaps{1}.map        = gray(256);# M5 D8 j( S4 U

) f: K' O% x: f! O' eoptions.colormaps{2}.offset     = 256;
2 x7 I1 G( j! h- hoptions.colormaps{2}.num        = 256;  % Number of colors" z- s5 ?6 `! H, C
options.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;# ]' Q+ p. v1 j* v2 N. R) ~3 n$ Z1 l/ ]  G
options.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;
4 I4 b. u7 q5 P, K* G6 |options.colormaps{2}.map        = jet(256);% c9 _) O7 f# R# ?
7 ?5 n* I$ K, h+ J; x# s
for iCMap = 1 : length(options.colormaps)
3 G' ]& j: U1 J* R    if iCMap == 1
" k+ e3 @3 }0 u! x' Y6 n' X/ O! M- H        cmap = options.colormaps{iCMap}.map;2 r6 R' w7 v) ]4 W# O) n/ E9 p' ^* m
    else) V* Z& T; x$ w5 [
        cmap = [cmap ; options.colormaps{iCMap}.map ];
) j9 [. G0 I2 f, e% U    end* ?1 T. t# p) D% V; ]
end
) Z# W7 Q( ]7 P, o3 b0 w
3 A6 S8 u8 k- c2 x: W6 o2 I' O7 h* M4 a+ K3 d9 b0 n$ e
我们定义了两个色图,然后把两个色图拼装起来。) g2 Z5 E# e; Z9 s: F% G2 K9 i

' f7 N, |* P; D$ Efig4 = figure;
; s0 i1 f  [9 T+ ecolormap(cmap);2 a9 j3 L, n: _! u: D' c

. V) ^, }* v; z6 {3 z7 ~& wsubplot(1,2,1);0 ?- [8 A3 R$ H  @
data1 = data +options.colormaps{1}.offset;, n: f2 T5 r' ]0 E# L: z  c
plotHandles(1) = pcolor( data1 );
6 D6 D' z8 l& f) E  Q3 S7 t( Tset( plotHandles(1) , 'EdgeColor' , 'none');4 H& W9 U- l  X, ^% [
caxis([0 , size(cmap,1) - 1]);# h! d4 Q; Z4 C9 \7 V$ W
5 c2 Y% ]3 n& m1 ~& G
+ c3 I5 K$ B$ e! K1 j! Q" g
subplot(1,2,2);
& X2 S7 \- x& Qdata2 = data + options.colormaps{2}.offset;
# k! j. l2 B7 b' _9 ]plotHandles(2) = pcolor( data2 );; r8 H  b- y: @0 G! s% u
2 |) `- N  s! Y, S, b
set( plotHandles(2) , 'EdgeColor' , 'none');5 f  d! z, s& }4 k" v
caxis([0 , size(cmap,1) - 1]);
- N5 F; S. D) a4 M  O" i8 b# S, A% ~2 n9 w
" J' l" q0 ]/ B7 a- H$ G' B6 ~
结果如下:
; E$ x/ e+ ^; r4 _* D: @  S " [' h9 n$ H# o/ Z
+ b6 R4 D/ {7 l6 a. B
实际上我们只有一个colormap,但达到了多个colormap的效果。
7 `* D$ l. S7 T, F3 D0 B; K
9 ~) s; y3 \) d; r- n% b

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-11-24 22:18 , Processed in 0.187500 second(s), 26 queries , Gzip On.

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

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

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