如何在子图中绘制灰度图和彩虹色图?

How to have grayscale and rainbow color plots in subplots?

我有一个 MATLAB 代码片段,它使用 subplot 在同一图中生成一些图。但是,我想在 subplot(3,3,2)subplot(3,3,5)subplot(3,3,8) 中制作 surf 图而不是灰度,并保持 subplot(1,3,1)subplot(3,3,3)subplot(3,3,6), subplot(3,3,9) 灰度。我怎样才能做到这一点?

img = imread('moon.tif');

subplot(1,3,1);
imagesc(img);
colormap(gray);
title('moon.tif');
axis tight; axis equal;

subplot(3,3,2);
sigma = 3;
gaussianfilter = fspecial('gaussian', [90,90], sigma); % typically we would choose to filter width to be six times sigma
surf(gaussianfilter);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

subplot(3,3,3);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
title('Filtered image');
axis tight; axis equal;

subplot(3,3,5);
sigma = 9;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

subplot(3,3,6);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
title('Filtered image');
axis tight; axis equal;

subplot(3,3,8);
sigma = 15;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

subplot(3,3,9);
img_filtered = conv2(img, gaussianfilter, 'same');
image(img_filtered);
title('Filtered image');
axis tight; axis equal;

您可以将所需的坐标轴传递给 colormap 函数,并指定要显示的配色方案(例如,黑色、灰色等)。轴可以从 subplot 函数中获得。

试试这个代码:

img = imread('moon.tif');

ax1 = subplot(1,3,1);
imagesc(img);
colormap(ax1, gray);
title('moon.tif');
axis tight; axis equal;

ax2 = subplot(3,3,2);
sigma = 3;
gaussianfilter = fspecial('gaussian', [90,90], sigma); % typically we would choose to filter width to be six times sigma
surf(gaussianfilter);
colormap(ax2, jet);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

ax3 = subplot(3,3,3);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
colormap(ax3, gray);
title('Filtered image');
axis tight; axis equal;

ax5 = subplot(3,3,5);
sigma = 9;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
colormap(ax5, jet);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

ax6 = subplot(3,3,6);
img_filtered = conv2(img, gaussianfilter, 'same');
imagesc(img_filtered);
colormap(ax6, gray);
title('Filtered image');
axis tight; axis equal;

ax8 = subplot(3,3,8);
sigma = 15;
gaussianfilter = fspecial('gaussian', [90,90], sigma);
surf(gaussianfilter);
colormap(ax8, jet);
title(['Gaussian filter \sigma = ', num2str(sigma)]);

ax9 = subplot(3,3,9);
img_filtered = conv2(img, gaussianfilter, 'same');
image(img_filtered);
colormap(ax9, gray);
title('Filtered image');
axis tight; axis equal;