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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。  e$ d7 c+ E+ ~9 }
首先我们读取图片:
9 m: `1 r+ N3 N8 d4 C5 \clear all;
# S) d. U6 `$ x3 jclose all;
5 t& K, ?+ `5 J- Jclc;4 T/ `$ L" n" K" i; F+ l/ s
%%
! I0 y4 u" ?( c" X2 C  z* \7 b: Qimgname='.\frog.bmp';  q/ L% H2 {# W, k5 y1 T( _& R( M% O
[imagRGB]     = imread( imgname );
$ e  k( N$ i3 A9 ^
8 Z- k  j1 ]1 H3 \; ]3 tif (length(size(imagRGB)) > 2)2 X2 ~( @7 C  C7 j  {1 d1 G1 j! S
    imagGray = rgb2gray(imagRGB);+ ^2 L5 C) y- {1 f. i
else6 C, J. l3 I4 i) W
    imagGray = imagRGB;& H. N; X! Y* W
end
1 E/ o0 u. h' T6 [" o; }clear('imagRGB');7 c% E# D+ Q5 ]4 F$ T8 \  [
data =  double(imagGray);
; @& T/ o8 r1 t$ |& R  j8 u5 b" h4 odata =flipdim(data,1);  . i* A/ H6 G" x; G# ~
   % Flip linear dimensions (image coordinate system starts at upper left& q0 H0 c" `9 Q9 k! l) t
   % corner )
1 A! T( l0 `9 h3 W  y* N& l
* S% z" K7 h4 Q% a, S6 J, k0 I2 k; A, h
方法1: 我们生成两个figure,分开画两个图
- t+ I. Q( ]9 I8 n' O$ c1 \fig1=figure;) N& @6 p" U6 H! M* N9 C
plotHandles(1) = pcolor( data );
* ?7 _% A9 U5 o5 a- H: g0 y5 Jset( plotHandles(1) , 'EdgeColor' , 'none');* H2 H* w# W; p7 ~& x& i  V
caxis([0,255]);
. {# a! S# C0 q$ D0 H' {1 z' @# E4 ocolormap gray;
* q( h0 H/ M* ]" x3 ~
) J% y: x) i! i3 V9 J' Q" I  I# ?fig2=figure;
- ~$ V" x6 N0 L/ _2 h; a2 i- K/ B5 CplotHandles(1) = pcolor( data );( }0 s4 o4 w4 r
set( plotHandles(1) , 'EdgeColor' , 'none');
! h3 a3 I4 l7 s, `caxis([0,255])0 u9 p3 \7 P9 B/ S6 I' [
colormap jet;( \- J/ U: t- |7 l% |, I* @* @) K
& V* C; V; l+ _7 I* g7 P% v
4 o! ~$ m( }' A, o  }7 a

9 Z; j5 R! R4 [$ D1 V: S! M: J结果如下:
5 ~/ N& Z# S3 l6 m# S9 L9 G
1 _8 Q' T$ m2 ]& ^注意:这是两个figure,每个figure可以使用一个唯一的colormap% J# J  g1 V+ e& c) C% y% ^
- \( `% {# M; G8 c5 |3 i/ O
, o( Z) ^  S% S9 _3 i% v
方法二: 一个figure,subplot6 x8 V2 D6 u7 W# \. l  Y
fig3=figure;
# i0 p6 y$ I7 L
0 A- ?& r$ o. p* Ssubplot(1,2,1);
6 x/ G$ i# G# |8 k4 U6 [plotHandles(1) = pcolor( data );
! U- `% K3 C' `9 sset( plotHandles(1) , 'EdgeColor' , 'none');% `$ v) j" t, H8 X1 T
caxis([0,255]);
3 w0 x# f( z; M$ [1 m  ecolormap gray;
: A: y0 D2 v3 ]8 f9 O
* t! [5 T  |2 l2 {4 s) ^7 Ysubplot(1,2,2);
2 R4 k' s# s% e5 w& B! iplotHandles(1) = pcolor( data );
4 X. ?5 o) e/ Z, Y- _$ Y8 bset( plotHandles(1) , 'EdgeColor' , 'none');, N, p* u" l" D  p4 Z7 l
caxis([0,255])8 l2 W' V. A! }4 P8 @
colormap jet;
7 R$ ?( ?* K0 d& l+ P' a- ]* M# u6 y9 h9 Z

$ A: H! ~' `% A+ P$ S/ ~9 ]  c6 ]结果如何呢?7 L% f6 x6 |) O
0 a& t/ j% J9 B4 [9 g. W" c: q
  u2 ?6 @% s: F! d8 x- g2 G

- p1 `) u9 _8 J& B& N: x8 c后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。6 j3 b; D, g2 w2 b* N3 C0 s; F1 F

1 }0 P+ n" Y: h方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图0 b1 x- ~; u% @. X
options.colormaps{1}.offset     = 0;
9 P# l8 _3 Q* p; A3 N! Doptions.colormaps{1}.num        = 256;  % Number of colors$ ?5 \- R/ \1 T  L4 I3 }. F/ u! K
options.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;
% @- X" G* R8 R/ ioptions.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;4 E% g5 M! W( [3 M3 w
options.colormaps{1}.map        = gray(256);4 n1 Y; ?2 f2 c8 }9 H6 c) x' E1 [1 h
3 Z1 m; s9 Q' \$ C
options.colormaps{2}.offset     = 256;. B4 p5 G, ~* [* b' i# o! o& i, P
options.colormaps{2}.num        = 256;  % Number of colors
" m( V" ]; j. ]  Joptions.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;' @% V9 B  S! _! B7 n5 s
options.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;) U% c7 \7 u4 ~; T+ d: X, k
options.colormaps{2}.map        = jet(256);
6 l0 U8 d$ l* P% C( w7 ?
4 t- I2 a- I7 y  O1 V/ z4 Mfor iCMap = 1 : length(options.colormaps)
* @% ~; c# }- S  Z9 R$ |3 T! h    if iCMap == 1% o. H/ O  C0 p# F, A$ I+ B
        cmap = options.colormaps{iCMap}.map;! \7 F" r& W5 O$ v
    else+ c  f" \- `0 `' ^5 T
        cmap = [cmap ; options.colormaps{iCMap}.map ];! Y! H% o6 z9 p" w
    end
- x) [) P) o( Z$ c/ x8 dend$ [  q6 b# m0 C3 S
: E) O8 o/ k  {9 r  U

. U" k- g( c. V# @. t6 ~我们定义了两个色图,然后把两个色图拼装起来。
4 b. w3 f! E8 F: q8 {8 V( F# _9 _$ `
& d% Z: o  c& B; Z; s; o* Hfig4 = figure;
; S: Y2 u% x& k5 p% ^colormap(cmap);
' U1 g/ r: D" O4 d. e0 V" e5 f
9 o0 x% w- \# o; H, ssubplot(1,2,1);" L1 I! m( S' D) E8 x( |: `+ y! z
data1 = data +options.colormaps{1}.offset;
/ v0 R3 P  b/ r$ _0 nplotHandles(1) = pcolor( data1 );" L8 P' I3 L) o% _
set( plotHandles(1) , 'EdgeColor' , 'none');
& {* O. C: o. P2 k0 O4 n8 _: zcaxis([0 , size(cmap,1) - 1]);
* x( D9 x& l& Q- N2 {
0 ?% G0 {! C* w4 V+ ?1 t
+ Z3 K4 @! @. e8 ^  P% g) R6 Q3 qsubplot(1,2,2);
4 o" m9 M/ n# @& s. M! g2 t- N* Zdata2 = data + options.colormaps{2}.offset;; y/ z; q. q$ W( W7 m; Z
plotHandles(2) = pcolor( data2 );
0 `6 T, f' e# Y6 s! k; s6 _" I" ~
6 A$ C5 }9 b5 {: e3 c3 h* t1 Gset( plotHandles(2) , 'EdgeColor' , 'none');% h# `4 f# `* P4 z% Z" O7 L4 C+ W3 \
caxis([0 , size(cmap,1) - 1]);# N8 p' S0 r4 ?1 q

# u8 l8 q) @* v2 D. h6 G9 X
" D$ `5 E9 y  e结果如下:
' P( \  [" D" \3 Y% _
; M( b6 C$ c8 x4 Y/ f
3 S6 V  _. k9 M9 l' W9 W实际上我们只有一个colormap,但达到了多个colormap的效果。
  l  J% H3 n" f  p
$ {* G6 r  C' ?7 {+ b- n: D

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-6-23 09:03 , Processed in 0.078125 second(s), 26 queries , Gzip On.

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

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

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