在 Matlab 中从 1×N 向量创建 RGB 矩阵
Creating an RGB Matrix from a 1 by N vector in Matlab
我有一个包含 N 个数值的向量,数值范围从 0 到 1。有没有办法让 Matlab 根据初始向量的索引值创建一个 3×N 的 RGB 值矩阵?
我希望颜色范围从代表 0 的蓝色到代表 1 的红色,但要像 jet 颜色图中那样使用中间颜色。到目前为止,我已经成功地让 Matlab 的范围从蓝色到红色,但中间有紫色元素。
如有任何帮助,我们将不胜感激。
谢谢,
瑞安
到目前为止的代码:
%Adapt colour values so that they are between 0 and 1
ixcolours=(Ix(:,1)+abs(min(Ix(:,1))))/max(Ix(:,1)+abs(min(Ix(:,1))));
iycolours=(Iy(:,1)+abs(min(Iy(:,1))))/max(Iy(:,1)+abs(min(Iy(:,1))));
for i=1:471;
if seed_locs(i,3)==20; %If we are in the 20th seed z slice
plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerEdgeColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
hold on
elseif test_locs(i,3)==20; %if we are in the 20th test z slice
plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[iycolours(i) 0 1-iycolours(i)],'MarkerEdgeColor',[iycolours(i) 0 1-iycolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
end
end
更新
感谢 lhcgeneva 和 FirefoxMetzger 的帮助,你们的两个算法都很有用。
我已经尝试实施 FirefoxMetzger 的代码,但现在我遇到了一个关于 imagesc 的问题。之前,我的完整代码是:
%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)]);
hold on
colormap gray
axis equal
k=k+1;
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=(Ix(:,1)+abs(absolutemin))/absolutemax;
iycolours=(Iy(:,1)+abs(absolutemin))/absolutemax;
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); %go to the corresponding z subplot
plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerEdgeColor',[ixcolours(i) 0 1-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);
plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[iycolours(i) 0 1-iycolours(i)],'MarkerEdgeColor',[iycolours(i) 0 1-iycolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
end
end
o=o+1; %go to the next z subplot
end
在那里我将有一个 2 x 5 的大脑子图,通过 plot 命令将数据作为正方形叠加在它上面。
但是,现在如果我尝试使用 RGB 值,我要么只得到大脑的图像,要么只得到标绘点,但没有叠加。
%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=((Ix(:,1)+abs(absolutemin))+1/64)/(absolutemax+1/64);
iycolours=((Iy(:,1)+abs(absolutemin))+1/64)/(absolutemax+1/64);
h1x=figure();
colour_values_x=interp1((1:64)/64,colormap(h1x,'jet'),ixcolours);
close(h1x)
h1y=figure();
colour_values_y=interp1((1:64)/64,colormap(h1y,'jet'),iycolours);
close(h1y)
%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;
end
figure(h5);
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); %go to the corresponding z subplot
plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[colour_values_x(i,1) colour_values_x(i,2) colour_values_x(i,3)],'MarkerEdgeColor',[colour_values_x(i,1) colour_values_x(i,2) colour_values_x(i,3)],'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);
plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[colour_values_y(i,1) colour_values_y(i,2) colour_values_y(i,3)],'MarkerEdgeColor',[colour_values_y(i,1) colour_values_y(i,2) colour_values_y(i,3)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
end
end
o=o+1; %go to the next z subplot
end
MATLAB 有一个内置的。如果 N 是您想要的色调数(在您的情况下为 471),您可以执行
N = 471;
c = colormap(jet(N));
然后通过 c(i, :)
在每个循环迭代中访问 c。
有关参考,请参阅 man page ;).
dummy_figure = figure();
color_values = interp1((1:64)/64,colormap(dummy_figure,'jet'),vector)';
close(dummy_figure);
其中向量是任何 Nx1 向量,其条目在 [0,1] 内。请注意,仅需要虚拟图形是因为颜色图总是会打开一个新图形,除非已经打开一个图形。根据您的设置,您也许可以只使用中间线。
即使您的矢量未排序或间距不均匀,此方法也有效。
我有一个包含 N 个数值的向量,数值范围从 0 到 1。有没有办法让 Matlab 根据初始向量的索引值创建一个 3×N 的 RGB 值矩阵?
我希望颜色范围从代表 0 的蓝色到代表 1 的红色,但要像 jet 颜色图中那样使用中间颜色。到目前为止,我已经成功地让 Matlab 的范围从蓝色到红色,但中间有紫色元素。
如有任何帮助,我们将不胜感激。
谢谢, 瑞安
到目前为止的代码:
%Adapt colour values so that they are between 0 and 1
ixcolours=(Ix(:,1)+abs(min(Ix(:,1))))/max(Ix(:,1)+abs(min(Ix(:,1))));
iycolours=(Iy(:,1)+abs(min(Iy(:,1))))/max(Iy(:,1)+abs(min(Iy(:,1))));
for i=1:471;
if seed_locs(i,3)==20; %If we are in the 20th seed z slice
plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerEdgeColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
hold on
elseif test_locs(i,3)==20; %if we are in the 20th test z slice
plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[iycolours(i) 0 1-iycolours(i)],'MarkerEdgeColor',[iycolours(i) 0 1-iycolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
end
end
更新
感谢 lhcgeneva 和 FirefoxMetzger 的帮助,你们的两个算法都很有用。
我已经尝试实施 FirefoxMetzger 的代码,但现在我遇到了一个关于 imagesc 的问题。之前,我的完整代码是:
%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)]);
hold on
colormap gray
axis equal
k=k+1;
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=(Ix(:,1)+abs(absolutemin))/absolutemax;
iycolours=(Iy(:,1)+abs(absolutemin))/absolutemax;
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); %go to the corresponding z subplot
plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerEdgeColor',[ixcolours(i) 0 1-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);
plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[iycolours(i) 0 1-iycolours(i)],'MarkerEdgeColor',[iycolours(i) 0 1-iycolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
end
end
o=o+1; %go to the next z subplot
end
在那里我将有一个 2 x 5 的大脑子图,通过 plot 命令将数据作为正方形叠加在它上面。
但是,现在如果我尝试使用 RGB 值,我要么只得到大脑的图像,要么只得到标绘点,但没有叠加。
%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=((Ix(:,1)+abs(absolutemin))+1/64)/(absolutemax+1/64);
iycolours=((Iy(:,1)+abs(absolutemin))+1/64)/(absolutemax+1/64);
h1x=figure();
colour_values_x=interp1((1:64)/64,colormap(h1x,'jet'),ixcolours);
close(h1x)
h1y=figure();
colour_values_y=interp1((1:64)/64,colormap(h1y,'jet'),iycolours);
close(h1y)
%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;
end
figure(h5);
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); %go to the corresponding z subplot
plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[colour_values_x(i,1) colour_values_x(i,2) colour_values_x(i,3)],'MarkerEdgeColor',[colour_values_x(i,1) colour_values_x(i,2) colour_values_x(i,3)],'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);
plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[colour_values_y(i,1) colour_values_y(i,2) colour_values_y(i,3)],'MarkerEdgeColor',[colour_values_y(i,1) colour_values_y(i,2) colour_values_y(i,3)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
end
end
o=o+1; %go to the next z subplot
end
MATLAB 有一个内置的。如果 N 是您想要的色调数(在您的情况下为 471),您可以执行
N = 471;
c = colormap(jet(N));
然后通过 c(i, :)
在每个循环迭代中访问 c。
有关参考,请参阅 man page ;).
dummy_figure = figure();
color_values = interp1((1:64)/64,colormap(dummy_figure,'jet'),vector)';
close(dummy_figure);
其中向量是任何 Nx1 向量,其条目在 [0,1] 内。请注意,仅需要虚拟图形是因为颜色图总是会打开一个新图形,除非已经打开一个图形。根据您的设置,您也许可以只使用中间线。
即使您的矢量未排序或间距不均匀,此方法也有效。