Matlab-图像的比例色条 [英] Matlab - Scale Colorbar of Image
问题描述
如何缩放假彩色图像的colorbar
轴?
How can I scale the colorbar
axis of a false color image?
我阅读了这篇文章,并复制了代码,但似乎无法正常工作:
I read this post,and copied the code but it seems not to work correctly:
请参见下面的两个图像.在第一个(没有缩放比例)中,色轴变为
Please see the two images below. In the first (without the scaling) the coloraxis goes
[1 2 3 4 5 6]*10^4
在第二张图片中
[0.005 0.01 0.015 0.02 0.025]
正确的缩放比例(使用C = 100000
)应该是
The correct scaling (with C = 100000
) would be
[0.1 0.2 0.3 0.4 0.5 0.6]
不缩放
缩放错误
我希望色轴按1/C
缩放,并且我可以自由选择C
,这样当像素值= 10^4
和C=10^6
时,缩放比例应显示为10^-2
.
I want that the coloraxis is scaled by 1/C
and I can freely choose C
, so that when the pixel value = 10^4
and C=10^6
the scale should show 10^-2
.
我首先将图像乘以C
的原因是要获得更多的小数位,因为所有小于1的值都将显示为零,而无需C
缩放.
The reason why I multiply my image first by C
is to get more decimals places, because all values below 1 will be displayed as zero without the C
scaling.
运行代码时,我得到yticks
作为具有以下值的工作区变量:
When I run the code I get yticks
as a workspace variable with the following values:
[500 1000 1500 2000 2500]
我的代码:
My code:
RGB = imread('IMG_0043.tif');% Read Image
info = imfinfo('IMG_0043.CR2'); % get Metadata
C = 1000000; % Constant to adjust image
x = info.DigitalCamera; % get EXIF
t = getfield(x, 'ExposureTime');% save ExposureTime
f = getfield(x, 'FNumber'); % save FNumber
S = getfield(x, 'ISOSpeedRatings');% save ISOSpeedRatings
date = getfield(x,'DateTimeOriginal');
I = rgb2gray(RGB); % convert Image to greyscale
K = 480; % Kamerakonstante(muss experimentel eavaluiert werden)
% N_s = K*(t*S)/power(f,2))*L
L = power(f,2)/(K*t*S)*C; %
J = immultiply(I,L); % multiply each value with constant , so the Image is Calibrated to cd/m^2
hFig = figure('Name','False Color Luminance Map', 'ToolBar','none','MenuBar','none');
% Create/initialize default colormap of jet.
cmap = jet(16); % or 256, 64, 32 or whatever.
% Now make lowest values show up as black.
cmap(1,:) = 0;
% Now make highest values show up as white.
cmap(end,:) = 1;
imshow(J,'Colormap',cmap) % show Image in false color
colorbar % add colorbar
h = colorbar; % define colorbar as variable
y_Scl = (1/C);
yticks = get(gca,'YTick');
set(h,'YTickLabel',sprintfc('%g', [yticks.*y_Scl]))
ylabel(h, 'cd/m^2')% add unit label
title(date); % Show date in image
caxis auto % set axis to auto
datacursormode on % enable datacursor
img = getframe(gcf);
nowstr = datestr(now, 'yyyy-mm-dd_HH_MM_SS');
folder = 'C:\Users\Taiko\Desktop\FalseColor\';
ImageFiles = dir( fullfile(folder, '*.jpg') );
if isempty(ImageFiles)
next_idx = 1;
else
lastfile = ImageFiles(end).name;
[~, basename, ~] = fileparts(lastfile);
file_number_str = regexp('(?<=.*_)\d+$', basename, 'match' );
last_idx = str2double(file_number_str);
next_idx = last_idx + 1;
end
newfilename = fullfile( folder, sprintf('%s_%04d.jpg', nowstr, next_idx) );
imwrite(img.cdata, newfilename);
推荐答案
问题:
1)您正在获得图形的YTick
(gca
),但没有获得颜色栏.这将为您提供图形的像素"坐标,而不是实际值.使用yticks = get(h,'YTick');
.
1) You are getting YTick
of the figure (gca
) but not the color bar. That would give you the "pixel" coordinates of the graph, instead of the actual values. Use yticks = get(h,'YTick');
.
2)caxis auto
应该在覆盖YTicks
之前(以及启用颜色栏之后);否则刻度和刻度将不匹配.
2) caxis auto
Should come before overwriting YTicks
(and after enabling the color bar); otherwise the scale and ticks will mismatch.
3)您是说C = 100000
吗?
结果:
这篇关于Matlab-图像的比例色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!