2015-11-30 43 views
0

我在MATLAB中讀入圖像並使用imagesc進行顯示。然後我將colormap設置爲grey更改彩條方案

在此圖像的頂部,我用噴射顏色方案繪製點。然後如何顯示jetcolorbar,以對應於原始圖像頂部繪製的點的顏色?我嘗試在全部繪圖後重新定義colorbar,但這會將原始灰度圖像更改回顏色,這是不期望的。

代碼:

%Create Figure with handle. 
h5=figure('units','normalized','outerposition',[0 0 1 1]); 
whitebg(h5,[0 0 0]); 
subplot(2,5,1); 
k=1; 
for i=16:25 
    subplot(2,5,k); 
    imagesc(squeeze(ana(:,:,i))); 
    title(['Z=',num2str(i)]); 
    colormap gray 
    axis equal 
    k=k+1; 
    colorbar 
end 

%Adapt colour values so that they are between 0 and 1. We want to scale 
%both data sets equally, so we find the smallest value across Ix and Iy. We 
%also find what will be the new largest value across Ix and Iy, after we 
%add the magnitude of the smallest value to make all numbers greater than 
%or equal to 0. 
absolutemin=min(min(Ix(:,1)),min(Iy(:,1))); 
absolutemax=max(abs(absolutemin)+(max(Ix(:,1))),abs(absolutemin)+max(Iy(:,1))); 

%Add the smallest value, and divide by the largest maximum value for both Ix 
%and Iy. 
ixcolours=uint8(((Ix(:,1)+abs(absolutemin))/absolutemax).*255)+1; 
iycolours=uint8(((Iy(:,1)+abs(absolutemin))/absolutemax).*255)+1; 

mycolours=jet(256); 
o=1; 
for k=16:25; %For all 3D slices 

    for i=1:471; %and for all x and y seed slices 
     if k==seed_locs(i,3); 
      subplot(2,5,o); 
      hold all%go to the corresponding z subplot 
      plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',mycolours(ixcolours(i),:),'MarkerEdgeColor',mycolours(ixcolours(i),:),'MarkerSize',10,'Marker','s') %plot the x and y seedlocs 
      %hold on 
     end 
    end 

    for i=1:486; 
     if k==test_locs(i,3); 
      subplot(2,5,o); 
      hold all 
      plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',mycolours(iycolours(i),:),'MarkerEdgeColor',mycolours(iycolours(i),:),'MarkerSize',10,'Marker','s') %plot the x and y seedlocs 
%    hold on 
     end 

    end 
    o=o+1; %go to the next z subplot 
end 
colormap jet 
colorbar 

回答

3

我覺得下面的例子可以幫助你改善你的代碼。你首先需要定義兩個顏色表:

colormap([cool(64);gray(64)]); 

然後讓我們說我們有兩組不同的數據點的繪製:

[X,Y,Z] = peaks(25); 
h(1) = surf(X,Y,Z);hold on 
h(2) = pcolor(X,Y,Z); 

所以數據與兩個不同的手柄定義。現在我們需要使用最小值和最大值來製作CData。

cmin = min(Z(:)); 
cmax = max(Z(:)); 

C1 = min(64,round((64-1)*(Z-cmin)/(cmax-cmin))+1); % CData for the first datapoints 

C2 = 64+C1; % CData for the second datapoints 

現在我們更新每個對象的CDatas。

set(h(1),'CData',C1); 
set(h(2),'CData',C2); 

現在你可以設置軸的CLIM屬性:

caxis([min(C1(:)) max(C2(:))]) 
colorbar; 

enter image description here