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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。* P8 r4 l' W$ N" i
首先我们读取图片:3 r5 q& V; I" C5 q) P2 s6 ~
clear all;) \7 n/ b. b$ `: _( @
close all;
2 f) b) t9 B+ ~' _& ^clc;
0 Z$ l1 x* B) P; Z9 z5 y* A%%7 Q( A4 X9 |2 Q9 c2 r* U8 ?$ x
imgname='.\frog.bmp';1 i" ^: u9 {/ J6 B/ W- b3 \+ d
[imagRGB]     = imread( imgname );
# k8 k' x  I2 ~7 L% z( |* w; l# |2 M6 e
if (length(size(imagRGB)) > 2)
) f, X! c* w( S0 T( q+ H3 K    imagGray = rgb2gray(imagRGB);
  m5 a6 K4 O9 d" u6 `+ Kelse* h/ Q8 u9 N4 Z6 S
    imagGray = imagRGB;1 N  \/ ^0 g$ a* w7 O
end
# K1 N  p. a' S( M& D; ]/ cclear('imagRGB');/ e4 P, |) R" |
data =  double(imagGray);3 z+ Y6 }9 {8 r% c0 W
data =flipdim(data,1);  
' w8 c" ~0 l5 j4 \: _6 D5 p8 |   % Flip linear dimensions (image coordinate system starts at upper left
: z2 z% R3 s: p6 G6 x   % corner )
! L" S2 Z; X; y1 O: |: E$ h% A
/ B/ ^* V8 E1 p* h4 B8 @9 R5 C
8 ?, F' \7 J2 b, M方法1: 我们生成两个figure,分开画两个图
9 ^* S( l6 s& b' y6 Bfig1=figure;
* M3 l: }4 @' iplotHandles(1) = pcolor( data );
/ Q& p$ F! x& r8 ~5 K- b' K* bset( plotHandles(1) , 'EdgeColor' , 'none');
& O9 C3 M' i# N0 wcaxis([0,255]);0 A& c" K" x# V0 C
colormap gray;
& X6 L9 U+ o7 N5 i$ ~& M. c  M 4 H0 P0 {  o( ~7 m' F
fig2=figure;
3 d% J3 w- h% z! r/ M% [8 vplotHandles(1) = pcolor( data );4 X: A: `; V" I3 r4 w' i. P' n- X* @, M
set( plotHandles(1) , 'EdgeColor' , 'none');  y( j/ w0 u7 X) F
caxis([0,255])1 k3 Y! y' m+ y3 {) l7 [
colormap jet;7 O6 E* w0 W& d1 m, D7 n

1 C8 h" ]# L$ h0 N
! m& s  a, |6 G$ }7 ]1 ]
( l9 q* y' T- e结果如下:* n% \2 l- n, H( v2 F' _5 @
0 I7 I' `+ l8 y8 Z  ~5 x& p
注意:这是两个figure,每个figure可以使用一个唯一的colormap
, X8 R) \$ `: r7 h
' d- i; o+ {( i# \% {
+ \! f# M" v) ^; T' x% k方法二: 一个figure,subplot/ ?$ o" M& o6 g6 \9 t1 h. \
fig3=figure;
( e2 u5 }3 h' \! \: b, w
- ~5 P0 [+ o" p: x8 hsubplot(1,2,1);' Q8 I/ U9 w- i( N- X
plotHandles(1) = pcolor( data );
% V( q! ]! X7 L/ R/ ^% pset( plotHandles(1) , 'EdgeColor' , 'none');
) V: A( {) B5 Fcaxis([0,255]);, e8 N$ m+ D! V; `" X
colormap gray;+ |7 {1 {3 \! }3 }9 C5 T; ]: I

8 j- n- I- j0 a5 osubplot(1,2,2);
0 ?8 z) [& P6 L8 v# }0 T* @& s, UplotHandles(1) = pcolor( data );
% a1 ?  h( F' m* ]/ Cset( plotHandles(1) , 'EdgeColor' , 'none');
/ p; j3 p1 _, z& E* _: Y% Q) ^+ |% Icaxis([0,255])& ^% ^1 v1 D+ p7 b
colormap jet;
# ^7 B7 i# K, |! k
5 B" S- ?4 _- N" M0 V. U" u5 O& B8 ~5 B$ t1 ^
结果如何呢?
* U( s. k8 W+ V9 T
3 O: ~$ N; f4 B# {4 {
# a- L4 O: d- q- s& j ; _- d9 |9 q% w1 l( B- o
后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。3 n+ P/ A# K  V0 W

3 a, A9 K! Z6 O- E! h方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图
$ x5 c9 K) v5 X( m; y9 c# coptions.colormaps{1}.offset     = 0;
3 G2 n. E7 X$ f5 ooptions.colormaps{1}.num        = 256;  % Number of colors
+ l3 Z3 I: f2 e& P, J  @1 p3 [5 R4 Boptions.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;
; t' n8 v" s6 S0 j. c) |options.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;
- e5 V! w/ M+ p0 p% l4 uoptions.colormaps{1}.map        = gray(256);
# ]0 Y- z! [% G: E
. a" n/ J* V% aoptions.colormaps{2}.offset     = 256;4 l' O8 }& [: c0 @8 T; X8 d7 K
options.colormaps{2}.num        = 256;  % Number of colors
, `9 E2 {- `2 Q  E8 G+ ]) N. Ioptions.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;1 Q8 q* u7 c. u- |0 L3 `; W
options.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;
9 R) @' k2 q8 r0 J# I' s7 Voptions.colormaps{2}.map        = jet(256);
& D4 X1 d: `5 R2 K2 D9 `. V7 {& P7 e( Y  C- H
for iCMap = 1 : length(options.colormaps)2 g% w1 c  U+ l' G" u( b& J
    if iCMap == 1, z! v8 C9 h$ V/ P* a; i, ?$ P3 P
        cmap = options.colormaps{iCMap}.map;3 F6 c* J: I! S& p
    else5 P/ F  W! u+ n
        cmap = [cmap ; options.colormaps{iCMap}.map ];( o6 n, W/ B  `+ l. O
    end
$ t; Y6 Z% B7 t& [1 Z) H2 Mend. Y; j+ l  O5 J8 Z

% z2 U& @/ ]$ D; m
$ c  Q* z$ d9 s3 C0 k我们定义了两个色图,然后把两个色图拼装起来。4 ^3 T, @2 S4 e+ |  J

; k9 T! F& C. h. e+ l8 W$ Ufig4 = figure;2 Y& E* C1 _, u  r0 t1 C" Q
colormap(cmap);
+ P* ?# @# }8 \" e* N8 F& r% W
* X1 n7 ^5 f( P/ A, c( V/ Vsubplot(1,2,1);
2 g. L9 r! G% P! B5 Tdata1 = data +options.colormaps{1}.offset;& T- u1 r# J5 m# p
plotHandles(1) = pcolor( data1 );: V* r3 E& n1 r& B
set( plotHandles(1) , 'EdgeColor' , 'none');9 `% m& D- X5 P) H
caxis([0 , size(cmap,1) - 1]);
( R4 A( R. r( |2 Y6 ] 7 t7 J2 Q9 p# e/ y; s4 t

" j" C; b* F$ n% l% ksubplot(1,2,2);3 r' K1 U# |' F% J3 {8 s
data2 = data + options.colormaps{2}.offset;# t, i) _/ _5 D
plotHandles(2) = pcolor( data2 );# Q2 E+ {  [. Z- F% B

) ]+ o9 _% `" K2 ^+ V; P9 Bset( plotHandles(2) , 'EdgeColor' , 'none');6 r. a, f8 a7 F4 e
caxis([0 , size(cmap,1) - 1]);
; b& x8 c/ y8 `' p% y% H
" h2 i. ]  H4 W* m% N% u8 x2 W8 W7 e! v/ l
结果如下:* f$ H! T( v, y# L7 p) h+ t9 E

+ ?6 B+ O; f# W: U9 A& v4 P& O0 S
- m- Y, r: o7 ?2 u1 @, }: D& W实际上我们只有一个colormap,但达到了多个colormap的效果。: @7 H' w' H( z9 r1 u

5 y+ F/ r2 U* E& l7 D5 p, k7 R

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-10-8 01:44 , Processed in 0.156250 second(s), 27 queries , Gzip On.

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

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

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