|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
我们有一张图片,读进来后,转换为gray,然后打算画两张图,一张用gray的色图,一张用jet的色图。
' {6 i* Y/ z4 q7 x7 E3 ^首先我们读取图片:
. m6 I3 A( c+ ]clear all;
5 ~, v# q: g& w" T; Cclose all;
8 f" r3 R+ i2 f" ~clc;
' x6 x$ z* b/ X0 f%%1 Q- H* d Q. i: `% d0 u% g
imgname='.\frog.bmp';
6 h# t; ~* W0 W' f$ Z[imagRGB] = imread( imgname );
& }3 d: i7 r* C) R' W' V4 _, M6 c; j) l
if (length(size(imagRGB)) > 2)
* z) e0 P- D, A X+ P imagGray = rgb2gray(imagRGB);" Z( A6 m" P' }7 Z
else+ o% R; p) f; w2 S2 B2 T
imagGray = imagRGB;* p' m7 L; |* l- u- K
end8 d4 B/ @% ?) s7 `6 m U" G) P
clear('imagRGB');
. c J3 _4 ^4 }/ w% q# ^4 sdata = double(imagGray);
3 f& J2 U- z) m$ tdata =flipdim(data,1); 3 }2 w8 k1 ]; u6 w6 S' w$ {
% Flip linear dimensions (image coordinate system starts at upper left! Y4 H; b1 V7 \: C5 k# N
% corner )% C' }" x! x9 N% ^- A) L+ y
( `' h% \3 i) P* w
1 ~; U% r9 p; P' Z$ E# ]方法1: 我们生成两个figure,分开画两个图
+ m7 A& b4 D' p' |# l+ Wfig1=figure;/ [1 h4 [: \3 j; ~* n: y5 |
plotHandles(1) = pcolor( data );
+ U- ?$ \9 ^6 M! q5 yset( plotHandles(1) , 'EdgeColor' , 'none');
2 V8 c5 k3 x2 d& S0 o* g- Z4 Pcaxis([0,255]);
2 v: G: @( J) `3 J; W0 b) Jcolormap gray;/ L; y" Z6 _+ O6 `! X) c- f/ z8 A, \
3 U+ Q- b- k( Z/ R
fig2=figure;6 `% _! L% t2 V* f: i
plotHandles(1) = pcolor( data );
5 j. @1 {& t. s, jset( plotHandles(1) , 'EdgeColor' , 'none');
' q/ f7 y6 M7 J+ X7 y ~8 k& m4 \caxis([0,255])& M& G9 t/ |5 F( Z2 D) w) ?
colormap jet;2 t3 n! i9 K; @# ~
1 y8 W5 V6 W6 _, C, Z
7 f4 A# c+ R9 j5 Q
( R& D4 G" ?3 y) q/ H
结果如下:- b+ G# A2 `+ Q* s
3 c2 _* Q% _$ V% f1 r注意:这是两个figure,每个figure可以使用一个唯一的colormap4 @& Q' R9 `) u3 J0 M0 J9 {& Z
7 @1 I. c8 u3 ^* B7 m; V3 c' U9 h' ~+ ]+ `0 V
方法二: 一个figure,subplot& T( q2 Z) x# w
fig3=figure;
: r# \- P( j1 e! A3 W* `7 Q, X5 m9 q7 L8 i: K: D4 `/ l
subplot(1,2,1);/ O# V) z/ E( X2 ]7 U4 w* r
plotHandles(1) = pcolor( data ); M4 F* f& w. a9 Y
set( plotHandles(1) , 'EdgeColor' , 'none');
+ H1 M5 @( y1 V3 T4 x, ecaxis([0,255]);
D1 U1 b% ]( `: Bcolormap gray;- }) k. R& [; T4 j/ R7 J7 W" _4 a$ P
3 ~# y& ?8 n3 L' c* e% Z: {
subplot(1,2,2);. Y5 z( V3 {% ^ ^8 L+ _9 m( \# t
plotHandles(1) = pcolor( data );
* x! m5 X3 Y- c7 Xset( plotHandles(1) , 'EdgeColor' , 'none');0 T2 |5 H# t' x' P1 U* s9 r
caxis([0,255])
- K+ J5 N; r) b r3 Ecolormap jet;) Y' m- ^+ E9 u; L# \/ j- _9 C
, I9 m, ?) d3 G0 o* h9 d* L. P* e8 V
* v ?# t( G) F8 I结果如何呢?5 n& d+ o9 H# t
8 B, c" N. I4 ]
5 O! ]7 U% J6 @+ T1 x ~
! T9 y) l! Z+ U# m6 X后面定义的colormap会把前面的覆盖掉。因此最终两张图片都使用了jet的色图。
9 ^: t/ v3 G1 Z# A' ?- X. f
# t- |" a! N1 Y* g t. |方法三: 既然只能使用一个colormap,那么我们就自己动手组装我们需要的色图; o, r A; _( s: d- |1 W
options.colormaps{1}.offset = 0;' K% a- m( l' M) G. `9 b1 i* K
options.colormaps{1}.num = 256; % Number of colors
: C# e8 g5 v# X& e# R+ X( goptions.colormaps{1}.cmin = 0 + options.colormaps{1}.offset;
# D$ G% s. A' Loptions.colormaps{1}.cmax = 255 + options.colormaps{1}.offset;& l& V" o2 o: d0 ]% d
options.colormaps{1}.map = gray(256);
' n, d/ M& ?& Q- G
6 h0 Z& C/ b& R% ~4 Y( I) hoptions.colormaps{2}.offset = 256;
% @5 ~* I% B+ Y uoptions.colormaps{2}.num = 256; % Number of colors/ {+ F: }7 z; V5 w a# w) O( R+ J6 s
options.colormaps{2}.cmin = 0 + options.colormaps{1}.offset;' Q2 d/ H+ l) j6 ^: U; h# E
options.colormaps{2}.cmax = 255 + options.colormaps{1}.offset;
( c. p$ U) |" r. soptions.colormaps{2}.map = jet(256);6 B2 z" v1 K# `! Q, G5 Z; |
* E; v( {& u+ ofor iCMap = 1 : length(options.colormaps)2 F! Z% B. o- |1 l2 J
if iCMap == 10 g, _1 g' V0 p1 W- _5 t
cmap = options.colormaps{iCMap}.map;9 ^+ l( V7 y2 T' s
else$ `+ J- s" w5 T7 Y! {1 d
cmap = [cmap ; options.colormaps{iCMap}.map ];: a/ [, E% U' m- u, T
end
/ v) c" y: D) T# B( uend
; W- i* F2 K! u+ \2 Z- w# J7 Q( B- c/ }5 A3 Q3 _1 D+ U
& y" y+ O L. p1 _
我们定义了两个色图,然后把两个色图拼装起来。( p( p- o+ l3 x
" g# N4 E/ V' C: I8 n
fig4 = figure;
7 T" N) K! K& G& Ycolormap(cmap);
) ^# R9 i6 e4 v$ S; `$ O% T% o: z3 e& x4 B
subplot(1,2,1);( Z3 c5 R0 J9 r# G
data1 = data +options.colormaps{1}.offset;2 e+ L# J& y- F! k. P3 l
plotHandles(1) = pcolor( data1 );& @. T; Z, i/ {1 w S6 O
set( plotHandles(1) , 'EdgeColor' , 'none');, \/ |% l8 y( `6 E! a5 {) U
caxis([0 , size(cmap,1) - 1]);( p( S9 R1 @3 R& t3 K! d
* I+ ], y1 i9 m2 v9 H- X7 Y$ W1 i5 S( A) g5 z. I* ~
subplot(1,2,2);7 Q7 u* O8 S% R6 `
data2 = data + options.colormaps{2}.offset;8 K7 c9 t9 X% Y- W6 Y1 A! w$ K
plotHandles(2) = pcolor( data2 );
. \5 K, q# }' j3 m( U/ c7 G& K: u$ e
set( plotHandles(2) , 'EdgeColor' , 'none');
! ~5 k7 ^" E( a0 a: c' ]caxis([0 , size(cmap,1) - 1]);" C. v" p! i& p/ \3 v5 ] k# f
: V5 B* `. m& ]$ e
( U3 W# O" ^5 o, `$ l结果如下:
I& i' h9 p0 p8 } c
7 t8 C3 g# _( u+ f" ^: t0 e; i
: u4 X; T: ~& q P* K0 \; k实际上我们只有一个colormap,但达到了多个colormap的效果。
; M+ K- w1 J! C6 L1 y+ D2 Z* a5 s6 u- W6 b
|
|