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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。
- ?1 Z/ K1 I8 C6 e1 o' t1 l首先我们读取图片:! t! q" y; |9 q# n2 h  r
clear all;
! v# z/ M, j5 I# Gclose all;0 t* |7 K3 u" x/ D# q- B
clc;! |( g% g$ o0 L9 x+ v
%%  X) c$ R$ F- t2 n* @* y
imgname='.\frog.bmp';5 k2 U8 x  y/ N: U9 {5 F
[imagRGB]     = imread( imgname );9 ^$ k8 t( X/ A
: a0 A+ }: A# A6 Z7 ]
if (length(size(imagRGB)) > 2)$ C+ p" O$ W9 d. U6 Q- E
    imagGray = rgb2gray(imagRGB);
. s! D7 z$ N  v% q! y# _else5 B! F8 w$ q5 U0 }( y! Q( A% {$ s8 q
    imagGray = imagRGB;6 v2 c; @( D7 s8 ?8 A
end/ H; `) f- K* q$ ]
clear('imagRGB');
' q5 r( b& I; y* i' gdata =  double(imagGray);; I! n2 G% U4 k8 N8 Y% Z
data =flipdim(data,1);  8 c( j6 ]7 w0 [
   % Flip linear dimensions (image coordinate system starts at upper left
1 r; X; b, R& E5 x7 R   % corner )
/ p& j% e) [% z- \  f: S0 ?% n' p0 d2 L* f. o

3 V/ }0 ~" l# @* |2 n方法1: 我们生成两个figure,分开画两个图+ Q- z! R* a" \" h  l: B
fig1=figure;
  S$ R  T$ D/ y& P, ^* P! splotHandles(1) = pcolor( data );: A3 J% k0 K: X6 S
set( plotHandles(1) , 'EdgeColor' , 'none');
# [; g4 U1 T% @8 l% R2 lcaxis([0,255]);
8 X) e- I$ ~( pcolormap gray;
" `$ u0 [9 Y7 Y ! `; Z1 T. p1 L$ C7 B
fig2=figure;
7 _& ~1 T+ N% F3 Q$ |5 EplotHandles(1) = pcolor( data );
. e" I. H' ]8 @* p# Y% q2 z8 G0 cset( plotHandles(1) , 'EdgeColor' , 'none');1 A; x% L& U2 f4 `1 O7 R( t
caxis([0,255])1 N2 D; u" r1 o4 n, v
colormap jet;' f6 {1 A5 t8 V/ d0 }5 n& ~2 M
, r- `6 t0 g& y

1 R; e+ a8 m* d0 [& e
: F/ }0 A6 ^# l5 e: F5 |& A结果如下:! _  w0 Z0 M1 e+ \1 N1 v' J
% D: m8 s  `/ \
注意:这是两个figure,每个figure可以使用一个唯一的colormap1 g! @  X* q3 c/ h: l

4 Z3 R0 q& [  @8 Q8 R2 p
/ o; h4 P( _- M, I8 ^9 X方法二: 一个figure,subplot8 P9 x! I7 X" [* u) U( ^1 R' B5 \
fig3=figure;
8 v: }3 r7 W. \( M  F: O' H7 @% q* [" z9 x# ^) y
subplot(1,2,1);( C. r6 p, X, B+ j# `. s) Z- b$ G
plotHandles(1) = pcolor( data );. V. y4 Y. n# }% h
set( plotHandles(1) , 'EdgeColor' , 'none');# |$ s! B# A+ X" P  _' z7 {
caxis([0,255]);8 ^8 _7 R; R/ f: d: s
colormap gray;+ u0 m, Q" T) O# D0 z3 a

! v5 n/ n: q4 l; Rsubplot(1,2,2);! d- [$ E% A4 A
plotHandles(1) = pcolor( data );/ @# E7 A3 l% z) `! e5 T9 `
set( plotHandles(1) , 'EdgeColor' , 'none');
- a; `1 m/ X3 ^; C6 hcaxis([0,255])2 f* c/ Y5 z8 g9 ]. W3 f
colormap jet;
; Z7 c& p  l# ]( E, R# n
" a" p, K( F; v: O; T6 N, p) T, G* d# f0 F+ j/ |' x. }
结果如何呢?
, f6 A: T- i. M; k6 [8 c1 l8 u& c4 ?
/ R2 g' O( P: v0 V& U

& y: l  z9 E0 |6 G" ]0 v4 S- g后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。8 L) F2 A5 I. _& ?' f
5 @5 ]2 i" b# k# O2 C; b' b& y2 [
方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图, d& m( I9 y: I' S
options.colormaps{1}.offset     = 0;* v  c0 x! d; }1 ~
options.colormaps{1}.num        = 256;  % Number of colors
% ?% E* u# Z4 V* x  ?$ uoptions.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;
* l  V$ ?8 E) p: [* Coptions.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;+ h, M  m0 d- u( Y+ B" f% ^
options.colormaps{1}.map        = gray(256);9 s. {9 O$ D, a8 o
3 Q* R' Z* D' ?! `
options.colormaps{2}.offset     = 256;$ J$ R7 R: h& G; Y" L
options.colormaps{2}.num        = 256;  % Number of colors
' S% @4 o7 |7 p0 roptions.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;2 z) H  N% C' Y" F8 T
options.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;
6 p$ k$ ~5 J# ^  G$ noptions.colormaps{2}.map        = jet(256);- T1 s& M: g% g& l' a

' R0 M% W6 ^4 V$ s" |+ nfor iCMap = 1 : length(options.colormaps)
9 E, J, C! o3 t7 b( c    if iCMap == 15 c( |" Y/ h; M
        cmap = options.colormaps{iCMap}.map;
% E. M: c$ j1 P! k    else
: j7 @2 v+ s( \" H1 o+ a        cmap = [cmap ; options.colormaps{iCMap}.map ];
9 _/ S* q, Y& W7 a    end: x/ }$ h0 Y( T0 f5 w
end) R, R1 f6 V( I& V: Z+ r* u$ ]
$ W( B( r4 q% I% f5 k% b) _

9 w7 V! h% E' o) u我们定义了两个色图,然后把两个色图拼装起来。
" a# _4 w3 ~+ M% k0 T% [
4 W" n+ ~7 i- L3 O9 ~4 w0 X( pfig4 = figure;
, Q2 T* r3 o% w) F' gcolormap(cmap);
) A6 `& y) Y, t$ G/ ?
" E5 ?  g; v& Q6 I- T1 @- o1 Jsubplot(1,2,1);7 Y; g+ S: P' \& \1 e' D; ?6 N, F
data1 = data +options.colormaps{1}.offset;2 P  H: a0 W, Q
plotHandles(1) = pcolor( data1 );& `8 h) r' B, E  [* n
set( plotHandles(1) , 'EdgeColor' , 'none');
$ \; p$ `0 f5 P8 y) t! Q1 kcaxis([0 , size(cmap,1) - 1]);( B- R" v7 B3 L2 K4 ~3 q, e1 @

9 `" g! M& z# i  b) i! y
4 X! r/ V3 w# S( N6 Wsubplot(1,2,2);* s: U/ t0 d! T! T
data2 = data + options.colormaps{2}.offset;) l, m  s" u& `; ~
plotHandles(2) = pcolor( data2 );
. g) U4 y$ z+ V9 i, y
* [4 H$ K0 K0 iset( plotHandles(2) , 'EdgeColor' , 'none');3 }, a: U5 L* C8 M2 v! B) |% Y) d5 y
caxis([0 , size(cmap,1) - 1]);
& K. [# e1 b9 w( a* l; Q. O! ^, W( N2 f. x
3 u: h; }) S& n7 [, H* m
结果如下:+ y' s$ D7 K# u5 n8 w

: W0 g% ^. k: d" ~4 L  c3 g+ J2 I/ U
实际上我们只有一个colormap,但达到了多个colormap的效果。
9 K7 |% t0 ~) \3 }8 Y4 i8 a" q& u
5 v0 y' a# \3 N) q$ l6 a0 l8 N) B

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-11-24 20:06 , Processed in 0.171875 second(s), 26 queries , Gzip On.

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

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

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