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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。9 v3 r. r/ b4 t2 O
首先我们读取图片:) O4 x3 f' a7 y* R
clear all;: E7 V: p& B+ \% o; G
close all;$ G/ r5 k5 ~' l3 O1 L
clc;  t+ E' e& O8 ?* K3 Y% Q
%%
) C/ m. i6 P% C2 j3 ], Pimgname='.\frog.bmp';+ p: e; F; ~3 n/ E/ a* z, x6 t
[imagRGB]     = imread( imgname );% f" b5 ?1 Y6 H( ?' r2 Q+ q

  P' M4 `6 O# m9 I7 q& }) sif (length(size(imagRGB)) > 2)5 e# ^0 H% K% L2 L# J& ?' h
    imagGray = rgb2gray(imagRGB);- N; X+ ]6 E* ~: X. @3 j% J
else9 ^) {5 u9 F& n0 z, q+ B
    imagGray = imagRGB;, U1 k1 x# h+ d2 R3 i  L
end: D* o7 d: {0 s
clear('imagRGB');; n7 i" E) k  ]/ Q- t
data =  double(imagGray);8 K3 B. S" R2 Z& Z+ ]. \4 n& T
data =flipdim(data,1);  
* R0 A  a6 G; h% k, W   % Flip linear dimensions (image coordinate system starts at upper left. K5 W& y. r6 L5 `( K& R: K
   % corner )
7 B7 y5 }. I9 U: w- Y7 q) F7 ?1 E' h+ r* m- P6 ?  F

& o2 }2 J# r+ N5 J& g, z7 c方法1: 我们生成两个figure,分开画两个图
1 I1 q5 I0 `6 i" Efig1=figure;
# r+ X+ O8 t  S: E6 \2 W, u7 splotHandles(1) = pcolor( data );: }& Z, G3 V4 {# k$ g+ Z5 S! Q
set( plotHandles(1) , 'EdgeColor' , 'none');4 h9 p* Y- ^8 @1 \6 d& C( U
caxis([0,255]);
* u) b/ \. g) g- S  xcolormap gray;
4 _5 L' x5 t) I- W' l8 K $ B6 P- _, i4 r9 j" i
fig2=figure;
% _& x( g. ^8 mplotHandles(1) = pcolor( data );
4 t7 j% x& U2 D7 s/ J9 l7 C1 O+ |set( plotHandles(1) , 'EdgeColor' , 'none');
/ h0 o' ]7 r; V2 @1 v$ {caxis([0,255])
9 `: p! S, |5 O6 T- T& Mcolormap jet;0 z# I; h7 g9 Z" [" U+ R6 y) f
  ^/ \  d3 Y" ~/ X9 b

2 @$ l& _6 X3 z" I: t! f/ @
" |+ z- I, G7 J/ {; T结果如下:! n0 ]. g( g2 F0 Y, S
: h/ x" Y* b+ F3 T0 ~
注意:这是两个figure,每个figure可以使用一个唯一的colormap
# u, e& z& a  r9 P* d. ~  L6 N: T* _% t( M+ ~
. e5 e9 \5 R8 u4 e2 o
方法二: 一个figure,subplot2 @& w4 m  G3 x) ?9 }& g
fig3=figure;7 Y# \% `: z1 J$ R( J2 [
2 ^) r$ B. K: \+ U) Q# e
subplot(1,2,1);
; p% Q/ b2 |. d. A# V1 @" ?6 O% rplotHandles(1) = pcolor( data );
, k0 Y" z" ^! M7 s2 w8 eset( plotHandles(1) , 'EdgeColor' , 'none');' s0 j* f2 f1 |
caxis([0,255]);1 a. r8 {: V, Y$ z  I$ I2 V9 p
colormap gray;# z; x3 u1 x' N- t& [

- n4 ]" W) L4 I5 g2 \5 B. Tsubplot(1,2,2);, c- F5 H4 u- ~5 K9 q2 i& |
plotHandles(1) = pcolor( data );
! a- v' v8 ?5 s7 w. V4 Xset( plotHandles(1) , 'EdgeColor' , 'none');  o& d: x% _2 |: Z5 K
caxis([0,255])
3 K  t! }$ N8 q+ a5 H7 hcolormap jet;
/ D& ?7 t! t, m$ n$ H
8 a1 n/ t1 ?% j! a% M3 M
( S: F5 [( [( g5 I6 @结果如何呢?8 D2 s+ e: v- w" U+ J1 M

) d' G2 o; n+ F' l4 B) j
, T" c( w7 Z" N1 Y, d) ?% |4 o
& Z+ _  r7 z! g1 r后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。) t- \) Q1 P+ ~1 H: @
" |& q0 b0 ~5 S, d" y/ Z
方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图1 l0 @" g, O; X% m" j
options.colormaps{1}.offset     = 0;
. h1 D1 R( ?1 {) }+ qoptions.colormaps{1}.num        = 256;  % Number of colors6 s4 W3 `# j, ~' M
options.colormaps{1}.cmin       = 0    + options.colormaps{1}.offset;
0 o4 }/ u# g0 O/ N( ~& @" z0 ooptions.colormaps{1}.cmax       = 255  + options.colormaps{1}.offset;" v. o8 p5 c: P6 H. B- W
options.colormaps{1}.map        = gray(256);
' ]2 P) u1 P# [' z; z* ^* L( b2 v& k1 o  w" U6 |( i( Q& {4 Q
options.colormaps{2}.offset     = 256;
0 V5 }6 N; K, D3 o, Noptions.colormaps{2}.num        = 256;  % Number of colors) d! l/ t! u( z0 t
options.colormaps{2}.cmin       = 0    + options.colormaps{1}.offset;' o4 V6 M: S  B- Z0 K
options.colormaps{2}.cmax       = 255  + options.colormaps{1}.offset;
" m; T# ?( M7 n" B: eoptions.colormaps{2}.map        = jet(256);
5 P& b& m5 U! k4 B) K
# g: E2 ~8 {& w$ K: U) dfor iCMap = 1 : length(options.colormaps); D! R' b! r2 q7 s
    if iCMap == 1
  L- {$ F) z( Q( p8 m        cmap = options.colormaps{iCMap}.map;. N  [9 C0 o/ y7 Y& {
    else! K* h6 l5 I8 B6 M
        cmap = [cmap ; options.colormaps{iCMap}.map ];4 f( k: `6 S+ C1 W, n
    end
. s! C* l. Y* uend, O  ?. K$ r/ @! S) I

: n9 }3 Q& u: b! W, f! O
2 W* u$ o3 a  k3 y我们定义了两个色图,然后把两个色图拼装起来。
2 n" I, p5 k' @' p3 g& B. a3 ]6 d/ o6 G4 A
fig4 = figure;
' W3 w$ L' ?# Hcolormap(cmap);
2 d" f) _3 M' D+ j  |
; f) E* k* K8 d6 z, Q% @# K% {7 |1 Esubplot(1,2,1);9 \. o% G: i- D  l" P
data1 = data +options.colormaps{1}.offset;
) ^/ a5 r; i2 R6 b$ J, ZplotHandles(1) = pcolor( data1 );
1 e$ B$ {) T& Q8 A' Eset( plotHandles(1) , 'EdgeColor' , 'none');
. _% S* ^, l4 V; f. ?1 kcaxis([0 , size(cmap,1) - 1]);
& ~: B  t; v, _& H 5 O) p0 I; U6 }3 ~1 G9 @' O

$ l' ~- \- q; J7 n' Asubplot(1,2,2);# e* m' Q/ z! N
data2 = data + options.colormaps{2}.offset;
: Q" G0 p9 w( S+ KplotHandles(2) = pcolor( data2 );
% h6 u, _/ j8 i  \
' s. [/ d8 [+ c3 v* h) O3 u3 x) wset( plotHandles(2) , 'EdgeColor' , 'none');
& K0 y5 l7 @$ A: p" q1 {( kcaxis([0 , size(cmap,1) - 1]);8 l& r9 Q# M: b

) W2 ~1 a1 z7 W" T* N: Y
& k" ~% t  J& B6 N* m结果如下:* ^3 c3 [2 m7 q( h

6 P  j1 L  D# R6 T: L$ Q0 k
6 f/ y' \: s" g- G( j实际上我们只有一个colormap,但达到了多个colormap的效果。
# a$ M3 U; ]0 F, ~+ [* w8 o  x) s  H7 a# W4 K

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-11-4 20:24 , Processed in 0.156250 second(s), 27 queries , Gzip On.

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

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

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