How to plot colorbar as seperate image?

13 ビュー (過去 30 日間)
David Weller
David Weller 2020 年 2 月 4 日
コメント済み: David Weller 2020 年 2 月 5 日
Hi, I would like to plot the colorbar of an image not next to my image, but in a seperate figur window. Therefore the colorbar should even be plotted in the size of an image.
I am looking for something like this:
min_val=min(example_img(:));
max_val=max(example_img(:));
figure(1),
imshow(example_img,[min_val max_val]); %show the image, but without colorbar
h=colorbar
%do something to prevent showing the colorbar in figure(1)
colorbar_img=get_colorbar_img(h) %do something to get an image that shows the %colorbar and has the same size as example_img
figure(2)
imshow(colorbar)
thanks for you help!

採用された回答

TADA
TADA 2020 年 2 月 4 日
編集済み: TADA 2020 年 2 月 4 日
I assume your image is grayscale, right?
If I understand you correctly:
min_val=min(example_img(:));
max_val=max(example_img(:));
figure(1);
imshow(example_img, [min_val, max_val]);
% setting the color map of the image to whatever we want
% I chose parula here but you can use whatever colormap
set(gca, 'colormap', parula);
% this generates a single column colorbar the same height as your image
% mapping colors to your image values between the calculated minimum and maximum
% because you wanted it as an image i translated it back to uint8
colorColumn = uint8(linspace(double(min_val), double(max_val), size(example_img, 1))');
% this will stretch the color-bar to the same width as your image
colorBarIm = repmat(colorColumn, 1, size(example_img, 2));
% this will display the color bar image on a second figure
figure(2);
imshow(colorBarIm);
% flip the axis view point because images are shown from top to bottom
% but colorbars are displayed bottom-up
view(180, 90);
% setting the color map of the colorbar image to match the one
% we set for the actual image
set(gca, 'colormap', parula);
% You probably also want to show the values corresponding to the colormap
axis on;
% remove the x tick marks because they are usually meaningless ona colorbar
xticks([]);
  1 件のコメント
David Weller
David Weller 2020 年 2 月 5 日
Wow, thanks a lot for helping me out so fast :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by