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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。/ |5 W/ E  J, X6 a2 B
首先我们读取图片:
* X" Y$ ~+ g+ q4 Q9 _- lclear all;( i0 H2 z& |# ]/ Z% z! J
close all;
; H6 e. L3 d. M1 I) K3 L! \, Uclc;$ T; N% Z4 |  s, K
%%
# r2 u2 Y) Q- k9 K) \' ~imgname='.\frog.bmp';2 f% u# r# `4 }$ j
[imagRGB]     = imread( imgname );
: `3 L9 g1 @3 f2 s6 Y" o* X% `9 O9 F& }
if (length(size(imagRGB)) > 2)7 y7 p! k4 s6 h
    imagGray = rgb2gray(imagRGB);! `: q; R8 I) o1 @
else8 v1 r$ S) i) Z+ V' U
    imagGray = imagRGB;& p1 ]0 I! Q8 ^9 U% J" e! Z
end
1 C& t: O# {. g( d2 cclear('imagRGB');
4 |% r& c) k1 O) q0 q3 a$ |) Fdata =  double(imagGray);
8 e/ q3 }  u/ c. i- B% o; Fdata =flipdim(data,1);  
( u! G7 B3 e$ b8 h+ g+ v5 E   % Flip linear dimensions (image coordinate system starts at upper left
! R; W( O5 f& M" }   % corner )4 e3 M9 K0 L1 }' J9 P! {3 E

( o) d) s, m* Q3 s
: V8 z5 t' F0 |: ^4 m  y1 n方法1: 我们生成两个figure,分开画两个图4 x' Q. m/ M: x* `! ]$ D
fig1=figure;; P5 z6 b; e2 h/ D' j
plotHandles(1) = pcolor( data );+ g! d1 D* L- G4 _$ D/ Z8 i
set( plotHandles(1) , 'EdgeColor' , 'none');
0 V( o: x7 ?! k8 T! G0 gcaxis([0,255]);
6 L$ `3 G5 b( u( h6 s, `colormap gray;
+ w& y: p, C7 w' p" K/ ]
$ m6 Z" x0 ~$ s2 I6 ]fig2=figure;
& P; m2 W. s' @3 m: WplotHandles(1) = pcolor( data );
/ J- a. [9 ?' L! W& v# R. yset( plotHandles(1) , 'EdgeColor' , 'none');
7 b. v# l1 Z" I" ncaxis([0,255])
$ A5 ?+ r$ q5 a$ M, w' z. Lcolormap jet;& s6 O, G  q: `  R/ i

  [2 ^* c# _; R' m8 @+ f! Q8 k: B- ~9 c5 h4 @3 j

! i; X* U$ A+ F2 ?结果如下:2 v* d% x/ S% m# W  M. m( M. Q
/ F3 m$ ~: q; h* V
注意:这是两个figure,每个figure可以使用一个唯一的colormap) y$ ?5 W4 A' ?, G0 M- B/ q

8 |% |4 W8 B3 r7 F9 I' A  T, a
3 Z7 Y" C. m7 x3 \5 ~# V, }方法二: 一个figure,subplot
; r' W. f# x/ x# Ofig3=figure;7 y3 |. ^! v; A: Z( [: Y
! r5 I5 }3 q( \9 i
subplot(1,2,1);
( L; f9 q5 e$ VplotHandles(1) = pcolor( data );
' Z& a3 |* i( @6 @, Fset( plotHandles(1) , 'EdgeColor' , 'none');
2 ~- z) M; ?5 s+ ?. ]' l  kcaxis([0,255]);
" ?/ U+ I; R, O! t# ycolormap gray;
2 M. K2 A/ [2 a & h3 O3 L7 \+ n  h: L
subplot(1,2,2);4 C* o6 N$ w% d7 E
plotHandles(1) = pcolor( data );
/ `" i( `* s. t% l3 O. d$ A+ Xset( plotHandles(1) , 'EdgeColor' , 'none');
5 l4 B. B  A( v2 X% xcaxis([0,255])5 A. C8 r* |: N* w# {
colormap jet;. i7 k; Q: _3 ]) x

- i6 Z7 z6 l' @% w- C7 X9 M0 D- B9 L& N( w. i  d
结果如何呢?
9 c. Q; y; q4 S- {+ Z. Y# |/ x9 w4 R3 J* u: w. Z) Q" H" R/ p  ~5 q
6 X+ W, i/ U+ Q4 F

6 F( u+ T( \# d- R后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。
' K  u* g) M. L  Y# S0 h
" E" u* L  Y* z( |! w" Z/ h4 {$ s- h方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图. b. l9 x* H* M. _" b$ A/ x# V
options.colormaps{1}.offset     = 0;' L8 v& L$ q" i% y
options.colormaps{1}.num        = 256;  % Number of colors: H! w4 X/ n4 }# q( b5 s
options.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;8 Q- r4 M( h9 [+ L  y6 J
options.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;
' g& \3 i- p# o/ ?options.colormaps{1}.map        = gray(256);! G  Y/ V7 e" e) N3 h9 E) u

6 q: J) l4 N7 t$ [options.colormaps{2}.offset     = 256;/ @7 S+ @& l, G( O; g
options.colormaps{2}.num        = 256;  % Number of colors
6 [& `, d3 s$ S9 ]3 O* [options.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;
# x6 A; h) \6 `) Ioptions.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;
% E% x9 E2 y" A- U* B3 Goptions.colormaps{2}.map        = jet(256);+ n" Z* X( z+ L& I
1 J/ [* O) c; a) R9 q
for iCMap = 1 : length(options.colormaps)
: B( T, u8 V1 f5 H, l: u) V2 E    if iCMap == 18 l, |  t' x& h6 [0 E
        cmap = options.colormaps{iCMap}.map;0 y9 {3 _+ N8 A* {
    else/ ]8 @& k$ O' h
        cmap = [cmap ; options.colormaps{iCMap}.map ];" T5 N( E8 Y% b" Y4 L
    end% b8 ^: F7 l0 p6 K
end! Y: _) Z( X0 {5 W

: I- L. C2 j4 a: T9 M' K0 u" T) }9 v/ E$ I$ I. e
我们定义了两个色图,然后把两个色图拼装起来。
7 Q1 C+ @3 v8 S+ R2 ~9 }- [
2 e6 [2 I7 U, p+ Tfig4 = figure;8 M( u: b9 v  p
colormap(cmap);
  L7 @; L" ^8 L4 D0 O- b6 B! {9 W1 r0 \: M$ q+ D
subplot(1,2,1);
8 y' k# K) [4 N5 u9 i$ a5 Udata1 = data +options.colormaps{1}.offset;
9 S* R" _" f, P7 q" E0 PplotHandles(1) = pcolor( data1 );
% a+ t) I6 O$ M$ [set( plotHandles(1) , 'EdgeColor' , 'none');" U9 G$ \. F6 h& y$ z
caxis([0 , size(cmap,1) - 1]);- p. a- d8 G) ]1 n
+ [- d( E* x2 G+ z" c( e

4 C: I8 M6 p8 g7 l. ssubplot(1,2,2);
# {. p* S! |. gdata2 = data + options.colormaps{2}.offset;
! ^" y4 s( u! AplotHandles(2) = pcolor( data2 );
5 o! C" g" d# q5 F9 D1 A
  j7 E4 ^3 g$ _) k: @set( plotHandles(2) , 'EdgeColor' , 'none');) |& n8 `( E# f
caxis([0 , size(cmap,1) - 1]);# q! K* h. }1 A5 O

; l$ s% K3 }; |/ U% {# m
5 ^$ o* m+ c# t结果如下:
7 x5 W9 a; k$ W" X7 F: l. i; K9 I
+ U5 w4 w3 |% |! g, l/ F3 P. j  W+ s. O, O
实际上我们只有一个colormap,但达到了多个colormap的效果。
& `: ?( B; k. l6 A% J) D+ r1 d7 E. D& i' d/ [

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-7-27 08:40 , Processed in 0.125000 second(s), 26 queries , Gzip On.

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

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

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