Problem with colorbar and values of scale
2 次查看(过去 30 天)
显示 更早的评论
I have this image in HSV colorbar, regarding a density parameter.
Values range from 0 (air) to a maximum of 10, which was my prerrogative (it only goes up to 7.5 though).
What I want to know is why the scale starts at 0.5, and not 0? Air we consider of zero density (although it is not true, for interaction with X-rays we can consider it so)
Before someone asks, I already verified the matrix of the density variable and there is zeros around the sample, which appears in the image.
My code below:
figure;
image(rho);
colormap(hsv(ceil(rhomax-rhomin)));
cbh = colorbar('location', 'SouthOutside');
set(cbh, 'Units', 'normal', 'Xtick',0:0.5:10); %(I thought this part would make the values start from 0)
text(3.5, -1, 'Densidade (g/cm³)', 'Parent', cbh);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/162339/image.png)
回答(1 个)
Image Analyst
2014-5-8
Let's say that rho ranged from 0.7 to 7.5. So rhoMax-rhoMin = 7.5-.5 = 7. So you will have 7 color steps when hsv(7) returns. And that's what you have: 7 color steps. It makes those steps (i.e. the tick mark labels) go from rhoMax to rhoMax, which is the range in your image.. If your rho went from 1001 to 1009, you'd have 8 color steps (1009 - 1001( and the tick marks would go from 1001 to 1009 because that's what's in your image. Does that make sense?
23 个评论
Haimon
2014-5-8
It does make sense. But even then, wouldn't the program start from the minimum value, go to the maximum and divide in equal steps? The minimum value is 0, so it should start from 0 right?
Would you suggest something different?
My objective would be to show all values of density (rho). It definitely starts from 0, but the maximum value might change depending on what sample I'm working on. the sample I use
Image Analyst
2014-5-9
How are you so sure that rhoMin is zero? What does this say in the command window?
min(rho(:))
max(rho(:))
rhomin
rhomax
Haimon
2014-5-9
I'm sure because I verified it myself.
And this suggestion of yours is already on my code.
Image Analyst
2014-5-9
编辑:Image Analyst
2014-5-9
I don't have your data so I can't verify that. All I can do is generate some sample data in the same range, and it works fine. What does this do for you:
rho = 7 * rand(700,700);
min(rho(:))
max(rho(:))
imshow(rho, []);
colormap(hsv(7));
colorbar
Haimon
2014-5-9
Answers are: ans =
4.3576e-005
ans =
7.0000
and an image with colorbar ranging from 1 to 6, although the extremes seem to be 0 and 7, without actually showing values.
Image Analyst
2014-5-9
OK, so if your min is near zero, the bottom of the colorbar will be 0 and top of the color bar will be 7. Keep in mind that there does not always seem to be a tick mark at the very ends. So in your case, the min of your data must be near 0.5 and the max near 7.5 (since your tick marks seem to be at the ends).
Haimon
2014-5-10
I used the min and max, again, in rho and still shows me that I have rhomin = 0, so I still can't figure out why the lowest value is 0.5, and not 0.
Haimon
2014-5-10
It is almost 21 mgb, so I can't attach here. Would you have an e-mail so I can send it to you?
Haimon
2014-5-10
I can't attach here, but you can get on my dropbox folder.
Here is the link:
https://www.dropbox.com/sh/4v66ly0m0ciezj2/AAC1TTHDDEO_zG1Slmr6ZpBja
Image Analyst
2014-5-13
No - extremely swamped with my regular job. Maybe on the weekend... Reply to this so I'll see you as the latest poster then. If I'm the last one to reply in a post, I normally don't open it up again.
Image Analyst
2014-5-31
I downloaded your data. Here's my code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
load('C:\Users\Mark\Documents\Temporary\rho.mat');
minValue = min(rho(:));
maxValue = max(rho(:));
fprintf('The min rho value = %f.\nThe max rho value = %f.\n', ...
minValue, maxValue);
imshow(rho, []);
colormap(hsv(7));
cbh = colorbar('location', 'SouthOutside');
set(cbh, 'Units', 'normal', 'Xtick',0:0.5:10); %(I thought this part would make the values start from 0)
text(3.5, -1, 'Densidade (g/cm³)', 'Parent', cbh);
Here's my image:
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
Do you see any problem with it?
Haimon
2014-5-31
I did that using imshow while i was waiting, and it also worked.
But I tried again with figure, and it didn't. It is pretty much like the old one.
Do you know why, considering the matrix is the same one I sent you?
Image Analyst
2014-5-31
It's not figure. It seems to be image(). image() causes the colorbar to start incorrectly at 0.5, while imshow() does it correctly and starts it at 0. I don't know why.
Haimon
2014-6-2
I see. That's strange, indeed.
One last thing: after I used imshow, I got this image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/179088/image.png)
But if I tried to save it automatically from the data, I get this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/179089/image.png)
Why they seem to be different? I wanted to save them - and I cannot save one by one, because there are thousands of images - , so I can make a 3D model later, but if they are different, the colorbar will not be the same, and I wouldn't know how to get the colorbar from the second one - although it would be really be best if they were the same.
Haimon
2014-6-3
编辑:Haimon
2014-6-3
The part of the code with imshow is this:
imshow(rho, []);
colormap(hsv(rhomax-rhomin));
cbh = colorbar('location', 'SouthOutside');
set(cbh, 'Units', 'normal', 'Xtick',0:0.5:10);
text(3.5, -1, 'Densidade (g/cm³)', 'Parent', cbh);
and the next line is this:
r = sprintf('rho%04d.tif',d);
imwrite(rho,hsv((ceil(rhomax-rhomin))),r,'tif')
They are one after the other so there isn't anything between those two parts, or variables, or code, that change the colormap. Also I got those two from the same original variable, in the same run, so I also did not change my initial variable.
Image Analyst
2014-6-3
I give the same answer as in your duplicate question: http://www.mathworks.com/matlabcentral/answers/132156#comment_217525
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)