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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。% P) I" V6 S% S. [+ I
首先我们读取图片:
* i. |7 J/ D0 U, |# }7 bclear all;
7 {4 \) W2 P" E8 \/ {' O; oclose all;6 }0 b# e! J! _5 ]+ z
clc;* N& B, m) o  q
%%9 A, P$ S; @  i8 A* |/ r$ v
imgname='.\frog.bmp';
, k/ H- n& }8 u' Z( S+ b[imagRGB]     = imread( imgname );) f- v! Q  m& _

( R, V- k( b7 ^0 ]7 g$ j" G, `if (length(size(imagRGB)) > 2)3 x6 k0 V/ ^6 L7 ~
    imagGray = rgb2gray(imagRGB);
+ p5 |  }$ G  X1 V* ?: l8 welse
9 f: r4 e6 o+ W) Q    imagGray = imagRGB;
% M/ x7 M5 Y  v* w6 r4 j/ w! Wend$ ?. U# w( e/ G
clear('imagRGB');  ]* T' f0 D: g( T: N; ~
data =  double(imagGray);& v1 o; a" G1 z, v7 S5 Z8 s
data =flipdim(data,1);  : N. ~& |: s/ t( \1 j( W
   % Flip linear dimensions (image coordinate system starts at upper left
3 U, f9 z/ h1 c! z/ O   % corner )
- M+ l- z  L2 s1 a* C% y: q6 z4 M0 y& x; _; ~: _2 j
3 k% j- _5 w5 w5 l7 K
方法1: 我们生成两个figure,分开画两个图
0 L' T7 S, c( W4 O, a2 Z( pfig1=figure;& |* p# ~* D; {4 B
plotHandles(1) = pcolor( data );
* T: z: A) p3 yset( plotHandles(1) , 'EdgeColor' , 'none');% t; g' W" A+ }2 S
caxis([0,255]);
" y# @2 |" g3 y1 h2 ^colormap gray;
! ?( W, w2 o4 i, [+ M  Y- `
' g# U. t, C# V$ s7 A" b# Kfig2=figure;
8 P. k& _. U7 `+ l" B- hplotHandles(1) = pcolor( data );. ^) d0 Q4 t5 c1 u1 q
set( plotHandles(1) , 'EdgeColor' , 'none');
/ H, E, q/ S+ X5 q7 W* O2 ]caxis([0,255]), h- p' L4 \# H
colormap jet;
/ o; y/ b& M% N/ [' J9 n1 `) i5 Q: ~) F* G' @) f0 s4 c7 Y' {
% W3 f/ Q' `' h
1 U+ r7 q# \3 c4 b
结果如下:
+ g$ L  k3 S, c: a& P" z
  |, N; R" [2 G% a( I注意:这是两个figure,每个figure可以使用一个唯一的colormap! f0 N# C, g. o7 A' r. H

! M( ~6 D0 e3 H, R2 {9 ]6 D4 }7 X5 H" ~6 e- w
方法二: 一个figure,subplot
& r* s7 N0 f9 b9 q5 h+ Z2 efig3=figure;
7 w% a+ C9 L5 \9 v2 _  ^5 ?% N9 _' `' [: |* _5 t
subplot(1,2,1);- ?* m5 H8 b) ]7 d1 f
plotHandles(1) = pcolor( data );
2 J3 ^6 L3 y9 Lset( plotHandles(1) , 'EdgeColor' , 'none');3 I8 L% x! o3 X- q2 m
caxis([0,255]);6 m) s7 B; x% S# ~6 B
colormap gray;/ `. X: y% N0 ^8 p# @
1 o, |% j0 D: o) @8 y
subplot(1,2,2);9 O/ t# A' ]! ~: {' t
plotHandles(1) = pcolor( data );0 z6 M6 J1 C  ^/ p
set( plotHandles(1) , 'EdgeColor' , 'none');
& d, [5 k9 L, C( X& N. n, X; Ecaxis([0,255])& i' s2 f# x4 ~+ R* h
colormap jet;
/ }. E% [& }3 ~' @; u/ |6 M; m, M+ {% N4 f0 a) u
, j, B' g! [0 j* C
结果如何呢?, ~6 }* e; r- G' i- `

" r' }9 P- V, G$ h# ~/ @+ i' p1 v7 K
- o6 d; ]) P' a, x 0 s  v# ~5 b' M4 S+ s2 }
后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。
  V; z3 ^7 b) l! N
0 c' O+ G) r3 t5 _0 `方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图* L9 m: n( L6 G4 s& F7 c# c* f0 {
options.colormaps{1}.offset     = 0;
6 ~3 l- ^& V- V& J, Uoptions.colormaps{1}.num        = 256;  % Number of colors
2 O9 S4 y9 n7 Z3 b: ]options.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;
. |, `  Y$ L) }options.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;( m/ c6 W3 ^3 U/ y
options.colormaps{1}.map        = gray(256);
1 A" \+ }% V. M' D- N
" W( `2 ~7 v  t) P3 w" Toptions.colormaps{2}.offset     = 256;) d5 r7 D9 F. A7 p' d" |1 e5 o1 I
options.colormaps{2}.num        = 256;  % Number of colors
1 P- I3 U7 [' f" c8 J9 @4 W7 L, ]  Loptions.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;
2 x. G8 r. K7 A3 w) ?7 V& k; Woptions.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;' J! j7 q. h0 ~6 j1 X* \
options.colormaps{2}.map        = jet(256);
- [' v5 x* G. ^+ _% v/ b
5 [: V8 ^# d6 U. I" X8 Ofor iCMap = 1 : length(options.colormaps)- i5 [4 c; i! x" C0 X# z% `
    if iCMap == 1
: _/ }7 b: g: U: c3 @1 N+ X" d        cmap = options.colormaps{iCMap}.map;) C7 J$ v; T5 b# d6 S
    else
+ E/ y7 {& l  M" u' K" n        cmap = [cmap ; options.colormaps{iCMap}.map ];
3 U. T: m! ]/ @1 _: n1 o! ^% e3 H    end
2 w) h: `! U$ x+ p  Oend
6 V' c0 z2 G! R- n6 q7 s& z
  N% ~; V/ \( M9 {8 P: w5 T: L4 q9 B) R$ D5 F
我们定义了两个色图,然后把两个色图拼装起来。* t- Y- y. L9 w  S) x7 v2 K

+ Q, u' `) z) s( ~  b0 rfig4 = figure;
6 U! J4 g3 g1 {% H# Lcolormap(cmap);/ y* [- b. A* ^

. }' p& u' ^5 R. hsubplot(1,2,1);* ^/ w9 L3 P$ l! D
data1 = data +options.colormaps{1}.offset;) V1 U/ F! h# _' E  T/ {
plotHandles(1) = pcolor( data1 );
- v; [! o  v& H5 w6 jset( plotHandles(1) , 'EdgeColor' , 'none');' Z' e  t+ M% G$ P
caxis([0 , size(cmap,1) - 1]);) F3 l8 b# s9 _2 u
* T& a0 k  J5 {
: g+ J( F% L/ Q+ H8 L' m0 y0 M
subplot(1,2,2);
0 N% J0 y& d, L. d9 y. x& cdata2 = data + options.colormaps{2}.offset;% M. b( d. S1 o5 {  `6 O4 M
plotHandles(2) = pcolor( data2 );
8 w( x" C2 H' P  F& a. H% u& `. z  ^* J1 ?/ d
set( plotHandles(2) , 'EdgeColor' , 'none');3 n: q: j* e) s& F$ \: G( h
caxis([0 , size(cmap,1) - 1]);
, k2 T' \8 a2 _7 I8 S" P6 I; V
0 Y5 N+ c9 I' {6 l) j
, J* z5 C5 r) W7 K结果如下:0 S; m& ?. H+ D( M: {' `" R& @- U

/ I, z! o% u0 B1 R0 |9 \
5 O! {9 D1 \+ s, ]1 `$ E. {' T: k, e实际上我们只有一个colormap,但达到了多个colormap的效果。
9 h3 }( _5 R# `( s+ ^. F/ j! N, t" O0 \* O8 \; y- ^

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-8-16 21:23 , Processed in 0.140625 second(s), 26 queries , Gzip On.

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

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

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